-->

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

2020-10-28 20:11发布

 

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

[csharp] view plaincopyprint?
 
  1. [root@control_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

[csharp] view plaincopyprint?
 
  1. [root@control_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设成了这样

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

 

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

[csharp] view plaincopyprint?
 
  1. [root@control_node vhosts]# curl www.proxy.com/test  
  2. 192.168.0.37 proxy test OK!  

 

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

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

[csharp] view plaincopyprint?
 
  1. [root@control_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

[csharp] view plaincopyprint?
 
  1. [root@control_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设成了这样

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

 

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

[csharp] view plaincopyprint?
 
  1. [root@control_node vhosts]# curl www.proxy.com/test  
  2. 192.168.0.37 proxy test OK!  

 

标签: