NGINX:允许多个端口可用于https +将所有http重定向到https

时间:2017-01-19 02:04:25

标签: linux redirect nginx https

我尝试部署一台NGINX服务器,该服务器通过https托管两个node.js Express应用程序。

我的主站点(在端口80上提供的站点)是在端口8001上运行的Express应用程序。(即https://example.com加载此应用程序)

我还在端口8002上运行另一个Express应用程序,我想在端口8080上公开提供。(即https://example.com:8080加载此应用程序)

这是我的/etc/nginx/sites-available/default文件:

server {
   listen 80;
   listen [::]:80;
   server_name example.com www.example.com;
   return 301 https://$server_name$request_uri;
}

server {
  # SSL configuration
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name example.com www.example.com;
  include snippets/ssl-example.com.conf;
  include snippets/ssl-params.conf;

  # Pass requests for / to localhost:8001:
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:8001/;
    proxy_ssl_session_reuse off;
    proxy_set_header Host $http_host;
    proxy_cache_bypass $http_upgrade;
    proxy_redirect off;
  }

  location ~ /.well-known {
    allow all;
  }
}

server {
  listen 8080 ssl;
  server_name example.com www.example.com;
  return 301 https://$server_name$request_uri;

  include snippets/ssl-example.com.conf;
  include snippets/ssl-params.conf;

  # pass requests to port 8002 where our other node server is running
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:8002/;
    proxy_ssl_session_reuse off;
    proxy_set_header Host $http_host;
    proxy_cache_bypass $http_upgrade;
    proxy_redirect off;
  }
}

如果有任何其他帮助,我一直在关注配置https和NGINX的DigitalOcean指南 herehere

1 个答案:

答案 0 :(得分:1)

从第3个服务器块中删除return 301 https://$server_name$request_uri;