Score:0

nginx config reverse proxy lose basepath on redirect without slash

in flag

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?

Richard Smith avatar
jp flag
You can use [`proxy_redirect`](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#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.
Linh Nguyen avatar
in flag
@RichardSmith i'm getting `HTTP/1.1 301 Moved Permanently` from the curl -i , whatshould i put proxy_redirect value as?
Richard Smith avatar
jp flag
Used `-I` (uppercase `I`) to see the value of the `Location:` response header.
Linh Nguyen avatar
in flag
@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!
Score:0
in flag

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
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.