Nginx – Having twice a “location / {}” block in nginx

nginxredirectrewrite

For all our sites we have an include file which contains several things, amongst this line:

location / { 
    try_files $uri /index.php?$query_string;
}

For a single site, we do want to redirect all requests from mysite.com/ to mysite.com/blog, as a temporary "fix". The block looks like this:

server {
    server_name mysite.com;
    root        /var/www/mysite;
    include     conf.d/common.conf.inc;

    # Redirect blog
    location / {
        rewrite / /blog redirect;
    }
}

This fails however, with the following error:

Testing nginx configuration: nginx: [emerg] duplicate location "/" in /etc/nginx/sites-enabled/mysite:7

The question

How can we have a server block with location / {} for the try_files and redirect all requests from / to /blog?

Best Answer

You may try as a solution to make redirect only from exatly root location:

 location = / {
        rewrite / /blog redirect;
    }