Virtualmin + Nginx - Rewrite for WP

Hi Guys

Im new here and new to Virtualmin, i have had a play about with it and put a wordpress site onto it.
I have tried so many times, searched the internet to find a solution to the Nginx rewrite issue.

I have added somethings into the conf file within webadmin but nothing seems to work.
Each time i change my permalinks in wp to look better it doesnt work.

Any help would be great.


You need to add the following to the nginx .conf for your site:

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

You can make virtualmin create new virtual hosts with this config built in, although updates to virtualmin will wipe this out:

Login as root, or sudo and edit the following file in your favorite editor:


Jump down to line 247, and you’ll see the following:

my $ploc = { 'name' => 'location’,
                'words' => [ '~', '\.php$' ],
                'type' => 1,
                'members' => [
                { 'name' => 'try_files',
                    'words' => [ '$uri', '=404' ],
                { 'name' => 'fastcgi_pass',
                    'words' => [ $port =~ /^\d+$/ ?
                                'localhost:'.$port :
                                'unix:'.$port ],
&save_directive($server, [ ], [ $ploc ]);

Right below that (before the “&flush_config_file_lines();” line), paste in the following:

my $wordpress = { 'name' => 'location',
                'words' => [ '/' ],
                'type' => 1,
                'members' => [
                { 'name' => 'try_files',
                    'words' => [ '$uri', '$uri/', '/index.php?$args' ],
&save_directive($server, [ ], [ $wordpress ]);

Save and exit.

I dont have this folder and i have a fresh install. Is there something missing.
I have installed minimal install would that make a difference.

Also when adding the following

I get this error

Failed to apply Nginx configuration : Configuration is invalid : nginx: [emerg] “location” directive is not allowed here in /etc/nginx/nginx.conf:125 nginx: configuration file /etc/nginx/nginx.conf test failed

What OS is your install on?

The location directive does not go in your main nginx.conf, but in the conf file for your virtual host.

Currently running on Centos7

I’ve done a fresh install of Debian 10 and changed this bit now.

But dont know where to add this in:

On the web inteface for webmin and virtualmin, go to
webmin —> servers -->nginx server.
click on edit configuration files
there will be a menu at the top where you can select your main nginx.conf as well as any of the conf files for your virtual domains. Choose the domain you wish to edit.

e.g. /etc/nginx/sites-available/

add the location directive below the location directive that is already there.

restart nginx (e.g. “service nginx restart”)

You should be good.


Just to get this right…

If I add this to each wordpress site that will work for the url rewrite, and wont be affected by any updates to webmin

If i add this, when creating new virtual servers it will have all the details for wordpress rewrite. But if virtualmin is update this will get wiped. Correct?

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

You’ve got them backwards. The first bit of Perl code is added to the Perl file so that in future, new virtual accounts will be created with the rewrite rules (until an update wipes it out).

The second thing, the Location directive goes in the Nginx config files for the relevant domains. It won’t get wiped out unless you do it.

Sorry that’s what I meant and that’s what I done.
Been staring at this screen too long lol

@palomnik thank you so much. I have searched for days and have read and tried so much things.

Works 100%

Thank you.

I just simply followed instructions from this blog article and have had my server running with no issues even after a bunch of upgrades:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.