linux Nginx反向代理的目录访问问题

前端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/的时候是可以访问的,如下

  1. [[email protected]_node ~]# curl -I http://www.proxy.com/test/
  2. HTTP/1.1 200 OK
  3. Server: nginx
  4. Date: Wed, 24 Apr 2013 04:22:40 GMT
  5. Content-Type: text/html; charset=utf-8
  6. Content-Length: 28
  7. Connection: keep-alive
  8. Last-Modified: Wed, 24 Apr 2013 03:09:13 GMT
  9. Accept-Ranges: bytes

 

但是如果访问www.proxy.com/test的话就会301

  1. [[email protected]_node ~]# curl -I http://www.proxy.com/test
  2. HTTP/1.1 301 Moved Permanently
  3. Server: nginx
  4. Date: Wed, 24 Apr 2013 04:25:01 GMT
  5. Content-Type: text/html
  6. Content-Length: 178
  7. Location: http://www.proxy.com/test/
  8. Connection: keep-alive

 

我刚开始以为是我前端的proxy_redirect设置有问题,后来修改proxy_redirect多次,均无法达到要求,最后突发奇想,把前端的nginx设成了这样

  1. server
  2.     {
  3.             listen  80;
  4.             server_name  www.proxy.com;
  5.             index index.php index.html index.htm;
  6.             location /test {
  7.                 proxy_next_upstream http_502 http_504 error timeout invalid_header;
  8.                 proxy_pass http://192.168.0.37/test/;
  9.                 proxy_set_header Host 192.168.0.37;
  10.                 proxy_set_header X-Forwarded-For $remote_addr;
  11.                 #proxy_redirect http://192.168.0.37/test/ /test/;
  12.             }
  13.             location / {
  14.                 proxy_next_upstream http_502 http_504 error timeout invalid_header;
  15.                 proxy_pass http://192.168.0.37/;
  16.                 proxy_set_header Host 192.168.0.37;
  17.                 proxy_set_header X-Forwarded-For $remote_addr;
  18.             }
  19.              access_log /data/logs/weblog/proxy_server.access.log;
  20. }

 

这样的话,访问www.proxy.com/test就没问题了

  1. [[email protected]_node vhosts]# curl www.proxy.com/test
  2. 192.168.0.37 proxy test OK!

Tagged: ,

Comments are closed.