server { listen 80; server_name 原域名; location / { #return 301 http://新域名$request_uri; # 重定向到新域名 proxy_pass http://新域名/; proxy_redirect http://原域名/ http://新域名/; } } server { listen 80; server_name 新域名; # websocket代理 #location /webSocket/chat { # proxy_pass http://xxx.xxx.com/webSocket/chat; # proxy_set_header Host $host; # proxy_set_header Upgrade 'websocket'; # proxy_set_header Connection 'Upgrade'; # proxy_connect_timeout 4s; # proxy_read_timeout 7200s; #两个小时 # proxy_send_timeout 12s; #} location / { proxy_pass http://localhost:8080; root html; index index.html index.htm; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
注意:用return,可以重定向到新域名的页面,但是请求方法是有问题的
server { listen 80; server_name www.old.com; location / { proxy_pass http://www.new.com; proxy_redirect off; } }
server { listen 80; server_name www.old.com; location / { proxy_set_header Host www.new.com; return 307 http://www.new.com$request_uri; # 重定向到新域名 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_redirect http://www.new.com http://www.yyhkjt.com; #rewrite ^(.*)$ http://www.new.com$1 permanent; #rewrite ^(.+)$ $scheme://www.new.com$1 redirect; #return 301 $scheme://www.new.com$request_uri; #proxy_pass http://www.new.com; } }
用postman请求会将post请求发送两次,并且第二次会转为get请求
301:客户端在收到 301 响应后,会自动将 HTTP 请求转为 GET 请求,同时将请求地址修改为重定向后的地址
307:对请求类型不做转换
参考链接:https://huaweicloud.csdn.net/63561b80d3efff3090b5a828.html