Changing permalinks gives 404 errors on nginx
Scenario :-
- Changed permalinks from the default to /%postname%/, and now the links within WordPress’s Admin panel give me 404 errors – Not WordPress 404 pages, nginx 404 pages.
- Tried creating a blank .htaccess file in the wordpress folder, giving it 666 permissions, changing the user and group to www-data and then changing the permalinks- that didn’t work.
- Then changed it to this before changing the permalinks:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
- When that didn’t work, changed RewriteBase to /wordpress/ before changing permalinks again – still nothing.
- Also gone into the site’s .conf file and changed try_files $uri $uri/ /index.php; to the following, restarting nginx and php5-fpm each time;
try_files $uri $uri/ /index.php?$query_string;
try_files $uri $uri/ /index.php?q=$request_uri;
try_files $uri $uri/ /index.php?$args;
- Running a server with nginx.
Solution :-
Those are Apache .htaccess rewrite rules, but it stated that domain is hosted on an Nginx server. Nginx does not use an .htaccess-like directory level file, much less does it use the .htaccess file itself. So, we need to edit the site’s nginx configuration itself.
sudo vi /etc/nginx/site-enabled/domain.conf
- Edit/add/append the following location block within the server block:
Sample configuration file is as follows :-
# WordPress single blog rules.
# Designed to be included in any server {} block.
# The rest of your server block
# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
- If you are using wordpress multisite with custom permalink setting: /%category%/%postname%/
/etc/nginx/site-available/domain.conf
On server
location / {
try_files $uri $uri/ /index.php?q=$uri$args;
}
- If your root wordpress is not the webroot but http://domain.com/wordpress/:
location /wordpress/ {
try_files $uri $uri/ /wordpress/index.php?q=$uri$args;
}
- If you are using old wordpress with blogs.dir,
add: location ^~ /blogs.dir { internal; alias /var/www/wordpress/wp-content/blogs.dir; access_log off; log_not_found off; expires max; }
Check the nginx configuration:
sudo nginx -t
Reload nginx:
sudo service nginx reload
Also try change permalink settings.