nginx proxy_pass配置

时间:2013-01-17 11:05:12

标签: nginx virtualhost proxypass

我需要将几个网址代理到不同的主机。实际上,我正在使用具有不同端口的相同主机来测试我的nginx配置。这是我的虚拟主机定义:

server {
    listen       8081;
    server_name  domain.com;

    location /Plasmid/ {
        proxy_pass   http://localhost:8000/Plasmid/;
    }


    location /_community/ {
         proxy_pass   http://localhost:8082/comments_api/ ;
    }

    location / {
        # rewrite cq_user_authenticated===(.*)/(.*)/iuuid=(.*)/commenti.html$  /Plasmid/comments/legacy/$3/$1 ;
        # rewrite querystring===(.*)$  /Plasmid/comments/legacy/$1 ;
        # rewrite cq_user_authenticated===([^&]*)&/.*uuid=([^/]*) /comments_api/legacy/$2 ;
        # rewrite userdetails(.*)  /Plasmid/comments/user_details ;
        root   html;
        index  index.html index.htm;
    }

}

我的主机文件当然有domain.com的映射

当我拨打网址时:http://domain.com:8081/Plasmid/default/page/12我收到了一个http 404

如果我从配置中删除第二个位置:

location /_community/ {
    proxy_pass   http://localhost:8082/comments_api/ ;
}

我获得了我想要的资源,但由于托管在不同的平台上,因此有些部分被遗漏了:

[error] 1033#0: *1 open() "/usr/local/Cellar/nginx/1.2.6/html/_community/content

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

做一点改变:

location ^~ /Plasmid/ {
   proxy_pass    http://localhost:8000/Plasmid/;
}

location ^~ /_comunity/ {
   proxy_pass    http://localhost:8082/comments_api/;

为什么?因为^~表示开头,当您请求页面时

http://domain.com:8081/Plasmid/default/page/12

符合该规则。在您的配置中,您没有使用任何标记,如下所示:

location /anylocation

看起来你的nginx更喜欢规则

location / {

大于

location /Plasmid

location /_comunity

因为它正在使用root指令并在html文件夹中搜索_community / content(因为您收到错误消息)。

换句话说,^~的优先级高于no mark。还有一点可以帮助在每个break指令后添加proxy_pass指令;

相关问题