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

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

从昨天就开始纠结了,在做实验的时候,遇到目录访问的问题,如下

 

前端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 OKServer: nginxDate: Wed, 24 Apr 2013 04:22:40 GMTContent-Type: text/html; charset=utf-8Content-Length: 28Connection: keep-aliveLast-Modified: Wed, 24 Apr 2013 03:09:13 GMTAccept-Ranges: bytes
 
 

 

 

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

 
  
[root@control_node ~]# curl -I http://www.proxy.com/testHTTP/1.1 301 Moved PermanentlyServer: nginxDate: Wed, 24 Apr 2013 04:25:01 GMTContent-Type: text/htmlContent-Length: 178Location: 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/test192.168.0.37 proxy test OK!
 
 

 

 

 

 

 


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

你可能感兴趣的:(Nginx反向代理的目录访问问题)