Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx 'proxy_pass' cannot have URI part in location?

Tags:

nginx

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;     ... } 
like image 805
Kar Avatar asked Feb 09 '14 18:02

Kar


People also ask

Why can't Nginx process the Uri part in the proxy_pass directive?

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.

Can proxy_pass have Uri part in regular expression?

2018/11/17 16:47:03 [emerg] 1#1: "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/nginx.conf:36 I've gone through several examples and reviewed a number of answers and I believe that the configuration I have should work.

What is proxy_pass in Nginx?

A simple example A proxy_pass is usually used when there is an nginx instance that handles many things, and delegates some of those requests to other servers. Some examples are ingress in a Kubernetes cluster that spreads requests among the different microservices that are responsible for the specific locations.

How do I bypass Nginx's requirement for hosts to be available?

You can circumvent nginx's requirement for all hosts to be available at startup by using variables inside the proxy_pass directives. HOWEVER, for some unfathomable reason, if you do so, you require a dedicated resolver directive to resolve these paths. For Kubernetes, you can use kube-dns. kube-system here.


1 Answers

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.

like image 109
Mohammad AbuShady Avatar answered Sep 21 '22 12:09

Mohammad AbuShady