Nginx每个位置/传递重写uri到proxy_pass

时间:2012-12-09 19:45:10

标签: url nginx rewrite proxypass

如果您希望通过基于位置的代理服务器将URL请求代理到两个不同的后端,那么这是最快,最干净的解决方案。

location /app1/ {
    alias /var/www/ruby/public;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    try_files $uri $uri/ @ruby;
}

location @ruby {
    proxy_pass http://127.0.0.1:3000;
}

location /app2/ {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    try_files $uri $uri/ @other;
}

location @other {
    proxy_pass http://127.0.0.1:8080;
}

使用此配置nginx传递“/ app1”或“/ app2”代理和后端无法识别url /命令..

例如,在访问http://127.0.0.1:3000时,我希望仅传递给/messages http://<nginx>/app1/messages - 但在上面的配置中,也会将/app1/作为http://127.0.0.1:3000/app1/messages传递。 /app2

也是如此

1 个答案:

答案 0 :(得分:11)

尝试将“/”放在上游名称的末尾 e.g

proxy_pass http://127.0.0.1:8080/;

请看这篇文章: How to preserve request url with nginx proxy_pass