Welcome! Log In Create A New Profile

Advanced

Re: Mass virtual hosting and global redirect

June 15, 2009 09:48AM
On Mon, Jun 15, 2009 at 09:30:47AM -0400, NoSync wrote:

> Hello everyone! I recently switched my main webserver (where I host clients' websites) from apache to nginx and I wrote a custom generic configuration file to handle everything in one place, so domains and subdomains are handled on the fly by creating directories and subdirectories. Here's the relevant snippet:
>
> server {
>
> listen 80 default;
>
> if ($host ~* ^(.*)\.(.*\..*)$) {
> set $sub $1;
> set $domain $2;
> }
>
> server_name _;
> server_name_in_redirect off;
> root /var/www/vhosts/$domain/$sub;
> access_log /var/log/nginx/access.log combined;
> error_log /var/log/nginx/error.log error;
>
> include /etc/nginx/conf/wordpress_params;
>
> location / {
>
> root /var/www/vhosts/$domain/$sub;
> index index.php index.html;
>
> Everything works just fine as nginx is a great product ;-) Now, my only problem lays in the fact I haven't been able to write a regexp to setup a global redirect from, eg, domain.com to www.domain.com. Could anyone help me on this?

Use server_name. Instead of

if ($host ~* ^(.*)\.(.*\..*)$) {
set $sub $1;
set $domain $2;
}

you may write in 0.7.x

server {
listen 80 default;
server_name ~^(.*)\.(.*\..*)$;
set $sub $1;
set $domain $2;

...
}

As to the redirect:

server {
listen 80 default;
server_name _;
rewrite ^ http://www.$host$request_uri?;
}

server {
listen 80 default;
server_name www.*;
...
}


--
Igor Sysoev
http://sysoev.ru/en/
Subject Author Posted

Mass virtual hosting and global redirect

NoSync June 15, 2009 09:30AM

Re: Mass virtual hosting and global redirect

Igor Sysoev June 15, 2009 09:48AM

Re: Mass virtual hosting and global redirect

NoSync June 15, 2009 11:23AM

Re: Mass virtual hosting and global redirect

Igor Sysoev June 15, 2009 11:34AM

Re: Mass virtual hosting and global redirect

NoSync June 15, 2009 12:22PM

Re: Mass virtual hosting and global redirect

Igor Sysoev June 15, 2009 02:47PM

Re: Mass virtual hosting and global redirect

NoSync June 16, 2009 09:43AM

Re: Mass virtual hosting and global redirect

Igor Sysoev June 16, 2009 09:56AM

Re: Mass virtual hosting and global redirect

NoSync June 16, 2009 10:32AM

Re: Mass virtual hosting and global redirect

Igor Sysoev June 16, 2009 10:34AM

Re: Mass virtual hosting and global redirect

NoSync June 16, 2009 11:06AM

Re: Mass virtual hosting and global redirect

edogawaconan June 18, 2009 09:13AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 154
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready