NGINX:使用1个域名为多个端口设置SSL证书

时间:2018-05-29 18:59:46

标签: node.js rest ssl nginx

我已经构建了一个使用Rest API获取其所有数据的网站。我的网站使用SSL认证进行保护。我的默认文件(etc/nginx/sites-enabled/default)如下所示:

server {
    listen 80;
    server_name example.com;
    rewrite ^/(.*) https://example.com/$1 permanent;
}

server {
    listen 443 ssl;
    listen [::]:80 default_server;

    root /var/www/example;

    index index.html;

    server_name example.com;
    ssl_certificate /root/example.com.crt;
    ssl_certificate_key /root/example.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    location / {
            try_files $uri $uri/ =404;
    }
}

问题是我的Rest API(我从中获取所有数据)必须有SSL证书以及安全地将所有数据传输到我的网站。

enter image description here

我在默认文件(etc/nginx/sites-enabled/default)中为其余的api创建了另一个服务器块。它看起来像这样:

server {
    listen 8877;
    server_name example.com;
    rewrite ^/(.*) https://example.com:8877/$1 permanent;
}

server {
    listen 443 ssl;
    listen [::]:8877 default_server;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name example.com;
    ssl_certificate /root/example.com.crt;
    ssl_certificate_key /root/example.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    location / {
            proxy_pass http://example.com:1111;
    }
 }

我知道我应该像这样把它们结合起来:

server {
    listen 80ssl;
    listen 8877 ssl;

    index index.html index.htm index.nginx-debian.html;

    server_name example.com;
    ssl_certificate /root/example.com.crt;
    ssl_certificate_key /root/example.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    location / {
        // DO SOMETHING
    }
}

问题是我需要位置块在端口80和端口8877上的功能不同。在端口8877上,位置块应指向我在后台proxy_pass http://example.com:1111;中运行的NodeJS项目。在端口80上它不应该指向我的NodeJS项目。我怎么能做到这一点?

或者有更好的方法来实现这一目标吗?我遇到这个问题已经被困了2天了。购买第二个域或SSL证书不是一个选项+我的ceritifcate支持单个域上的多个端口。

1 个答案:

答案 0 :(得分:1)

这就是我要做的/尝试:

(如果你不需要,你应该考虑关闭TLS 1.0)

# General HTTP to HTTPS
server {
        listen 80;
        listen [::]:80;
        server_name example.com default_server;

        location / {
                return 302 https://$host$request_uri;
        }
}

server {
    listen 443 ssl;
    server_name example.com default_server;

    root /var/www/example;
    index index.html;

    ssl_certificate /root/example.com.crt;
    ssl_certificate_key /root/example.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    location / {
            try_files $uri $uri/ =404;
    }
}

server {
    listen 8877 ssl;
    listen [::]:8877 ssl;
    server_name example.com;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    ssl_certificate /root/example.com.crt;
    ssl_certificate_key /root/example.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    location / {
            proxy_pass http://example.com:1111;
    }
 }