【问题标题】:Nginx - Redirect HTTP to HTTPSNginx - 将 HTTP 重定向到 HTTPS
【发布时间】:2020-09-27 21:39:50
【问题描述】:

我遵循了一些关于在 Lightsail 上设置 .Net Core 服务器以及如何为 Nginx 设置 SSL 的教程,得到以下“默认”配置文件:

server {
    listen 80 http2 default_server;
    root /var/www/mywebsite/wwwroot;
    server_name mywebsite.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    root /var/www/mywebsite/wwwroot;
    ssl_certificate /etc/letsencrypt/live/www.mywebsite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.mywebsite.com/privkey.pem;
    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

我目前能够连接到https://mywebsite.com 和https://www.mywebsite.com,但是当我尝试转到http 版本时,要么什么都找不到,要么在chrome 上下载了一个带有一些奇怪字符的文件。对此的任何帮助将不胜感激。

【问题讨论】:

    标签: .net http nginx networking


    【解决方案1】:
    Try this:
    
        
    server {
        listen 80;
        listen [::]:80;
        server_name example.com www.example.com; 
        return 301 https://example.com$request_uri;
    }
    
    server {
        listen 443 ssl;
        listen [::]:443 ssl;
        server_name www.example.com; 
        ssl on;
    }
    

    【讨论】:

    • 虽然此代码可能会解决问题,但 including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的答案添加解释并说明适用的限制和假设。
    【解决方案2】:

    该错误似乎是由仅支持带有 SSL 的 HTTP/2 的浏览器引起的。在我的情况下这不是问题,因为我正在重定向到安全站点,因此将第一个服务器块的第 1 行从

    listen 80 http2;
    

    到

    listen 80;
    

    解决了我的问题,现在重定向工作正常。

    【讨论】:

      猜你喜欢
      • 2011-03-29
      • 2018-01-05
      • 2014-10-11
      • 2018-08-18
      • 2017-02-25
      • 1970-01-01
      • 2014-05-08
      • 2015-08-26
      • 1970-01-01
      相关资源
      最近更新 更多