【问题标题】:Nginx not redirecting properlyNginx 没有正确重定向
【发布时间】:2021-12-06 11:28:57
【问题描述】:

我有点困惑,我真的以为这将是一个简单的 5 分钟任务,但没有。我按照文档进行了重定向,但无法使其正常工作,它只是将新 url 的倍数(不确定有多少,但我从 url 栏的边缘算出至少 10 个)附加到原始一。这是我的服务器块配置:

server {
        listen 80;
        server_name old.i.p.address;
        return 301 new.i.p.address/redirect_page/;

        location ~ /si/home/0/ {
                 return 301 /si/home/;
                 }

        location = /favicon.ico { access_log off; log_not_found off; }
        location /static {
                      alias /home/rob/pcc_django/staticfiles/;
                      }

        location / {
                 proxy_set_header Host $http_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;
                 proxy_pass http://unix:/run/gunicorn.sock;
        }
    }

抱歉,如果缩进错误,它在我的机器上的格式正确,我还没有弄清楚如何在本网站的代码块中保持正确的格式。

我已经尝试了多个版本,在新 IP 地址的开头有和没有“http://”,我也试过有和没有“/homepage”的版本。我也尝试过使用“重写”,因为最初我只是想要一个永久重定向,但这是唯一允许 ngingx 服务器启动的迭代,它在 url 栏中给出了奇怪的结果:

old.i.p.address/homepage/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/...etc ad infinitum

请问我该如何解决这个问题?

【问题讨论】:

  • old.i.p.addressnew.i.p.address 只是 IPv4 地址还是域名?
  • 没错,它们只是 IPv4 地址,这是一个内部工作网站。
  • new.i.p.address/redirect_page/ 字符串被浏览器视为同一服务器上的 relative URI,从而为您提供无限循环,每次迭代都会连接 new.i.p.address/redirect_page/。例如,如果您有一个 return 301 /new.i.p.address/redirect_page/; 规则,那么该字符串将被视为绝对 URI,在每次迭代中为您提供相同的地址。唯一正确的方法是使用http://(或https://)前缀。正如@emptyhua 所说,301 永久重定向由浏览器缓存,因此您应该清除缓存或从隐身窗口尝试。
  • @IvanShatsky 非常感谢,已经解决了,如果您可以将其更改为答案而不是评论,我愿意接受您的回答。

标签: linux nginx centos7


【解决方案1】:

301重定向会缓存在浏览器中,你可以clear the browser cache,然后再试试return 301 http://new.i.p.address/redirect_page/;

【讨论】:

    【解决方案2】:

    new.i.p.address/redirect_page/ 字符串被浏览器视为同一服务器上的相对 URI,从而为您提供无限循环,每次迭代都将new.i.p.address/redirect_page/ 部分连接起来。例如,如果您有一个 return 301 /new.i.p.address/redirect_page/; 规则,那么该字符串将被视为绝对 URI,在每次迭代中为您提供相同的地址。唯一正确的方法是使用http://(或https://)前缀。正如@emptyhua 所说,HTTP 301 永久重定向由浏览器缓存,因此您应该清除缓存或从隐身窗口尝试。

    【讨论】:

      猜你喜欢
      • 2016-04-27
      • 1970-01-01
      • 2020-02-10
      • 2018-04-14
      • 2020-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多