Mark Mark - 2 months ago 16
PHP Question

Wordpress live site pages return 404 error nginx php-fpm

I have a wordpress site with the permalinks set to post name. All the pages do not appear and return a 404 except for the index. If I set this to plain, then they appear, but many resources are missing, and I'm not sure why.

So just to be clear, if permalink is plain, it works but then there are many errors as there is a 404 on multiple files (except on the index).
If permalink is postname then the entire page is a 404.

Here is my htaccess file, my nginx config, and my php-fpm config.
Can anyone see the problem?

# 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


NGINX CONF

server {
listen 80;
server_name www.example.com example.com;
root /var/www/example;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl;
server_name www.example.com example.com;
root /var/www/example;
index index.php;
ssl_certificate /etc/nginx/ssl/example.com/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com/example.com.key;

access_log /var/log/nginx/example.log main;
error_log /var/log/nginx/example_error.log;

location / {
root /var/www/example;
}

error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

}


PHP-FPM

[example]
listen = 127.0.0.1:9001
listen.allowed_clients = 127.0.0.1
user = nginx
group = nginx
...and some more I don't think is relevant but I can supply if asked for.


Thank you for all your help!

Answer

Without seeing the actual error or which files are 404'ing, I think you may be missing try_files in your nginx config. I'd check out this: http://nginxlibrary.com/wordpress-permalinks/

Also, nginx doesn't need or want .htaccess: https://www.nginx.com/resources/wiki/start/topics/examples/likeapache-htaccess/

Also, I'd check to make sure your URL are set correctly in your DB. (I'd post the link but I don't have the rep) I'd use wp-cli to do a search-replace. More than once I've forgotten to do this moving from dev to staging and had a wtf moment. :)

Hopefully this all helps!

* Edited to help a bit more *

After looking more into those tutorials, they leave out some finer points. Without going to far into it, try using something closer to this:

    listen       443 ssl;        
    root /var/www/example;
    index index.php index.html index.htm;

    server_name www.example.com example.com;

    ssl_certificate /etc/nginx/ssl/example.com/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com/example.com.key;

    access_log  /var/log/nginx/example.log main;
    error_log  /var/log/nginx/example_error.log;

    location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
               try_files $uri =404;
               fastcgi_split_path_info ^(.+\.php)(/.+)$;
               fastcgi_pass   127.0.0.1:9001;
               fastcgi_index index.php;
               fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
               include fastcgi_params;
    }

This is a mix of what you have and the configuration I run. Let me know how it does for you.