简体   繁体   中英

nginx proxy_pass configuration

I need to proxy a couple of urls to different hosts. Actually, I'm using the same host with different port to test my nginx configuration. This is my virtual host definition:

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;
    }

}

Of course my hosts file has mapping for the domain.com

When I call the url: http://domain.com:8081/Plasmid/default/page/12 I get an http 404

If I remove the second location from my configuration:

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

I get the resource I want, but some part are missed since the are hosted on a different platform:

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

How can I resolve this issue?

Do a little change:

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

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

Why is that? Because ^~ means starts with and when you request for page:

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

it fit to that rule. In your configuration you are using no mark and something like this:

location /anylocation

and it looks like your nginx prefer rule

location / {

than

location /Plasmid

and

location /_comunity

because it's using root directive and searching for _community/content in html folder (as you get in error message).

In other words ^~ has greater priority than no mark . One thing that could also help is to add break directive after each proxy_pass directive;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM