使用 NGINX 反向代理时如何将 HTTP 重定向到 HTTPS?
How to redirect HTTP to HTTPS while using an NGINX reverse proxy?
我正在尝试在 AWS 上使用 NGINX 作为 运行 节点服务器的反向代理。如果我转到 https://example.com/ , my connection is secure and everything is fine. But, when I go to http://example.com/ ,不会发生重新路由,并且我的连接不安全。我还在后台使用 pm2 运行 节点服务器。
我已经尝试了 google 问题时出现的默认服务器块重新路由,但到目前为止没有任何效果。我的猜测是 Node 正在处理端口 80 上的请求,因为我的网站按照我完全设置网站之前的方式出现。但我不知道如何解决这个问题。
这是我在 /etc/nginx/nginx.conf 中的服务器块:
server {
# if ($host = www.example.com) {
# return 301 https://$host$request_uri;
# } # managed by Certbot
listen 80;
listen [::]:80;
server_name _;
return 301 https://$host$request_uri; # managed by Certbot
}
server {
server_name www.example.com example.com; # managed by Certbot
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://localhost:3000;
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;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
listen [::]:443 ssl ipv6only=on default_server; # managed by Certbot
listen 443 ssl default_server; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # mana$
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # ma$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
非常感谢任何建议,因为这是针对投资组合网站的,大多数地方不会 link 直接到 HTTPS。
如果其他人遇到过这个问题,我设法解决了这个问题。在阳光下尝试了一切之后,我记得在按照在线指南从地址中删除端口号时,我搞砸了我的 iptables。我通过擦除我的 iptables 配置解决了我的问题,并且由于我使用的是代理,所以我不需要重新路由端口。
我正在尝试在 AWS 上使用 NGINX 作为 运行 节点服务器的反向代理。如果我转到 https://example.com/ , my connection is secure and everything is fine. But, when I go to http://example.com/ ,不会发生重新路由,并且我的连接不安全。我还在后台使用 pm2 运行 节点服务器。
我已经尝试了 google 问题时出现的默认服务器块重新路由,但到目前为止没有任何效果。我的猜测是 Node 正在处理端口 80 上的请求,因为我的网站按照我完全设置网站之前的方式出现。但我不知道如何解决这个问题。
这是我在 /etc/nginx/nginx.conf 中的服务器块:
server {
# if ($host = www.example.com) {
# return 301 https://$host$request_uri;
# } # managed by Certbot
listen 80;
listen [::]:80;
server_name _;
return 301 https://$host$request_uri; # managed by Certbot
}
server {
server_name www.example.com example.com; # managed by Certbot
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://localhost:3000;
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;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
listen [::]:443 ssl ipv6only=on default_server; # managed by Certbot
listen 443 ssl default_server; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # mana$
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # ma$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
非常感谢任何建议,因为这是针对投资组合网站的,大多数地方不会 link 直接到 HTTPS。
如果其他人遇到过这个问题,我设法解决了这个问题。在阳光下尝试了一切之后,我记得在按照在线指南从地址中删除端口号时,我搞砸了我的 iptables。我通过擦除我的 iptables 配置解决了我的问题,并且由于我使用的是代理,所以我不需要重新路由端口。