Nginx 重写规则核心 PHP + Wordpress | 无法登录 WP-Admin 面板

Nginx 重写规则核心 PHP + Wordpress | 无法登录 WP-Admin 面板

我有一个用核心 PHP 开发的静态网站(没有使用框架),并且还在同一域下的子目录中使用 wordpress 安装了一个博客。

Like : example.com/blog

由于主站点是使用静态 php 文件构建的,根据 SEO 建议,我编写了一个简单的重写规则来从 URL 中删除 .php 扩展名。

所以

http://example.com/abc.php will rewrite to http://example.com/abc

现在来谈谈实际问题。

当我尝试登录时http://example.com/blog/wp-admin并输入用户名和密码,它会重定向 wp-login.php,但由于上述重写规则,它会更改为 wp-login 并导致主站点上找不到重定向 404 页面。

重写规则Nginx vhost-conf为:

location ~ \.php$ {
    if ($request_uri ~ (.*)\.php$) {
        return 301 $1;
    }

    try_files $uri =404;

    expires off;

    fastcgi_read_timeout 900s;
    fastcgi_index index.php;
    fastcgi_intercept_errors on;
    fastcgi_pass 127.0.0.1:9001;

我知道这可能不是最好的办法但是我已经尝试了 stackexchange 上几乎所有的答案几个小时,最后才使用上面的方法解决了这个问题。

我的问题是如何将 wp-admin 排除在此类重定向之外?或者有人可以建议其他好的重定向/重写规则来避免此类冲突?

完成 Nginx 配置:

server {
    listen 80; # Default listen port
    if ($host = www.example.com) {
        rewrite ^/(.*)$ http://example.com/$1 permanent;
    }
    server_name example.com www.example.com;
    root  /home/example.com/public_html/;
    index index.php index.html;
    access_log /var/log/nginx/skpat77-access.log;
    error_log /var/log/nginx/skpat77-error.log;
    gzip on; # Turn on gZip
    gzip_static on;
    gzip_comp_level 9;
    gzip_min_length  1400;
    gzip_vary  on;
    gzip_types  text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    try_files $uri $uri/ /index.php?$args;


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

    location ~ \.php$ {
        if ($request_uri ~ (.*)\.php$) {
            return 301 $1;
        }

        try_files $uri =404;

        expires off;

        fastcgi_read_timeout 900s;
        fastcgi_index index.php;
        fastcgi_intercept_errors on;
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

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

谢谢!

答案1

尝试以下配置:

server {
    # Handle redirects to www.example.com
    server_name example.com;
    return 301 http://www.example.com$uri;
}

server {
    listen 80; # Default listen port
    server_name www.example.com;
    root  /home/example.com/public_html/;
    index index.php index.html;
    access_log /var/log/nginx/skpat77-access.log;
    error_log /var/log/nginx/skpat77-error.log;
    gzip on; # Turn on gZip
    gzip_static on;
    gzip_comp_level 9;
    gzip_min_length  1400;
    gzip_vary  on;
    gzip_types  text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    # Add .php extension to list of files to go through before trying WP
    try_files $uri $uri.php $uri/ /index.php?$args;


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

    location ~ \.php$ {
        if ($request_uri ~ (.*)\.php$) {
            return 301 $1;
        }

        try_files $uri =404;

        expires off;

        fastcgi_read_timeout 900s;
        fastcgi_index index.php;
        fastcgi_intercept_errors on;
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

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

但是,这种方法的问题在于可能会产生其他副作用。我建议您使用带扩展名的原始 .php 文件,或者在您自己的原始 PHP 文件中实现与 Wordpress 中类似的友好 URL 系统。

或者更好的是,在 WP 中实现您自己的 PHP 文件。

答案2

我自己也在调查这个问题 - 同时我发现我可以使用 /blog/index.php/wp-admin/ 而不是 /blog/wp-admin/ 登录

这并非解决方案,而是重新配置过程中的一种缓解措施

相关内容