WordPress 3.0 & nginx - permalink, 404 problem

2019-03-19 11:13发布

I've installed nginx, FastCGI and PHP on my server. WordPress 3.0 installed after a bit of a monster battle, but it's installed and working well.

However, when I change the permalink settings to anything other than default, I get 404 errors on every post, article and page.

I understand that this is something to do with nginx not supporting .htaccess and WordPress getting confused with where to go when a page is requsted.

I've tried a few rewrites in the nginx conf files and even the nginx compatibility plugin; neither have worked. With one rewrite I managed to stop the 404 errors, but instead of WordPress finding the post I was after I merely got my PHP confirmation page. Bah.

Forums are littered with people with similar issues. Does anyone have a solution?

8条回答
太酷不给撩
2楼-- · 2019-03-19 11:28

Adding this block to your nginx.conf should solve the issue:

     if (!-e $request_filename) {
            rewrite ^/wordpress_dir/(.+)$ /wordpress_dir/index.php?q=$1 last;
     }

Hope this helps.

Good luck.

查看更多
贪生不怕死
3楼-- · 2019-03-19 11:30

On your location / block,

add this and remove any non-specific rewrite rules:

try_files $uri $uri/ /index.php;
查看更多
疯言疯语
4楼-- · 2019-03-19 11:33

This was how I solved my permalinks in my wordpress blogs in dreamhost.

Inside the folder /home/ftpusername/nginx/example.com/ (if you don't have it, create it)
created the file nginx.conf with the following content

location / {
    index index.php index.html index.htm;
    try_files $uri $uri/ /index.php?$args;
}

restarted the nginx
/etc/init.d/nginx reload

Some notes:
ftpusername and example.com MUST be changed according to your system.

That was it!
Good luck for u all.

查看更多
疯言疯语
5楼-- · 2019-03-19 11:36

Have you tried the nginx Compatibility plugin?

Plus ElasticDog seems to provide a fairly concise article on getting WP working with nginx - which includes getting pretty permalinks to work.

Here's another article that seems to deal specifically with nginx rewrite rules for WordPress.

查看更多
祖国的老花朵
6楼-- · 2019-03-19 11:37

After much pain:

# if filename doesn't exist, take the request and pass to wordpress as a paramater
         if (!-e $request_filename) {
                rewrite ^/wordpress/(.+)$ /wordpress/index.php?q=$1 last;
         }

If the requested file does not exist, pass it to index.php. It's a bit slow and I think I might try and not use a query, but it does work... :)

查看更多
Rolldiameter
7楼-- · 2019-03-19 11:40

If wordpress is on another directory besides the root, instead of having

if (!-e $request_filename) {
    rewrite ^/wordpress/(.+)$ /wordpress/index.php?q=$1 last;
}

You can have:

location /wordpress {
    try_files $uri $uri/ /wordpress/index.php?$args;
}

This page has exactly the same concept. I should have read and tried it first: nginx rewrite rule under a subdirectory

查看更多
登录 后发表回答