【问题标题】:Permanent redirect while reverse proxying with nginx使用 nginx 进行反向代理时永久重定向
【发布时间】:2020-07-20 03:14:40
【问题描述】:

我已经设置了一个 nginx 反向代理服务器,它将 blog.xxx.com 代理到 xxx.com/blog。这是我的配置文件。

server {
    listen 80;
    root /var/www/html;
    server_name xxx.com www.xxx.com;
    
    location /.well-known/acme-challenge {
        root /tmp/letsencrypt/www;
    }
    location / {
        return 301 https://$host$request_uri;
    }
}
server {

    listen 443;
    
    server_name xxx.com;
    
    root /var/www/html;

    include /etc/nginx/snippet/ssl.conf;
    
    location /blog/ {
        proxy_pass https://blog.xxx.com;
        proxy_set_header Host blog.xxx.com;
        rewrite /blog/(.*) /$1 break;
        proxy_redirect off;
        expires -1;
        add_header Cache-Control no-store;
        proxy_read_timeout 90;
        proxy_connect_timeout 90;
        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_set_header X-NginX-Proxy true;
        proxy_set_header Connection "";
    }

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://xxx:8090;
    }
    
}

它工作得很好。但我还需要将blog.xxx.com 永久重定向到xxx.com/blog。一旦我设置了重定向规则,就会出现too many redirects 的情况。

有没有办法同时进行反向代理和301重定向?

【问题讨论】:

  • blog.xxx.com 是如何托管的,您对它有多少控制权?
  • @RichardSmith 它托管在 siteground 上,可以完全控制其设置...

标签: nginx reverse-proxy


【解决方案1】:

我是否正确理解提到的两个网站托管在不同的地方?如果是这样,我会在第一页上执行 303 重定向到您的第二页。如果第一页没有 Nginx,你大概可以在你的博客软件中(或者直接在 HTML、PHP 等中)这样做。为了防止无休止的重定向,你可以在发送给客户端的同时重写它们:

https://serverfault.com/a/986034/304842

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多