Question

Enabling Nginx mod_rewrite

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 %{REQUEST_FILENAME} !-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.

Subscribe
Share

This comment has been deleted


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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

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

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.

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

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

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

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

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

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