【问题标题】:Linux/Nginx too many redirectsLinux/Nginx 重定向过多
【发布时间】:2021-11-14 06:13:31
【问题描述】:

我尝试了许多 nginx 配置,直到我找到了可行的方法。我不确定我的服务器上是否存储了我所有更改的某种记录,因为当我打开我的 web 应用程序时,我收到一个错误“重定向太多”。它具有与以前相同的重定向数量,只是配置不同。谁能提供任何见解?

server {

    listen 80;
    server_name cigars-rec.com;

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

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

}

upstream cr2-cigar-recommender {
    server  cr2-cigar-recommender:8501;
}

server {
    listen 443 ssl;
    server_name cigars-rec.com;

    ssl_certificate /etc/letsencrypt/live/cigars-rec.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/cigars-rec.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # streamlit config
    location / {
        proxy_pass http://cigars-rec.com;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;
    }
    
    location /page1 {
        default_type "text/html";
        alias /templates/page1.html;
    }

    location /page2 {
        default_type "text/html";
        alias /templates/page2.html;
    }

    location /page3 {
        default_type "text/html";
        alias /templates/page3.html;
    }

}

【问题讨论】:

  • 请将您的 nginx 配置发布为 markdown 格式的文本块。
  • @IvanShatsky 已更新

标签: linux nginx virtual-machine


【解决方案1】:

在您的配置文件中,您的 http 服务器块中有此指令用于端口 80:

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

这似乎是故意的 http 到 https 重定向,这很可能正是您想要的。

但是你的 https 服务器块有一个 proxy_pass 告诉 nginx 建立一个到 http://cigars-rec.com/ 的 http 连接并将响应返回给客户端。该 http URL 将由发出重定向的 http 服务器块提供服务。

这意味着任何与该位置匹配的对您网站的 http 或 https 版本的请求都将始终返回重定向并导致您描述的情况。

【讨论】:

  • 我不完全理解您的回复 - 这里是技术新手。我具体需要改变什么?
  • 不确定我是否正确标记了您? TO新手。
  • 您在您的服务器块上为端口 80 发出重定向。您的 https 服务器块只是代理到您的 http 服务器块,它发出重定向。您可能希望将您的 https 服务器块配置为不 proxy_pass 到网站的 http 版本。
猜你喜欢
  • 2016-05-10
  • 2018-10-07
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多