When using Nginx and creating a new virtual server, nginx.conf need "include" in server block

After you create a virtual server with Nginx, the server block needs an "include" at the end, so Nginx is able to load additional configs for that server. You can use the already existing one that is commented out

include /etc/nginx/conf.d/*.conf;

but it needs to be put at the end of the server block.

Image attached for reference

Status: 
Closed (fixed)

Comments

You can configure the Virtualmin Nginx module to add custom directives to new server blocks. This can be done at System Settings -> Features and Plugins, by clicking on Configure next to Nginx and filling in the "Additional Nginx directives for new virtual hosts" field.

awesome thanks Jamie!

Jamie,

When using the Nginx directives, I found a bug when I used the "File or directory for new virtual hosts", which does work for Virtual Server creation, but when you delete the server it leaves the server in the directory without deleting it like it's supposed to.

to fix, I have to add:

include /etc/nginx/sites/*;

in the main nginx.conf

Now, it deletes the virtual server from the directory as expected, however I do get an error:

Applying Nginx configuration .. .. configuration is invalid : nginx: [emerg] unexpected end of file, expecting ";" or "}" in /etc/nginx/sites/nikanti.net.lock:2 nginx: configuration file /etc/nginx/nginx.conf test failed

I'm not sure if this is related, but I tried to disable the "Test new configuration before saving?" in Webmin > Servers > Nginx Webserver > Edit Configuration Files, but after saving, it doesn't disable.

This is only a minor bug as it's only a warning, but it might confuse a hosting customer.

Change your include line to :

include /etc/nginx/sites/*.conf;

that will make it skip any .lock files that Virtualmin creates.

The problem here is that the sites are stored as their domain names in the directory:

domain1.com domain2.net etc.

so include /etc/nginx/sites/*.conf; doesn't work. It would only work if I stored all the sites within a single sites.conf file though, but having each server with their own config file is better organized.

here's a fix, in case anyone else want to store servers in directories as separate files. I just created a few includes for each tld I want to support:

include /etc/nginx/sites/.com; include /etc/nginx/sites/.net; include /etc/nginx/sites/.org; include /etc/nginx/sites/.th; include /etc/nginx/sites/*.de;

etc, etc.

After testing, it doesn't work properly because it puts the directives in the wrong place.

For example, I need to include:

location / { try_files $uri $uri/ /index.php?$args; }

Which allows Nginx to do pretty permalinks in WordPress. The location of the directives needs to happen after:

location ~ .php$ { try_files $uri =404; fastcgi_pass unix:/var/php-nginx/139563233210424.sock/socket; }

So you need a way to insert directives into the defaultlocation block for .php that Virtualmin creates? Unfortunately we don't have a way to configure that yet, sorry.

Jamie,

All that it needs is:

include /etc/nginx/conf.d/*.conf;

exactly where I showed you in the pic. That will load the various directive files needed, exactly how it's supposed to work.

Or you can move the insertion of the directives at the end, instead of inserting it at the very beginning.

For reference:

http://codex.wordpress.org/Nginx

The nginx wordpress directives have to be added at the end, in their example they are using:

include sites-enabled/*;

It has to be included in the server block, but at the end of the file. It won't work inserting the directives before the:

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/php-nginx/139563233210424.sock/socket;
}

You can see in the picture, highlighted in red, that the directives are being inserted into the wrong place. It doesn't make sense to add the directives here.

I mentioned where the directives have to be inserted since the very first post. If it's inserted where Virtualmin currently does, Nginx will fail to restart. If you insert the directives at the end, it works like it's supposed to.

Unfortunately, Virtualmin doesn't necessarily know what the correct ordering should be for those custom directives - it just inserts them after the first set of default lines.

Instead of adding your custom settings this way, a better option may be to write a small script that gets called after a domain is created to add the directives - see https://www.virtualmin.com/documentation/developer/prepost

Thanks, didn't know there was an option to do that. I'll give it a try.

Forgot to update that I got it working. Thanks Jamie.

Mostafa's picture
Submitted by Mostafa on Tue, 10/11/2016 - 17:31

You can use the following directive to include all files inside the conf.d folder except the .lock files:

include /etc/nginx/conf.d/*[!lock];