我试了下,location /{}规则时彷佛不太正常,会致使location为空。这个有待详细考证 css
------------------------------------------------- html
from: nginx 中文网 前端
Nginx的代理功能太完善了,咱们看看proxy_redirect参数的做用。 nginx
案例说明:
要作一个html.aslibra.com的域名处理不少网站的html内容,固然是后端的服务器了,目录分析
html.zcom.com/img.aslibra.com/
html.zcom.com/css.aslibra.com/
访问的域名是该目录下的域名,那前端nginx的配置应该相似这样: 后端
server {
server_name img.aslibra.com;
location / {
rewrite ^(.*) /$http_host$1 break;
proxy_set_header Host html.aslibra.com;
proxy_pass http://cache-89;
}
} 服务器
但这样访问目录时若是没有以“/”结尾,则服务器会返回301redirect: frontend
[root@aslibra ~]# curl -I http://img.aslibra.com/www
HTTP/1.1 301 Moved Permanently
Server: nginx/0.7.59
Date: Tue, 21 Jul 2009 15:28:58 GMT
Connection: keep-alive
Location: http://html.aslibra.com/img.aslibra.com/www/ curl
html.aslibra.com这个域名并不是公布的域名,返回给客户端是会天然产生错误的
Nginx能够很好的处理这个问题: 网站
server {
server_name img.aslibra.com;
location / {
rewrite ^(.*) /$http_host$1 break;
proxy_set_header Host html.aslibra.com;
proxy_pass http://cache-89;
proxy_redirect http://html.aslibra.com/img.aslibra.com/ /;
}
} url
加一行proxy_redirect后,正常了:
[root@aslibra ~]# curl -I http://img.aslibra.com/www
HTTP/1.1 301 Moved Permanently
Server: nginx/0.7.59
Date: Tue, 21 Jul 2009 15:23:49 GMT
Content-Type: text/html
Location: http://img.aslibra.com/www/
Connection: keep-alive
Content-Length: 185
Expires: Tue, 21 Jul 2009 16:23:49 GMT
Cache-Control: max-age=3600
就这么样就ok啦~
不过貌似不支持变量出如今地址里,这个就郁闷了,必须指定相应域名。
对于多个域名匹配的server,redirect设置不能写做’/'了,不然会用第一个域名做为redirect域名
能够写几个匹配规则:
proxy_redirect http://html.aslibra.com/img.aslibra.com/ http://img.aslibra.com/;
proxy_redirect http://html.aslibra.com/css.aslibra.com/ http://css.aslibra.com/;
==================
NGINX的proxy_redirect功能比较强大,其做用是对发送给客户端的URL进行修改。以例子说明:
server {
listen 80;
server_name test.abc.com;
location / {
proxy_pass http://10.10.10.1:9080;
}
}这段配置通常状况下都正常,但偶尔会出错, 错误在什么地方呢? 抓包发现服务器给客户端的跳转指令里加了端口号,如 Location: http://test.abc.com:9080/abc.html 。由于nginx服务器侦听的是80端口,因此这样的URL给了客户端,必然会出错.针对这种状况, 加一条proxy_redirect指令: proxy_redirect http://test.abc.com:9080/ / ,把全部“http://test.abc.com:9080/”的内容替换成“/”再发给客户端,就解决了。
server {
listen 80;
server_name test.abc.com;
proxy_redirect http://test.abc.com:9080/ /;
location / {
proxy_pass http://10.10.10.1:9080;
}
}
http://nginx.179401.cn/
圣地啊 加红 加粗~!!
出处:http://nginx.179401.cn/StandardHTTPModules/HTTPProxy.html
proxy_redirect
语法:proxy_redirect [ default|off|redirect replacement ]
默认值:proxy_redirect default
使用字段:http, server, location
若是须要修改从被代理服务器传来的应答头中的"Location"和"Refresh"字段,能够用这个指令设置。
假设被代理服务器返回Location字段为: http://localhost:8000/two/some/uri/
这个指令:
proxy_redirect http://localhost:8000/two/ http://frontend/one/;
将Location字段重写为http://frontend/one/some/uri/。
在代替的字段中能够不写服务器名:
proxy_redirect http://localhost:8000/two/ /;
这样就使用服务器的基本名称和端口,即便它来自非80端口。
若是使用“default”参数,将根据location和proxy_pass参数的设置来决定。
例以下列两个配置等效:
location /one/ { proxy_pass http://upstream:port/two/; proxy_redirect default;} location /one/ { proxy_pass http://upstream:port/two/; proxy_redirect http://upstream:port/two/ /one/;}
在指令中可使用一些变量:
proxy_redirect http://localhost:8000/ http://$host:$server_port/;
这个指令有时能够重复:
proxy_redirect default; proxy_redirect http://localhost:8000/ /; proxy_redirect http://www.example.com/ /;
参数off将在这个字段中禁止全部的proxy_redirect指令:
proxy_redirect off; proxy_redirect default; proxy_redirect http://localhost:8000/ /; proxy_redirect http://www.example.com/ /;
利用这个指令能够为被代理服务器发出的相对重定向增长主机名: