nginx

时间:2015-11-27 11:52:41

标签: nginx reverse-proxy proxypass

我是一名前端开发人员,上次尝试过我的nginx配置,但是工作正常。以下是配置:

server {
    listen       80;
    server_name  localhost;
    access_log  /var/log/nginx/localhost.access.log;
    location / {
             #By default route to node.js running on localhost:9000 port
             proxy_pass http://localhost:9000;
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection 'upgrade';
             proxy_set_header Host $host;
             proxy_cache_bypass $http_upgrade;
    }
    #currently only one server but will have to redirect to n hosts based on a parameter
    location /hosts.json {
      proxy_pass      http://app-host.net:3000;
    }
    #currently only one server but will have to redirect to n hosts based on a parameter
    location /hosts/ {
      proxy_pass      http://app-host.net:3000;
    }
}

现在,我需要根据参数重定向到4个不同的服务器。即如果城市是Bangalore,我需要重定向到bangalore.corp.net:3000,如果城市是NewYork,那么我需要重定向到newyork.corp.net:3000,依此类推。

我有点期待:

location /app1/hosts/ {
      proxy_pass      http://app1-host.net:3000;
    }
#But the proxy pass should point to http://app1-host.net:3000/hosts and not http://app1-host.net:3000/app1/hosts

我们如何在nginx配置文件中处理此类代理传递。请告诉我。

1 个答案:

答案 0 :(得分:0)

您有/app1/hosts/foo表格的网址,该网址应映射到http://app1-host.net:3000/hosts/foo。可以通过在proxy_pass指令中附加一个URI来实现,该指令的作用类似于alias

location /app1/hosts/ {
  proxy_pass http://app1-host.net:3000/hosts/;
}

有关详细信息,请参阅this document