NGinX多个子uris到后端根

时间:2016-03-09 19:40:55

标签: nginx proxy location uri reverse

我有一个我无法解决的问题,我希望我能得到一些提示来完成这项工作。我有多个后端,我想允许通过反向代理访问。我可以使用NGinX中的多个域轻松完成此操作:

server {
   listen  80;
   server_name     repo.test.local;

   location / {
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_pass http://172.16.1.32;
                   break;
          }
   }
   server {
   listen  80;
   server_name     maprwebui.test.local;

   location / {
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_pass https://172.16.1.104:8443;
                   break;
          }
   }
   server {
   listen  80;
   server_name     spark.test.local;

   location / {
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_pass http://172.16.1.102:8080;
                   break;
          }
   }
   server {
   listen  80;
   server_name     yarnhistory.test.local;

   location / {
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_pass http://172.16.1.202:19888;
                   break;
      }
   }

但是我希望能够像这样使用子URI这样做:

server {
    listen  80;     
    server_name     proxy.test.local;

   location /repo {
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_pass http://172.16.1.32;
                    }
   location /maprui {
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_pass https://172.16.1.104:8443;
                    }
   location /spark {
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_pass http://172.16.1.102:8080;
                  }
   location /yarn {
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_pass http://172.16.1.202:19888;
                  }
   }

我已经尝试了所有我能想到的设置,但是我最终在这些URI下使用了404s的URI,或者NginX应用程序认为不是试图点击类似http://proxy.test.com/yarn/jobhistory的东西,而是重新路由到http://proxy.test.com/jobhistory

我尝试过重写规则,看看它们是否有效,但它们都没有,或者我做错了。我已经测试了以下内容,无法修复:

    rewrite ^/yarn/(.*) /$1 break;
    proxy_pass http://172.16.1.202:19888;

有没有办法让这个工作起来,而不必触及您想要公开的后端服务,或者不使用多个域来完成我的工作?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您的解决方案可能非常简单。 Nginx查看您作为主机传递给proxy_pass的内容,而不是URI,或者尝试这样:

server {
    listen  80;     
    server_name     proxy.test.local;

   location /repo {
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_pass http://172.16.1.32/;
                    }
   location /maprui {
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_pass https://172.16.1.104:8443/;
                    }
   location /spark {
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_pass http://172.16.1.102:8080/;
                  }
   location /yarn {
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_pass http://172.16.1.202:19888/;
                  }
   }

请注意端口后的/。它应该就是这么简单,你要做的当然是一个足够普遍的用例。如果这不起作用,请告诉我,我们可以尝试别的。

有关更多讨论,请参阅this question