nginx反向代理文件夹

我试图用反向代理访问另一台服务器。 “主”服务器在mydomain.com后面,我想通过使用mydomain.com/test访问第二个。 目前只是mydomain.com/test的作品。

但是,如果我尝试访问mydomain.com/test/myfiles,我被redirect到mydomain.com/myfiles,因为这个URL的目标是“主”服务器,所以不存在404找不到。 我尝试了多个与proxy_redirect的东西,重写但没有任何工作。

server { listen 80; index index.php index.html index.htm; root /path/to/content; server_name localhost mydomain.com; location / { try_files $uri $uri/ =404; #/index.html; } location /test/ { proxy_pass http://192.168.1.202/; proxy_set_header Host $host; } location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } } 

curl -I "http://192.168.1.202" -H "Host: mydomain.com"在主服务器上给出:

 HTTP/1.1 301 Moved Permanently Server: nginx/1.2.1 Date: Sun, 02 Nov 2014 15:02:56 GMT Content-Type: text/html Content-Length: 184 Location: example.com/myfiles Connection: keep-alive 

问题是,当您使用带有proxy_pass指令的尾部斜线时, proxy_redirect的默认行为如下所示:

 location /test/ { proxy_pass http://192.168.1.202/; proxy_set_header Host $host; } 

是相同的 :

 location /test/ { proxy_pass http://192.168.1.202/; proxy_redirect http://192.168.1.202/ /test/; proxy_set_header Host $host; } 

所以给定curl输出,你必须设置:

 location /test/ { proxy_pass http://192.168.1.202/; proxy_redirect http://$host/ /test/; proxy_set_header Host $host; }