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

I'm trying to rewrite a particular url, but it doesn't seem to work unless a subdomain is included as part of the url. For example, given the following:

server {
  server_name ;
  listen  80  default;
  root  /usr/share/nginx/www/example/public;
  rewrite ^ /test redirect;
} correctly redirects to But does not?

I'm running nginx 1.0.11 (custom built) on a Fedora box if that helps.

EDIT: PS - I've also tried server_name * with no luck.

share|improve this question
Increase your error_log verbosity to notice, and turn on rewrite_log. See if anything relevant shows up in the error logs, and post the relevant sections here. (You can also add the same directives to the http block to see that the error is not originating elsewhere). – cyberx86 Jan 4 '12 at 4:29
You probably have another server block interfering, please paste all your server blocks. – Martin Fjordvald Jan 4 '12 at 7:46
Wow, I'm a total moron. Sorry for all the noise. Basically, I edit /etc/hosts so that I can type "" into my browser and have it point to rather than the 'real' "". I do this so I can write things like an nginx config with "" rather than having to type "localhost" on one machine and then "" on the production server. The only problem is that I didn't add to /etc/hosts! So it was pointing to the 'real' server which I was obviously not changing. Thanks for all the help. – ktr Jan 4 '12 at 17:05
up vote 0 down vote accepted

You can try redirecting any request to go to

server {
    listen 80 default;
    rewrite ^/(.*)$1 permanent;

server {
    location /original/ {
        rewrite ^/(.*) redirect;
    # rest of config here;
share|improve this answer
Thanks, but still no luck. The 1st rewrite happens as expected ( ->, but rewrites inside of the directive still don't seem to work ... – ktr Jan 4 '12 at 4:11
Made a change in the rewrite block. And forgot to add, the root line is not needed in the www block. – 111110100 Jan 4 '12 at 7:20
Does the above example work on your machine? I.e., just wondering if you tried it out ... it still doesn't seem to redirect anything (even with the location directive) on my machine. Maybe I'll try it out on another machine and see if I have the same problem. Thanks for the suggestions though. – ktr Jan 4 '12 at 16:45
@ktr the above example is the same one I use on production servers. Redirect the www to the none-www domain. – 111110100 Jan 5 '12 at 6:41
thanks for the help, got it working :) – ktr Jan 5 '12 at 17:59

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.