NGINX Rewrite Question

Hello!

I looked at the NGINX wiki and found some examples on how to use the rewrite module for redirecting a HTTP request. I seem to have stumbled upon a problem though. The code is below.

server {
        listen          80;
        server_name     unknown-server.com;
        rewrite         ^ http://www.unknown-server.com$request_uri? permanent;
        access_log      /srv/www/unknown-server.com/logs/access.log;
        error_log       /srv/www/unknown-server.com/logs/error.log;
        root            /srv/www/unknown-server.com/public_html;

        location / {
                index   index.html index.htm index.php;
        }

        location ~ \.php$ {
                try_files       $uri =404;
                include         /etc/nginx/fastcgi_params;

                if ($uri !~ "^/img/") {
                        fastcgi_pass 127.0.0.1:9000;
                }

                fastcgi_index   index.php;
                fastcgi_param   SCRIPT_FILENAME /srv/www/unknown-server.com/public_html$fastcgi_script_name;
        }
}

When I visit unknown-server.com it will redirect to www.unknown-server.com (adding the 'www') which is exactly what I want; however the actual index.html file is not showing up but rather the default NGINX website that shows "Welcome to nginx!"

What I would like to have is a redirect of http://unknown-server.com to http://www.unknown-server.com or anything after. For example if someone visits http://unknown-server.com/forums/path/to/something NGINX will redirect it to http://www.unknown-server.com/forums/path/to/something - just adding the 'www' but keeping the path.

How can I do this?

Thanks in advance.

[NOTE]:

Right now the NGINX server has unknown-server.com set to accept both unknown-server.com and www.unknown-server.com requests. I set it back until I can fix this.

2 Replies

So the reason you're getting the "welcome to nginx" index.html, is because you haven't defined a server with a name that matches "www.unknown-server.com". You need to split this config into two blocks, one as a canonical redirect, and the other as the real server. For example:

server { 
        listen          80; 
        server_name     unknown-server.com; 
        rewrite         ^ http://www.unknown-server.com$request_uri? permanent;
}

server { 
        listen          80 default; 
        server_name     www.unknown-server.com;
        access_log      /srv/www/unknown-server.com/logs/access.log; 
        error_log       /srv/www/unknown-server.com/logs/error.log; 
        root            /srv/www/unknown-server.com/public_html; 

        location / { 
                index   index.html index.htm index.php; 
        } 

        location ~ \.php$ { 
                try_files       $uri =404; 
                include         /etc/nginx/fastcgi_params; 

                if ($uri !~ "^/img/") { 
                        fastcgi_pass 127.0.0.1:9000; 
                } 

                fastcgi_index   index.php; 
                fastcgi_param   SCRIPT_FILENAME /srv/www/unknown-server.com/public_html$fastcgi_script_name; 
        } 
}

Thank you so much for the explanation and example! It worked perfectly and it's just how I want it. I couldn't believe it was something so simple.

:D

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct