简体   繁体   中英

How to remove first part of route in nginx?

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM