【问题标题】:NGINX wordpress 404 for all subpages所有子页面的 NGINX wordpress 404
【发布时间】:2017-09-26 23:59:37
【问题描述】:

我有两个问题,但我认为它们是相关的。 /wp-admin/blog 之类的子目录返回 404,因此,永久链接在 /blog/category1/page.php 之后不起作用

我的设置:

我有一台运行 nginx 的服务器 192.168.1.4。在另一台服务器上,192.168.1.1 我有一个使用虚拟主机的 apache 网络服务器,它托管我的 wordpress 网站。没有 nginx 的设置可以正常工作,但是当我打开 nginx 时,我遇到了一些问题。

Nginx 不适用于永久链接。我使用了默认值,所以现在它就像:http://www.mywebsite.co.uk/?page_id=90 可以正常工作(只要它不在子目录中)。

子目录(不是根目录)中的所有内容都会中断。包括管理页面http://www.mywebsite.co.uk/wp-admin,或(在关闭永久链接之前)http://www.mywebsite.co.uk/blog。他们都去404,具体来说:404 Not Found nginx/1.4.6 (Ubuntu)

这是我的配置:

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
   }
}

作为参考,这是我的永久链接:/blog/%category%/%postname%/

更新

尝试将其添加到我的配置中:

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; 
    }
}

这会在日志中返回一个错误:

rewrite or internal redirection cycle while internally redirecting to "/wp-admin/index.php", client: xxxxx, server: mydomain.co.uk`

【问题讨论】:

  • try_files 的第三条路径假定最终移出,但这条路径又回到了这个location
  • @Deadooshka 是的,你可能是对的。但这是许多帖子建议解决我遇到的问题的方法
  • index 还从location 进行内部重定向

标签: php wordpress nginx reverse-proxy virtualhost


【解决方案1】:

你有一些工作配置。 nginx 在您的配置中的目的是反向代理到 Apache 服务器。 indextry_files 在这种情况下是不合适的。试试:

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;
   }
}

【讨论】:

  • 我确定我试过了,但它确实有效...非常感谢!
  • 请问192.168.1.1 来自哪里?
【解决方案2】:

为什么你不想在没有 apache 的情况下尝试 wordpress? 工作配置是这样的:

server {
    listen 80;    
    server_name site.com;
    root /home/www/site.com;

    access_log /var/log/nginx/log;
    error_log /var/log/nginx/error.log;

    index index.php;

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

    # SECURITY : Deny all attempts to access PHP Files in the uploads directory
    location ~* /(?:uploads|files)/.*\.php$ {
        deny all;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm/php.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    }

    location = /favicon.ico { log_not_found off; access_log off; }

    location = /robots.txt {allow all; log_not_found off; access_log off; }

    error_page 404 /404.html;

    location ~ /\. { deny all; }

    location ~* ^.+\.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off; 
        log_not_found off; 
        expires 30d;
    }
}

【讨论】:

    【解决方案3】:

    我发现问题在于未启用 mod_rewrite。我必须使用 apache 在服务器上运行 a2enmod rewrite

    【讨论】:

      【解决方案4】:

      【讨论】:

        猜你喜欢
        • 2018-05-10
        • 2014-09-12
        • 2021-06-05
        • 2018-04-28
        • 1970-01-01
        • 2011-07-08
        • 1970-01-01
        • 2014-08-13
        • 2019-06-30
        相关资源
        最近更新 更多