Enabling Nginx mod_rewrite

March 7, 2015 50k 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
RewriteRule . /index.php [L]

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

11 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.

try converting your 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.

Have another answer? Share your knowledge.