我已经设置了一个 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
一旦我设置了重定向规则,就会发生重定向过多的情况。
有没有办法可以同时实现反向代理和 301 重定向?