Want to redirect the server url to server/somestring through nginx

8/22/2019

want to redirect the server url to server url/somestring through nginx.conf. Tried location /somestring{} inside server tag in nginx.conf, didn't work. Please suggest how can i achieve that.

-- Tanvi Pagare
kubernetes
nginx

2 Answers

8/22/2019

An example of nginx config that allows to redirect multiple domains. You can use a similar approach to redirect paths as well.

map $http_host $redirect_destination {
    hostnames;
    default where-to-go-by-default;
    my-domain where-to-redirect-my-domain;
}

server {
    listen 80;
    location / {
        return 301 https://$redirect_destination/some-path;
    }
}
-- Keilo
Source: StackOverflow

8/26/2019

By using the return directive we can completely avoid evaluation of regular expression. Please refer to documentation.

The following location block may be selected to respond for request URIs that look like /site, /site/page1/index.html, or /site/index.html. You can read more about location block here.

In your case it would be similar to that:

location /old-url {
  return 301 /new-url;
-- muscat
Source: StackOverflow