Regular Expression global redirect

altiamge nginx-forum at
Sun Feb 26 03:59:50 UTC 2012

I'm using nginx as a reverse proxy for about 2000 websites. I'm trying
to find a good way to redirect all www traffic to nonwww addresses. I
don't want to have a separate entry for every domain...just a global
redirect in the server block preferably. I found lots of examples to do
this one domain at a time, but does anyone have any suggestions on how
to do it for the whole server?

I was thinking of extracting the domain something like this then using
an if statement, but I understand that if's are not recommended:

server_name ~^(\.)?(?<domain>.+)$;


here's my server block:

server {
 	listen 80;
 	server_name	_;

 	location / {
 		proxy_pass  http://websites;

Posted at Nginx Forum:,222984,222984#msg-222984

More information about the nginx mailing list