albasiero
By:
albasiero

Enabling Nginx mod_rewrite

March 7, 2015 69.2k views
Nginx WordPress CentOS

I just moved a Wordpress blog on a new droplet hosting account. Previously the hosting was on Apache, now is on Nginx. I have a problem that now Wordpress pretty links (post-name) are not working and I think the problem comes from the fact that mod)rewrite is not enabled.

Till now I had it in htaccess like

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUESTFILENAME} !-f
RewriteCond %{REQUEST
FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

How can I implement this in Nginx and where specifically? I am a complete beginner.

13 Answers

Nginx does not support .htaccess files.

On CentOS Nginx stores virtual host files in /etc/nginx/conf.d. I assume you're using the default.conf file. So edit this file and find the following:

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

and make the following change:

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.php?$args;
    }

Save the file and reload Nginx:

sudo service nginx reload

In your site's server block add /index.php?$args;
to try_files so that you get something like this:

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

Then go into your Wordpress settings and re-enable pretty permalinks.

The server block is defined by

server {
...
}

By default it resides within the configuration file in /etc/nginx/sites-available

  • Thanks, but in /etc/nginx/ there is no sites-available folder and no file available.

Then it should be in /etc/nginx/nginx.conf

I'm not sure where it would be in CentOS.

Finally, there were needed the arguments to nGinx config to the End Of File

$ nano /usr/local/apps/nginx/etc/conf.d/common

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

then Restart Nginx
$ service nginx restart

And finally in WP-Admin --> settings--> Permalinks
-->set to default once... then set to "Post Name" which rewrites WP rules.

This solved the issue for me, maybe it will also help anyone else.

  • to be clear are you saying you added this block:

    $ nano /usr/local/apps/nginx/etc/conf.d/common

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

    to the end of your nginx.conf file?

try converting your htaccess - http://winginx.com/en/htaccess

PS - I know this is old thread but may be this can be helpful for someone else.

My WORKED solution..!
Already its mentioned by "UKn0Me March 7, 2015"

Plz follow:

  1. Use filezilla, login to your SSH-FTP using top level SSH user (may 'root')
  2. Goto the path "/home/Domain-UserName/conf/web/nginx.conf"
  3. Probably you should see..

try_files $uri $uri/ /index.php?$args;
Here, Nginx checks for the existence of a file at the URL ($uri), then for a directory ($uri/).
If it doesn’t find a directory or a file, it performs an internal redirect to /index.php passing the query string arguments as parameters.

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

  1. If your blog is in a subfolder (say /wordpress), you’ll have to add an extra location /wordpress/ block to your configuration file :

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

Restart NGINX (or) may be whole server.

My server configs are:
DigitalOcean, Ubuntu 14x, Vesta Nginx+php-fpm, Wordpress on subfolder.

I have the following:

try_files $uri $uri/ =404;

Should I just replace the above line with

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

Or should I add it as a new line? Just curious about that before I commit to the change.

Thank you
Brian

I followed the tutorial here (https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lemp-on-ubuntu-16-04) to setup Wordpress on NGINX and at the end of it I get the 404 page. I had

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

in but that was giving the generic 404 - so I went back to

try_files $uri $uri/ =404;

and I get a custom 404 I built. HOWEVER I am not getting the Wordpress index.php (gives the 404) but I can get the default DigitalOcean index.html

  • NM - I am an idiot. I had commented out the root and index lines for the main server....

Have another answer? Share your knowledge.