António P. P. Almeida
February 25, 2012 11:42PM
On 26 Fev 2012 04h59 CET, nginx-forum@nginx.us wrote:

> 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>.+)$;
>
> thanks,
> altimage
>
> here's my server block:
>
> server {
> listen 80;
> server_name _;
>
> location / {
> proxy_pass http://websites;
> }
> }
Try:

server {
server_name ^~www\.(?<domain>.*)$;
return 301 http://$domain;
}

server {
server_name ^~(?<domain_name>[^\.]*)\.(?<tld>[^\.]*)$;

location / {
proxy_pass http://$domain_name.$tld;
}
}

--- appa

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

Regular Expression global redirect

altiamge February 25, 2012 10:59PM

Re: Regular Expression global redirect

António P. P. Almeida February 25, 2012 11:42PM

Re: Regular Expression global redirect

Edho Arief February 26, 2012 01:22AM

Re: Regular Expression global redirect

António P. P. Almeida February 26, 2012 09:40AM

Re: Regular Expression global redirect

Edho Arief February 26, 2012 09:44AM

Re: Regular Expression global redirect

altiamge February 26, 2012 02:27AM

Re: Regular Expression global redirect

altiamge February 26, 2012 06:39PM

Re: Regular Expression global redirect

Edho Arief February 26, 2012 02:30AM

Re: Regular Expression global redirect

António P. P. Almeida February 26, 2012 07:22PM

Re: Regular Expression global redirect

altiamge February 26, 2012 07:26PM

Re: Regular Expression global redirect

altiamge February 26, 2012 08:15PM

Re: Regular Expression global redirect

António P. P. Almeida February 26, 2012 07:42PM

Re[2]: Regular Expression global redirect

Max February 27, 2012 01:34AM

Re: Re[2]: Regular Expression global redirect

António P. P. Almeida February 27, 2012 05:14AM

Re[4]: Regular Expression global redirect

Max February 27, 2012 10:48PM

Re: Re[4]: Regular Expression global redirect

António P. P. Almeida February 28, 2012 07:12AM

Re: Regular Expression global redirect

Edho Arief February 26, 2012 01:12AM

Re: Regular Expression global redirect

Mark Alan February 26, 2012 07:20AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 157
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready