1

i have the following nginx configs to redirect the url path to it's perspective services

server { listen 80; server_name abc.com; location = favicon.ico { access_log off; log_not_found off } proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_cache_bypass $http_upgrade; location /a-ms/ { rewrite /a-ms/(.*) /$1 break; proxy_pass http:host.docker.internal:3000/; } location /b-ms/ { rewrite /b-ms/(.*) /$1 break; proxy_pass http:host.docker.internal:4000/; } } 

the backend microservices using nodejs to host the api and swagger doc

When i got to a url from a browser like abc.com/a-ms/doc/ it return the swagger normal, but when i go to url without slash for example abc.com/a-ms/doc it redirected me to abc.com/doc which is not what i wanted(it missing the location path)(. How do i fix this with nginx config settings?

4
  • 2
    You can use proxy_redirect to correct a 3xx response from your application. Use: curl -I http://example.com/a-ms/doc to identify the exact value of the Location http response header. Commented Apr 26, 2022 at 7:25
  • @RichardSmith i'm getting HTTP/1.1 301 Moved Permanently from the curl -i , whatshould i put proxy_redirect value as? Commented Apr 26, 2022 at 7:58
  • Used -I (uppercase I) to see the value of the Location: response header. Commented Apr 26, 2022 at 8:25
  • @RichardSmith i checked and the location is /doc/. I updated my nginx config file with proxy_redirect /doc /a-ms/doc and now it's working. Thank you! Commented Apr 26, 2022 at 8:36

1 Answer 1

1

following RichardSmith suggestion of using proxy_redirect and now my location redirect is correct and included the location path

location /a-ms/ { rewrite /a-ms/(.*) /$1 break; proxy_pass http:host.docker.internal:3000/; proxy_redirect /doc /a-ms/doc; #add this } location /b-ms/ { rewrite /b-ms/(.*) /$1 break; proxy_pass http:host.docker.internal:4000/; proxy_redirect /doc /b-ms/doc; #add this } 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.