请求 wp-admin/admin-ajax.php return 404 错误
Requests to wp-admin/admin-ajax.php return 404 error
我已经从 Apache HTTP Server 切换到 Nginx,现在我正在使用的 WordPress 插件在使用正确注册的操作向 wp-admin/admin-ajax.php 发送请求时收到 404 错误响应:
https://projectname.com/wp-admin/admin-ajax.php/?action=my-ajax-action
PHP 和 Nginx 日志中没有错误。此外,当在 wp-admin/admin-ajax.php 文件的顶部添加断点时,Xdebug 不会停止。所以这个文件好像根本就没有被调用。
这是我的 Nginx 配置(它实际上是一个 docker 容器,前面有一个 Apache HTTP 服务器作为反向代理服务器):
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
index index.php index.html;
root /var/www/html;
server_name localhost;
#SSL
ssl_certificate /etc/ssl/certs/self-signed.crt;
ssl_certificate_key /etc/ssl/private/self-signed.key;
include ssl/ssl.conf;
#include snippets/snakeoil.conf;
error_log /var/log/nginx/https_error.log;
access_log /var/log/nginx/https_access.log;
add_header scheme $scheme;
add_header host $host;
add_header uri $uri;
location / {
try_files $uri $uri/ /index.php?$args;
#try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
expires -1;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass backend:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
#include global/fastcgi_optimize.conf;
}
# 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 -1;
}
include global/compression.conf;
}
知道什么会导致 404 响应吗?
它 returns 404 因为没有匹配这个 URL 的块。 PHP CGI 处理程序参数存在于第二个块中:
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass backend:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
#include global/fastcgi_optimize.conf;
}
此块匹配任何以 .php 结尾的位置(与您提供的 URL 不同)。所以你只需要像这样稍微修改正则表达式:
location ~ (.*\.php)(/.*)$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass backend:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
#include global/fastcgi_optimize.conf;
}
这也将匹配 .php 后跟任意数量字符的斜线。这应该与您的 URL.
匹配
所以,admin-ajax.php 之后的尾部斜杠是问题所在。
我刚刚在服务器级别专门为 wp-admin/admin-ajax.php/ 添加了一个重写规则,现在一切正常:
rewrite ^/wp-admin/admin-ajax.php/(.*)$ /wp-admin/admin-ajax.php;
虽然位置块中的工作正则表达式(或 fastcgi_split_path_info ? )会更优雅。
我已经从 Apache HTTP Server 切换到 Nginx,现在我正在使用的 WordPress 插件在使用正确注册的操作向 wp-admin/admin-ajax.php 发送请求时收到 404 错误响应:
https://projectname.com/wp-admin/admin-ajax.php/?action=my-ajax-action
PHP 和 Nginx 日志中没有错误。此外,当在 wp-admin/admin-ajax.php 文件的顶部添加断点时,Xdebug 不会停止。所以这个文件好像根本就没有被调用。
这是我的 Nginx 配置(它实际上是一个 docker 容器,前面有一个 Apache HTTP 服务器作为反向代理服务器):
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
index index.php index.html;
root /var/www/html;
server_name localhost;
#SSL
ssl_certificate /etc/ssl/certs/self-signed.crt;
ssl_certificate_key /etc/ssl/private/self-signed.key;
include ssl/ssl.conf;
#include snippets/snakeoil.conf;
error_log /var/log/nginx/https_error.log;
access_log /var/log/nginx/https_access.log;
add_header scheme $scheme;
add_header host $host;
add_header uri $uri;
location / {
try_files $uri $uri/ /index.php?$args;
#try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
expires -1;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass backend:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
#include global/fastcgi_optimize.conf;
}
# 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 -1;
}
include global/compression.conf;
}
知道什么会导致 404 响应吗?
它 returns 404 因为没有匹配这个 URL 的块。 PHP CGI 处理程序参数存在于第二个块中:
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass backend:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
#include global/fastcgi_optimize.conf;
}
此块匹配任何以 .php 结尾的位置(与您提供的 URL 不同)。所以你只需要像这样稍微修改正则表达式:
location ~ (.*\.php)(/.*)$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass backend:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
#include global/fastcgi_optimize.conf;
}
这也将匹配 .php 后跟任意数量字符的斜线。这应该与您的 URL.
匹配所以,admin-ajax.php 之后的尾部斜杠是问题所在。 我刚刚在服务器级别专门为 wp-admin/admin-ajax.php/ 添加了一个重写规则,现在一切正常:
rewrite ^/wp-admin/admin-ajax.php/(.*)$ /wp-admin/admin-ajax.php;
虽然位置块中的工作正则表达式(或 fastcgi_split_path_info ? )会更优雅。