nginx https如何配置多个域名

有两个无关的项目,都配置https证书,用的免费证书。下面是两个配置文件。
cheesi.cn.conf

server {
    listen 80;
    #填写绑定证书的域名
    server_name www.cheesi.cn;
    #把http的域名请求转成https
    rewrite ^(.*)$ https://$host$1 permanent;
}

server {
    listen       443;
    server_name  www.cheesi.cn;
    ssl_certificate ./conf.d/1_www.cheesi.cn_bundle.crt;
    ssl_certificate_key ./conf.d/2_www.cheesi.cn.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;



    charset utf-8;
    access_log  /var/log/nginx/cheesi.cn.access.log  main;
    error_log  /var/log/nginx/cheesi.cn.error.log  error;


    set $root_path '/www/cheesi/public';
    root $root_path;
    index index.php index.html index.htm;

    client_max_body_size 20m;

    try_files $uri $uri/ @rewrite;

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }

    location ~* ^/(css|img|js|flv|swf|download|html)/(.+)$ {
        root $root_path;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass   phalcon_php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param  SCRIPT_FILENAME  /www/cheesi/public$fastcgi_script_name;
        fastcgi_param HTTPS $https if_not_empty;
        fastcgi_param   HTTP_SCHEME         https;
        include        fastcgi_params;

    }

knowledge.cheesi.cn.conf

server {
    listen 80;
    #填写绑定证书的域名
    server_name knowledge.cheesi.cn;
    #把http的域名请求转成https
    rewrite ^(.*)$ https://$host$1 permanent;
}
server {
    listen       443;
    server_name  knowledge.cheesi.cn;
    ssl_certificate ./conf.d/1_knowledge.cheesi.cn_bundle.crt;
    ssl_certificate_key ./conf.d/2_knowledge.cheesi.cn.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;

    charset utf-8;
    access_log  /var/log/nginx/knowledge.cheesi.cn.access.log  main;
    error_log  /var/log/nginx/knowledge.cheesi.cn.error.log  error;

    client_max_body_size 50M;
    keepalive_timeout 300;

    set $root_path '/www/cheesi_knowledge';
    root $root_path;
    index index.php index.html index.htm;

    try_files $uri $uri/ @rewrite;

    location ~* ^/(css|img|js|flv|swf|download|html)/(.+)$ {
        root $root_path;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass   wordpress_php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param  SCRIPT_FILENAME  /www/cheesi_knowledge$fastcgi_script_name;
        fastcgi_param HTTPS $https if_not_empty;
        fastcgi_param   HTTP_SCHEME         https;
        include        fastcgi_params;
    }

目前这两个配置文件不知道哪里冲突,如果按照当前设置,那么https://knowledge.cheesi.cn 会报错SSL_ERROR_BAD_CERT_DOMAIN,
如果在任意conf文件中listen 443 改为 listen 443 ssl 那么https://knowledge.cheesi.cn 访问正常而https://www.cheesi.cn 会提示The plain HTTP request was sent to HTTPS错误。

阅读 2.8k
1 个回答

listen 443 ssl;如果不写ssl就相当于没有启动ngx_http_ssl_module 模块,之前的写法是ssl on ,nginx 1.15.0. 以后改用 listen 443 ssl 这种写法

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题