从昨天就开始纠结了,在做实验的时候,遇到目录访问的问题,如下
前端nginx vhost的设置如下,代理访问后端的192.168.0.37
server { listen 80; server_name www.proxy.com; index index.php index.html index.htm; location /test/ { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_pass http://192.168.0.37; proxy_set_header Host 192.168.0.37; proxy_set_header X-Forwarded-For $remote_addr; proxy_redirect http://192.168.0.37/test/ /test/; } access_log /data/logs/weblog/proxy_server.access.log; }
后端的192.168.0.37在根目录下是有test目录的,该目录下有个index文件,内容为“192.168.0.37 proxy test OK!”
现在的问题是如果在访问www.proxy.com/test/的时候是可以访问的,如下
[root@control_node ~]# curl -I http://www.proxy.com/test/ HTTP/1.1 200 OK Server: nginx Date: Wed, 24 Apr 2013 04:22:40 GMT Content-Type: text/html; charset=utf-8 Content-Length: 28 Connection: keep-alive Last-Modified: Wed, 24 Apr 2013 03:09:13 GMT Accept-Ranges: bytes
但是如果访问www.proxy.com/test的话就会301
[root@control_node ~]# curl -I http://www.proxy.com/test HTTP/1.1 301 Moved Permanently Server: nginx Date: Wed, 24 Apr 2013 04:25:01 GMT Content-Type: text/html Content-Length: 178 Location: http://www.proxy.com/test/ Connection: keep-alive
我刚开始以为是我前端的proxy_redirect设置有问题,后来修改proxy_redirect多次,均无法达到要求,最后突发奇想,把前端的nginx设成了这样
server { listen 80; server_name www.proxy.com; index index.php index.html index.htm; location /test { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_pass http://192.168.0.37/test/; proxy_set_header Host 192.168.0.37; proxy_set_header X-Forwarded-For $remote_addr; #proxy_redirect http://192.168.0.37/test/ /test/; } location / { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_pass http://192.168.0.37/; proxy_set_header Host 192.168.0.37; proxy_set_header X-Forwarded-For $remote_addr; } access_log /data/logs/weblog/proxy_server.access.log; }
这样的话,访问www.proxy.com/test就没问题了
[root@control_node vhosts]# curl www.proxy.com/test 192.168.0.37 proxy test OK!