Nginx 提供错误的证书

Nginx 提供错误的证书

我在使用 nginx 时遇到了问题。它提供了错误的证书。我使用的是 cent OS 7,目前已经部署了 2 个运行良好的 net core api 和一个带有 kestrel 的 net core mvc。mvc 的证书有问题,这是我的配置文件:

#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.


    include        /etc/nginx/proxy.conf;


    include        /etc/nginx/conf.d/*.conf;


    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
add_header Access-Control-Allow-Origin *;
       }

        error_page 404 /404.html;
        location = /404.html {
        }

       error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }
}

和我的 mvc.conf 文件:

    upstream AdminMvc{
        server localhost:5000;
    }

#    server {
 #       listen     80;
#   server_name  admin.domain.com;
 #      return     301 https://$host$request_uri;
  #  }

    server {
        listen                    443 ssl;
        server_name               admin.domain.com;
        ssl_certificate           /etc/letsencrypt/live/admin.domain.com/fullchain.pem;
        ssl_certificate_key       /etc/letsencrypt/live/admin.domain.com/privkey.pem;
        ssl_protocols             TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers               "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
        ssl_ecdh_curve            secp384r1;
        ssl_session_cache         shared:SSL:10m;
        ssl_session_tickets       off;
        ssl_stapling              on; #ensure your cert is capable
        ssl_stapling_verify       on; #ensure your cert is capable

        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
        add_header X-Frame-Options DENY;
        add_header X-Content-Type-Options nosniff;

        #Redirects all traffic
        location / {
            proxy_pass http://localhost:5000;
        }
    } 

和我的api.conf:

    upstream apiNetCore{
        server localhost:5200;
    }

#    server {
 #       listen     80;
#   server_name  api.domain.com;
 #      return     301 https://$host$request_uri;
  #  }

    server {
        listen                    443 ssl;
        server_name               api.domain.com;
    ssl_certificate /etc/letsencrypt/live/api.domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/api.domain.com/privkey.pem;
        ssl_protocols             TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers               "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
        ssl_ecdh_curve            secp384r1;
        ssl_session_cache         shared:SSL:10m;
        ssl_session_tickets       off;
        ssl_stapling              on; #ensure your cert is capable
        ssl_stapling_verify       on; #ensure your cert is capable

        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
        add_header X-Frame-Options DENY;
        add_header X-Content-Type-Options nosniff;

        #Redirects all traffic
        location / {
            proxy_pass http://localhost:5200;
        }
    
}

其他 api 与前面相同。打开 mvc 站点时显示 ERR_CERT_COMMON_NAME_INVALID 并提供 api 证书。在此行导入 .conf 文件 include /etc/nginx/conf.d/*.conf;

答案1

服务器返回的证书与 URL 中的名称不匹配。根据此描述,您已订购了 example.com 的证书,但尝试以 test.example.com 的身份访问该网站,而该域名并不是颁发证书的域名。

此问题可能是由于对 URL 中的域名与证书的比较方式存在错误理解所致。一般来说:

  • 证书仅对证书的主题备用名称部分中明确提及的域有效(Chrome 会忽略 CN)。这意味着 example.com 与 test.example.com 不匹配。
  • 如果您有通配符,则只能有一个 *,它必须是最左边的标签,并且只匹配域的单个部分,即 *.example.com 的证书将匹配www.example.com和 test.example.com 但不是www.test.example.com

相关内容