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

Yet another nginx rewrite rule question

How can I do a rewrite from http://www.* to http://* ?

thanks in advance

-- Deb


I'm sorry I didn't see the textile formatting removed the * from my question. I fixed it now. What I need to do is go from to, where joe could be any word.

share|improve this question
up vote 7 down vote accepted

Whats the significance of the extra period before Is the goal to remove the www from the URL? If so, this should do the trick:

if ($host ~* www\.(.*)) {
  set $host_without_www $1;
  rewrite ^(.*)$ http://$host_without_www$1 permanent; # $1 contains '/foo', not ''

Don't forget to: sudo /etc/init.d/nginx restart to load it up

Source: NGINX Wiki

share|improve this answer
I'm sorry I didn't see the textile formatting removed the * from my question. What I need to do is go from to, where joe could be any word. In so you are right, I just need to remove the www. – deb May 8 '10 at 13:38

That's quite a bit of a hack.

The fastest way performance wise would be

server {
  rewrite ^$request_uri permanent;

You save a regex match as well as two captures plus you get the advantage of nginx using hash tables to look up the matching server block.

Also, you do not need to restart nginx - a reload is all that's required, and whoever would want to have more down time than required?

share|improve this answer
+1 makes sense...cheers – iainlbc May 7 '10 at 20:18
This is not a hack. Nginx does very fast lookup through server names, so this is really the fastest approach. – Alexander Azarov May 13 '10 at 7:33
@Alaz That was in response to the answer provided by iainlbc, I meant that the if condition was a hack, not the server look up I recommended. – Martin Fjordvald May 13 '10 at 12:17

You can use regular expression server names (see

server {
  listen 80;
  listen 443;
  server_name ~^www\.(\w+)\.domain\.com$
  location / {
    rewrite ^ $scheme://$$request_uri permanent;
share|improve this answer

Martin F's solution is all well and good, until you've got hundreds of domains. I would, however, suggest going the other way - serve the app at, and redirect from Pretty sure that's in an RFC.

share|improve this answer
If you have nginx handle that many domains then write a script to generate the configuration. It's a fairly straight forward configuration language and you really don't want to do regex parsing on every page load on a high traffic server. – Martin Fjordvald May 8 '10 at 19:16
In principle, perhaps, but have you profiled it? I've had some very high load sites using regex for all requests. It takes care. Also, many shared hosting environments don't have serious performance concerns, but do have management overhead concerns. Both solutions could work well within different paramaters. – Justin Alan Ryan May 8 '10 at 19:21
If you have hundreds of domains, use regexp in server_name: – Alexander Azarov May 13 '10 at 7:34

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.