语法:
proxy_redirect redirect replacement;
proxy_redirect default;
proxy_redirect off;
默认:proxy_redirect default;
配置块(使用的字段):http、server、location
作用:当上游服务器返回的响应是重定向或刷新请求(如HTTP响应码是301或者302)时,proxy_redirect可以重设HTTP头部的location或refresh字段。如果需要修改从被代理服务器传来的应答头中的"Location"和"Refresh" 字段,可以用这个指令设置。
关于重定向
• 通过重定向,浏览器知道页面位置发生变化,从而改变地址栏显示的地址。
• 通过重定向,搜索引擎意识到页面被移动了,从而更新搜索引擎索引,将原来失效的链接从搜索结果中移除。
• 临时重定向(R=302)和永久重定向(R=301)都是亲搜索引擎的,是SEO的重要技术。
• Redirect是浏览器和服务器发生两次请求,也就是服务器命令客户端“去访问某个页面”;
• redirect的URL需要传送到客户端。
• redirect是从一个地址跳转到另一个地址。
一、不启动重定向
server {
listen 80;
server_name www.boke.com;
location / {
proxy_pass http://192.168.1.154:8080;
proxy_redirect off;
}
}
此时我们通过curl查看结果得出
# curl -I http://www.boke.com/wuman
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 24 Dec 2015 12:02:00 GMT
Content-Type: text/html; charset=iso-8859-1
Connection: keep-alive
Location: http://192.168.1.154:8080/wuman/ # Location 中带有实际地址端口是不允许的
二、开启重定向
server {
listen 80;
server_name www.boke.com;
location /one {
proxy_pass http://www.boke.com:8080/two;
proxy_redirect http://www.boke.com:8080/two/ /one; # 重定向某个路径,可以省略主机地址
proxy_redirect ~^http://www.boke.com:8080(.*) http://www.boke.com$1; # 正则匹配重定向某个地址
proxy_redirect http://$host:$server_port/ http://$host/; # 支持host变量重定向
}
# curl -I http://www.boke.com/one
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 24 Dec 2015 12:08:34 GMT
Content-Type: text/html; charset=iso-8859-1
Connection: keep-alive
Location: http://www.boke.com/two/ # Location 地址已经被重定向