在Nginx(https)中托管多个服务器块

时间:2018-09-30 02:12:31

标签: node.js nginx

我当前的nginx配置看起来像这样

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

    server_name api.myapp.com;

    location / {
        proxy_pass http://localhost:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

将生产Web应用程序托管在api.example.com上,但是现在我需要另一个配置来进行暂存构建。我可以旋转节点的另一个过程,使暂存webapp可在http://localhost:3002上访问,但是由于暂存webapp也必须是https,所以第二个配置块应该在nginx中吗?

1 个答案:

答案 0 :(得分:0)

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

    server_name api.myapp.com;
    ###
    ssl configure
    ###

    location / { 
        if ($server_port = 443) {
            proxy_pass http://localhost:3002;
        }
        if ($server_port = 80) {
            proxy_pass http://localhost:3001;
        }
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;   

    }      
}