nginx 'proxy_pass' cannot have URI part in

2019-01-22 12:47发布

I have a location block as

location @test{
    proxy_pass http://localhost:5000/1;
}

but nginx complains that "proxy_pass cannot have URI part in location given by regular expression..." Does anyone know what might be wrong?

I'm trying to query localhost:5000/1 when an upload is complete:

location /upload_attachment {
    upload_pass @test;
    upload_store /tmp;
    ...
}

标签: nginx
3条回答
该账号已被封号
2楼-- · 2019-01-22 12:53

Try omitting the "/" (URI part) and check.

location @test{
    proxy_pass http://localhost:5000;
}
查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-22 13:09

Technically just adding the URI should work, because it's documented here and it says that it should work, so

location @test{
    proxy_pass http://localhost:5000/1/; # with a trailing slash
}

Should have worked fine, but since you said it didn't I suggested the other way around, the trick is that instead of passing /my/uri to localhost:5000/1, we pass /1/my/uri to localhost:5000,

That's what my rewrite did

rewrite ^ /1$1

Meaning rewrite the whole URL, prepend it with /1 then add the remaining, the whole block becomes

location @test{
    rewrite ^ /1$1;
    proxy_pass http://localhost:5000;
}

Note: @Fleshgrinder provided an answer explaining why the first method didn't work.

查看更多
4楼-- · 2019-01-22 13:16

What's actually happening here?

nginx cannot process your desired URI part in the proxy_pass directive because you're within a named location (hence the error message). This is because nginx is built in a modular fashion and each configuration block is read in various stages by the various modules. So just remember that you cannot have a URI within your proxy_pass directive in the following cases:

  1. Regular Expression Locations
  2. Named Locations
  3. if Blocks

How could we solve this problem?

Mohammad AbuShady explained how to do a rewrite and pass the requested URI to the proxy server. I just wanted to clarify the reason.

查看更多
登录 后发表回答