Warm tip: This article is reproduced from serverfault.com, please click

NGINX wordpress 404 for all subpages

发布于 2017-04-28 19:17:41

I have two problems but I think they're related. Subdirectories like /wp-admin, /blog return 404, consequently, permalinks do not work as they follow /blog/category1/page.php

My setup:

I have a server 192.168.1.4 running nginx. On another server, 192.168.1.1 I have an apache web server using virtualhosts which is hosting my wordpress site. The setup works fine without nginx, but when I turn nginx on I have a few issues.

Nginx will not work with permalinks. I have used default, so now its like: http://www.mywebsite.co.uk/?page_id=90 which works fine (as long as its not in subdirectory).

Everything in subdirectory (not in root) breaks. Including admin pages http://www.mywebsite.co.uk/wp-admin, or (before turning off permalinks) http://www.mywebsite.co.uk/blog. They all go to 404, specifically: 404 Not Found nginx/1.4.6 (Ubuntu)

Here is my config:

server {
   server_name mywebsite.co.uk www.mywebsite.co.uk;
   location / {
        index index.php;
        proxy_pass http://192.168.1.1$request_uri;

        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;

        try_files $uri $uri/ =404;
        #try_files $uri $uri/ /index.php?$args; #not working, error: rewrute or internal redirection cycle while interally redirecting to index.php
   }
}

For reference, here is my permalinks: /blog/%category%/%postname%/

Update

Tried adding this to my config:

server {
    ... config above ...
   location /wp-admin/ {
       index index.php
       try_files $uri $uri/ /wp-admin/index.php?$args;
       proxy_pass http://192.168.1.1$request_uri; 
       proxy_set_header Host $host; 
    }
}

this retrns an error in log:

rewrite or internal redirection cycle while internally redirecting to "/wp-admin/index.php", client: xxxxx, server: mydomain.co.uk`
Questioner
boywonder
Viewed
0
Richard Smith 2017-04-29 04:25:29

You have bits of a working configuration. The purpose of nginx in your configuration, is to reverse proxy to the Apache server. The index and try_files are inappropriate in this case. Try:

server {
    server_name mywebsite.co.uk www.mywebsite.co.uk;
    location / {
        proxy_pass http://192.168.1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
   }
}