【问题标题】:Nginx giving 404 error for WordPress' /wp-admin/ rootNginx 为 WordPress' /wp-admin/ root 提供 404 错误
【发布时间】:2019-04-22 16:01:51
【问题描述】:

我有一个 Symfony2 应用程序与 WordPress 一起运行 - mysite.com/blog 路由到我的 /var/www/mysite/wordpress/ 目录,其他所有内容都路由到 /var/www/mysite/symfony

server {
    listen 80;
    server_name mysite.com

    location / {
        try_files $uri /app.php$is_args$args;
    }

    location ~ ^/app\.php(/|$) {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        internal;
    }

    location /blog {
        root /var/www/mysite/wordpress;
        rewrite ^/blog/(.+)$ /$1 break;
        try_files $uri $uri/ /blog/index.php?q=$uri&$args;
        index index.php;

        location ~ \.php {
            fastcgi_pass 127.0.0.1:9000;
            include fastcgi_params;
            fastcgi_split_path_info ^(?:\/blog)(.+\.php)(.*);
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
}

一切正常,除了 WordPress 管理员 (mysite.com/blog/wp-admin/) 给我一个 404 错误。访问 mysite.com/blog/wp-admin/index.php 按预期工作,所以看起来index index.php 行不工作。这可能是什么问题?

【问题讨论】:

  • 显示完整配置。我在/wp-admin/ 路径中看不到/blog/ 部分,所以这个位置与问题无关。
  • @AlexeyTen 抱歉,我应该澄清一下我正在访问 /blog/wp-admin/。我已经更新了问题。
  • wordpress index.php 在/var/www/mysite/wordpress 中吗?那么我认为 /blog 位置块中的 try_files 子句应该是 try_files $uri $uri/ /index.php?$args

标签: wordpress nginx


【解决方案1】:

您应该使用alias 而不是root 指令:

location ^~ /blog {
    alias /var/www/mysite/wordpress;
    index index.php;

    try_files $uri $uri/ /blog/index.php?q=$uri&$args;

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_split_path_info ^(?:\/blog)(.+\.php)(.*);
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

【讨论】:

  • 感谢 Valery,但我实际上无法使用别名:serverfault.com/questions/642579/… 还有其他想法吗?
  • @Jonathan,我已经用最新的 nginx/wordpress 测试了解决方案,一切都按预期工作。
  • 你说得对 - 自从我发布这个问题以来,它似乎在一年左右的时间里得到了修补。感谢您试用。
【解决方案2】:

您需要编辑 nginx 服务器配置。

# WordPress single blog rules.
# Designed to be included in any server {} block.

# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
    try_files $uri $uri/ /index.php?$args;
}

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(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 max;
}

# Uncomment one of the lines below for the appropriate caching plugin (if used).
#include global/wordpress-wp-super-cache.conf;
#include global/wordpress-w3-total-cache.conf;

# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)

    include fastcgi.conf;
    fastcgi_index index.php;
#   fastcgi_intercept_errors on;
    fastcgi_pass php;
}

更多关于这里: http://codex.wordpress.org/Nginx#General_WordPress_rules

【讨论】:

  • 感谢 Saurabh,但这不包括我的多站点设置。
【解决方案3】:

那么,如果您声称 /blog/wp-admin/index.php 有效,但 /blog/wp-admin/ 无效,也许只是有条件地附加 index.php,如果需要的话?

+    rewrite ^/blog/wp-admin(/)?$ /wp-admin/index.php break;
     rewrite ^/blog/(.+)$ /$1 break;

那么,日志对您的 404 有什么影响?我认为这可能与index 指令导致“内部重定向”这一事实有关,因此如果您的404 最终是通过/ 而不是/blog @987654330 生成的,我不会感到惊讶@。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 2012-05-31
    • 2015-08-12
    • 2017-02-04
    • 2023-03-13
    • 2021-08-13
    • 1970-01-01
    相关资源
    最近更新 更多