反向代理:192.168.1.187 (开始的试验中没有189的存在,反向和web都在187上做)
web:192.168.1.189
nginx地址重写后,页面URL改变实例:
全都在187上面测试
访问 http://test.com/test.html 内容是test1.com的root路径下的内容,但是页面url也跳转成http://test1.com/test.html
server {
server_name test.com;
location / {
root html;
}
location ~ /test.html {
rewrite (.*) http://test1.com$1;
}
}
server {
server_name test1.com;
location / {
root html/test;
}
}
}
nginx地址重写后,页面URL不改变实例:
访问 http://test.com/test.html 内容是test1.com的root路径下的内容,但是页面url不跳转
两种写法:
一:if判断写法
server {
server_name test.com;
location / {
root html;
if ($request_uri ~ /test.html) {
proxy_pass http://test1.com;
}
}
}
server {
server_name test1.com;
location / {
root html/test;
}
}
}
二:location写法
server {
server_name test.com;
location / {
root html;
}
location /test.html {
proxy_set_header Host test1.com;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://test1.com;
}
}
server {
server_name test1.com;
location / {
root html/test;
}
}
}
访问 http://test.com/test目录下的内容,实际访问是test1.com的root路径下的内容,但是页面url不跳转
两种写法:
一:if判断写法
server {
server_name test.com;
location / {
root html;
if ($request_uri ~ ^/test/(.*)$) {
proxy_pass http://test1.com;
}
}
}
server {
server_name test1.com;
location / {
root html/test1;
}
}
}
二:location写法
server {
server_name test.com;
location / {
root html;
}
location /test/ {
proxy_set_header Host test1.com;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://test1.com;
}
}
server {
server_name test1.com;
location / {
root html/test1/;
}
}
}
两台机器测试,访问 http://test.com/test.html(192.168.1.187) 内容是test1.com(192.168.1.189)的root路径下的内容,但是页面url不跳转
访问目录同理。
192.168.1.187配置:
upstream test1 {
server 192.168.1.189;
}
server {
server_name test.com;
location / {
root html;
}
location /test.html {
proxy_set_header Host test1.com; #这里原本是proxy_set_header Host $host; 但是这里$host
是test.com,传到189上的时候还是test.com,而189上并没有
test.com域名,并且我们要proxy_pass过去访问的是
test1.com。所以这里手动将$host写成test1.com
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://test1;
}
}
}
192.168.1.189配置:
server {
listen 80;
server_name test1.com;
access_log off;
root html;
location ~ /test.html {
root html;
}
}
}
rewrite加proxy_pass:访问http://a.com/test/a/123 转向http://b.com/test/index.html?a=show123
反向代理机器:
server {
listen 80;
server_name a.com;
location / {
root html;
rewrite ^(.*)/a/(.*)$ $1/index.html?a=show$2 break;
proxy_pass http://b.com;
}
}
web:
server {
listen 80;
server_name b.com;
}
访问:http://a.com/test/a/123
web上nginx的日志:
192.168.6.33 - - [29/Jul/2012:12:53:44 +0800] "GET /test/index.html?a=show123 HTTP/1.0" 200 19 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。