Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm running nginx and in the config file I need to always have the domain example.com redirect to example.com/test. I tried various methods for achieving this but I always got a redirect error.

EDIT: It's worth mentioning that I only want example.com to redirect to example.com/test. I don't want example.com/something to have /test in the url.

What is the correct way to do this?

This is a snippet of my current nginx.conf:

server {
  server_name example.com www.example.com;
  location / {
    rewrite ^.+ /test permanent;
  }
}

server {
  listen 80;
  server_name www.example.com example.com;
  location / {
    root /var/www/apps/example/current/public;
    passenger_enabled on;
    rails_env production;
  }
}
share|improve this question
    
Why have you got two server blocks with the same server_name? NGINX will only process the first one it comes to –  sam Jun 24 '11 at 7:56
    
I edited out some of the domains for simplicity. The last server_name field includes another domain as well. Basically, there are two domains, each pointing to the same CMS. The CMS then displays the correct domain based on virtual folder name. It removed it so that people wouldnt get confused. –  Amund Jun 24 '11 at 7:58
    
@Amund Each server_name should only occur in ONE server block. I.E. example.com can only be in one server block regardless of how many other domains you have –  sam Jun 24 '11 at 8:01
    
Are you sure about that? Because I`ve gotten redirects to work perfectly fine with two server blocks. For instance the first one redirects www.example.com to example.com and the second block is for the actual application. –  Amund Jun 24 '11 at 8:09
    
@Amund Yes, NGINX will only process a single server block, the first one it finds and ignore all others –  sam Jun 24 '11 at 14:38
show 1 more comment

2 Answers

up vote 0 down vote accepted

I do not remember, but somehow the right way:

server {
    server_name  example.com;
    rewrite ^/$ http://www.example.com/test   permanent;
}
share|improve this answer
    
Thanks, mate. That did the trick! –  Amund Jun 28 '11 at 11:02
add comment

samarudge is right - you should have only one server block.
Try putting root, passenger_enabled and rails_env right under server (or even http)

server {
  listen 80;
  server_name www.example.com example.com;
  root /var/www/apps/example/current/public;
  passenger_enabled on;
  rails_env production;

  location / {
      rewrite ^.+ /test permanent;
  }
}
share|improve this answer
    
This is what I'm doing now, but all references I find on the web uses two block. Is that outdated information? Both seems to work, so I`m a bit confused. –  Amund Jun 28 '11 at 11:04
    
Passenger nginx manual modrails.com/documentation/… describes possible places for each directive. –  SaveTheRbtz Jun 28 '11 at 11:06
add comment

Your Answer

 
discard

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.