我正在尝试部署一个通过 https 托管两个 node.js Express 应用程序的 NGINX 服务器。
我的主站点(将在端口 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;
}
}
如果有任何额外帮助,我一直在按照 DigitalOcean 指南配置 https 和 NGINX here 和 here .
最佳答案
从第三个服务器 block 中删除 return 301 https://$server_name$request_uri;
。
关于linux - NGINX:允许多个端口可用于 https + 将所有 http 重定向到 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732568/