【问题标题】:nginx proxy_pass configurationnginx proxy_pass 配置
【发布时间】:2012-12-31 21:49:21
【问题描述】:

我需要将几个 url 代理到不同的主机。实际上,我使用具有不同端口的同一主机来测试我的 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;
    }

}

当然,我的 hosts 文件有 domain.com 的映射

当我调用 url: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

我该如何解决这个问题?

【问题讨论】:

    标签: nginx virtualhost proxypass


    【解决方案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。也可以帮助的一件事是在每个 proxy_pass 指令之后添加 break 指令;

    【讨论】:

      猜你喜欢
      • 2012-10-02
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多