Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
 Post subject: NGINX Rewrite Question
PostPosted: Wed May 23, 2012 7:16 pm 
Offline
Newbie

Joined: Wed May 23, 2012 4:09 pm
Posts: 3
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.

Code:
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.


Top
   
 Post subject:
PostPosted: Wed May 23, 2012 7:21 pm 
Offline
Senior Newbie

Joined: Sun Aug 22, 2010 12:57 pm
Posts: 19
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:

Code:
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;
        }
}


Top
   
 Post subject:
PostPosted: Wed May 23, 2012 8:11 pm 
Offline
Newbie

Joined: Wed May 23, 2012 4:09 pm
Posts: 3
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


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group