Brideo
Brideo

Reputation: 163

How to keep url port with proxy_pass in nginx

I am trying to use proxy pass to access an image service, however, my nginx container is running on port 8080 and when nginx passes to the service it redirects the user to port 80.

Here is my nginx snippet:

location /api/Image/ {
   proxy_pass http://image.service.com/api/Image?$args;

   proxy_redirect off;
   proxy_set_header Host $host;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Example of my curl request:

➜  .ssh curl -I http://local.docker:8080/api/Image\?urlId\=s4U3y%2FjS%2FPqDJjS%2BQzOjOu4PvCLx8QQQmwd2%2BwTT8C8%3D
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 03 Dec 2018 16:01:18 GMT
Content-Type: text/html
Content-Length: 178
Location: http://local.docker/api/Image/?urlId=s4U3y%2FjS%2FPqDJjS%2BQzOjOu4PvCLx8QQQmwd2%2BwTT8C8%3D
Connection: keep-alive
Strict-Transport-Security: max-age=63072000; includeSubdomains;
X-UA-Compatible: IE=Edge

My end goal is to add a jpg suffix, but my main issue is that I can't work out how to prevent nginx from redirecting to port 80.

Upvotes: 2

Views: 589

Answers (1)

Andrew Howden
Andrew Howden

Reputation: 307

Based on that NGINX block, you will need to request that URL including the trailing slash.

The NGINX block will match URLs as follows:

/api/Image/foo/bar
/api/Image/

But not

/api/Image

Which is how your cURL command is constructed:

curl http://local.docker:8080/api/Image\?urlId\=s4U3y%2FjS%2FPqDJjS%2BQzOjOu4PvCLx8QQQmwd2%2BwTT8C8%3D

It's likely the redirect is coming from the default block, and I would guess it's not coming from NGINX at all but rather an upstream proxy.

Upvotes: 2

Related Questions