我正在尝试在nginx反向代理后面使用nodejs app来处理ssl
我的应用程序在localhost:2000上运行。我可以确认这是使用curl命令。
这是我的nginx设置:
# the IP(s) on which your node server is running. I chose port 3000.
upstream dreamingoftech.uk {
server 127.0.0.1:2000;
keepalive 16;
}
# the nginx server instance
server {
listen 0.0.0.0:80;
server_name dreamingoftech.uk;
return 301 https://$host$request_uri;
}
#HTTPS
server {
listen 443 ssl http2;
server_name dreamingoftech.uk;
access_log /var/log/nginx/dreamingoftech.log;
error_log /var/log/nginx/dreamingoftech.error.log debug;
ssl_certificate /etc/letsencrypt/live/dreamingoftech.uk/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/dreamingoftech.uk/privkey.pem;
include snippets/ssl-params.conf;
# pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://dreamingoftech.uk/;
proxy_redirect off;
#proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "";
proxy_ssl_session_reuse off;
proxy_cache_bypass $http_upgrade;
}
}
如果我现在卷曲https://dreamingoftech.uk,它需要一段时间,但我确实得到了网页。虽然有消息:
卷曲:(18)传输关闭,剩余1个字节读取
但是,从浏览器查看时,我收到502网关错误。
我检查了错误日志,结果如下:ERROR LOG
我无法理解为什么反向代理会在流程中添加这样的时间延迟。任何想法都将不胜感激。
PS:在上游配置中我尝试过localhost而不是127.0.0.1无效
答案 0 :(得分:0)
我的配置几乎相同。你能尝试下面的
吗?您可以将所有http重定向到https
server {
listen 80;
return 301 https://$host$request_uri;
}
或针对此类
的特定网站server {
server_name dreamingoftech.uk;
return 301 https://dreamingoftech.uk$request_uri;
}
但只为您的案例选择一个
然后确保节点服务器在http模式下运行,不 https。
你还提到你在端口3000上运行节点,然后使用端口3000而不是2000,正如我在你的配置中看到的那样。
确认上述内容后,将所有数据包重定向到本地主机
server {
listen 443;
server_name dreamingoftech.uk;
ssl_certificate /etc/letsencrypt/live/dreamingoftech.uk/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/dreamingoftech.uk/privkey.pem;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Fix the “It appears that your reverse proxy set up is broken" error.
proxy_pass http://localhost:3000;
proxy_read_timeout 90s;
proxy_redirect http://localhost:3000 https://dreamingoftech.uk;
}
}
创建一个文件,并将上面的代码汇总到sites-available
,其名称为dreamingoftech.uk
,并使用ln -s
创建一个到sites-enabled
的软链接。转到nginx.conf
并确保包含文件夹sites-enabled
然后必须重新启动 nginx以检查它是否有效
答案 1 :(得分:0)
@Stamos感谢您的回复。我试过了,但不幸的是它没有用。我决定尝试最基本的节点应用程序,我仍然可以使用我正在使用的基本模块。
我尝试了这个并且它立即起作用。
问题在于我的应用程序。我会一步一步地重建和测试,直到找到问题为止,
谢谢你的时间!