Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In Nginx, I would like to redirect my to .how do I do that ? I would like to use the existing sites config file for instead of creating a new one.

the config file I have is (how do I add to this)


server {
listen   80;
server_name  *;
access_log /srv/www/;
error_log /srv/www/;

location / {
    root   /srv/www/;
    index  index.html index.htm;

location /phpmyadmin { root /usr/share; index index.php; }

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  /srv/www/$fastcgi_script_name;



share|improve this question

Sorry Mike, your if statement solution is a bad idea... refer to the Nginx pitfalls wiki page for why (in this case nginx will evaluate the directive for every request, which is inefficient)...

add another server block above the one you have containing this

server {
   rewrite ^ $scheme://$request_uri redirect;

and magic will be done...

share|improve this answer

This would go in your server block. This is untested but should work or give you an idea

if ($host ~* "^(.*)\.domain\.com$"){
  set $subd $1;
  rewrite ^(.*)$$subd permanent;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.