Restore problem with nginx virtual domain config

Hello all,

Tried to restore full virtualmin domain backup, but I got error with restoring nginx site config.

Original sites-available/domain.com config file

server {
server_name domain.com;
rewrite ^(.*) http://www.domain.com$1 permanent;
}
server {
server_name www.domain.com;
listen 80;
root /home/domain/public_html;
index index.html index.htm index.php;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SCRIPT_FILENAME /home/domain/public_html$fastcgi_script_name;

location ~ \.php$ { try_files $uri =404; fastcgi_pass localhost:9002; return 403;}

}

and this is after restoring backup:

server { server_name /home/domaindomain.com; rewrite /home/domain^(.*) /home/domainhttp://www.domain.com$1 /home/domainpermanent; access_log /home/domain/var/log/virtualmin/domain.com_access_log; error_log /home/domain/var/log/virtualmin/domain.com_error_log; }

Restored config is not full and also it need to be fixed.
Is it a bug?

This problem here may be that you have two separate server blocks for the domain, which Virtualmin doesn’t expect (or create). Is that really necessary?

Maybe.
How then nginx config should be for permanent redirect from domain.com to www.domain.com in right way in one server block?

Check out this page : http://forum.slicehost.com/index.php?p=/discussion/1425/redirect-non-www-to-www-with-nginx/p1

Basically, you just need one server block containing :

if ($host != 'www.site.com' ) { rewrite ^/(.*)$ http://www.site.com/$1 permanent; }

Yes, I know this config too.
But using “if” is a bad practice (according to author of nginx).

Isn’t better to use whole file to backup but not content of that file?

We can’t really use the whole file, because on some systems Nginx just has a single config file.

What’s the problem with using an if statement?

Okay, maybe is it possible to make an option to backup whole file?

There is no problem with if statement but it’s good to follow nginx author recommendations.

Thanks.

That’s kind of tricky to do in a general case, as it is possible for other domains to have server blocks in the same file.