How to remove first part of route in nginx?

Viewed 15

I have nginx configured on server like below

localhost ~ elastic{
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

With this I go on http://nginx_host/elastic/something I reach http://localhost:5000/elastic/something.

But I want to change it such that by going on http://nginx_host/elastic/something I reach http://localhost:5000/something.

I have many other services running on this server using same configuration as above, therefore I cannot use a generic method to remove the first part of the path, it needs to be specifically to remove elastic from start. How can I do this?

1 Answers

You can modify the config like this:

localhost  /elastic/ {                  # note the trailing slash here
  proxy_pass http://localhost:5000/;    # note the trailing slash here
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
}

The trailing slashes will ensure that /elastic/ are removed from the url while proxying the request. So, http://nginx_host/elastic/something will now go to http://localhost:5000/something.

Related