36

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;
    ...
}
5
  • add a trailing / after the 1, make it http://localhost:5000/1/; Commented Feb 9, 2014 at 23:27
  • 2
    @MohammadAbuShady Same error, I'm afraid. Commented Feb 10, 2014 at 14:37
  • 1
    well then maybe try the other way around, use the base proxy localhost:5000 but run a rewrite before it, rewrite ^ /1$1 last Commented Feb 10, 2014 at 14:39
  • @MohammadAbuShady perfect! Do you know where this is actually documented? Commented Feb 10, 2014 at 14:52
  • Don't know if there's a documentation for this, but I'll explain it in an answer Commented Feb 10, 2014 at 15:00

4 Answers 4

66

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Could you please provide a non-broken link for the following? It returns 404. nginx.com/resources/wiki/HttpProxyModule#proxy_pass
Thanks in my case I should add break rewrite ^ /search$1 break; or it will have this error: rewrite or internal redirection cycle while processing "/search
How rewrite will work if the proxy_pass is not localhost ?
60

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.

Comments

17

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

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

3 Comments

Yes, that'd work. However, I'd like it to be directed to localhost:5000/1. I'm trying to query that URI when an upload is complete. I've extended my original question now and hopefully it'll be more clear.
@Kate Ok. So that is working.You'd like it to be directed to localhost:5000/1 ..let me check.
This was exactly the problem with my config. I didn't realize that you couldn't have a trailing "/"
0

None of the above helped me.

My workaround is:

location @cat {
    resolver 8.8.8.8 valid=30s;
    set $upstream https://upload.wikimedia.org/wikipedia/commons/5/53/Sheba1.JPG;
    proxy_pass $upstream;
}

Note that a resolver directive might differ regarding your context.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.