简体   繁体   English

无法获得 Http -> Https 301 重定向以使用 Nginx

[英]Unable to get Http -> Https 301 redirect to work with Nginx

Unfortunately I'm unable to get this HTTP redirect to HTTPS to work.不幸的是,我无法将此 HTTP 重定向到 HTTPS 工作。 I'm hosting a web app and running it with PM2, with nginx acting as a reverse proxy.我正在托管一个 Web 应用程序并使用 PM2 运行它,而 nginx 充当反向代理。 I have no problems when I have my location / block inside the listen 80 block, but after using the letsencrypt certbot, the site just won't load.当我的位置/块位于 listen 80 块内时,我没有任何问题,但是在使用 letencrypt certbot 后,该站点将无法加载。 I'm getting no errors in /var/log/nginx/errors.log, but also no records of access in /var/log/nginx/access.log.我在 /var/log/nginx/errors.log 中没有收到错误,但在 /var/log/nginx/access.log 中也没有访问记录。 Nginx syntax test passes. Nginx 语法测试通过。 When I look at the Chrome network tab, it just shows the 301 redirect and that the headers are provisional.当我查看 Chrome 网络选项卡时,它只显示 301 重定向并且标头是临时的。 I have port 80 and 443 and nginx full open on ufw, using ubuntu 20.04.我使用 ubuntu 20.04 在 ufw 上完全打开了端口 80 和 443 以及 nginx。 What am I missing here?我在这里缺少什么? Attached my sites-available below, and I have a working symlink to sites-enabled.下面附上我的网站,我有一个指向启用网站的有效符号链接。

server {
    listen 80;
    listen [::]:80;
    server_name www.example.com;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.example.com;
    
    ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/www.example.com/chain.pem;

    include /etc/letsencrypt/options-ssl-nginx.conf;

    location / {
        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;
        proxy_pass http://localhost:3000;
    }
}

The virtual server I was running this on was a Google Cloud Server provisioned by my employer, I ended up guessing that port 443 was blocked by a GCS firewall, and that was correct.我运行它的虚拟服务器是我雇主提供的谷歌云服务器,我最终猜测端口 443 被 GCS 防火墙阻止,这是正确的。 Here are instructions for adjusting that firewall: https://cloud.google.com/vpc/docs/using-firewalls以下是调整防火墙的说明: https : //cloud.google.com/vpc/docs/using-firewalls

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM