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/ config file

server {
rewrite ^(.*)$1 permanent;
server {
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/; rewrite /home/domain^(.*) /home/domain$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?

How then nginx config should be for permanent redirect from to in right way in one server block?

Check out this page :

Basically, you just need one server block containing :

if ($host != '' ) { rewrite ^/(.*)$$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.


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.