Nginx + WordPress, 404 errors for all pages?
We installed Nginx, MariaDB, PHP, and WordPress on Mac OS. The homepage is displayed fine, but all other pages are returning 404 errors?
Tested with
- Nginx 1.17.9
- WordPress 5.4
- PHP 7.4
Review the current Nginx + WordPress integration.
nginx.conf
server {
listen 8080;
server_name localhost;
root /usr/local/var/www/wordpress;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
1. Solution
To fix it, add try_files $uri $uri/ /index.php?$args;
in the location /
and reload the Nginx’s config file.
nginx.conf
location / {
# file ($uri) or directory ($uri/)? if not, redirect to /index.php + query string
try_files $uri $uri/ /index.php?$args;
index index.html index.htm index.php;
}
Below is a working nginx.conf
on our test machine.
nginx.conf
user mkyong staff;
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 8080;
server_name localhost;
error_log /usr/local/var/log/nginx/error.log;
access_log /usr/local/var/log/nginx/access.log main;
root /usr/local/var/www/wordpress;
location / {
try_files $uri $uri/ /index.php?$args;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
Reload the Nginx config file.
Terminal
$ nginx -s reload
in 2022 not work
2023… location / {
try_files $uri $uri/ /index.php?$args;
}
worked like a charm
It works fine, you have my gratitude
thanks it worked like a charm !
it works, thank you
it works, thank you