Search code examples
phpwordpressnginxwp-admin

Requests to wp-admin/admin-ajax.php return 404 error


I've switched from Apache HTTP Server to Nginx and now the WordPress plugin I'm working on gets 404 error response when sending requests to wp-admin/admin-ajax.php with correctly registered actions:

https://projectname.com/wp-admin/admin-ajax.php/?action=my-ajax-action

There are no errors in the PHP and Nginx logs. Also when adding a breakpoint right on the top of the wp-admin/admin-ajax.php file, Xdebug doesn't stop. So the file seems not to be called at all.

Here is my Nginx config (it's actually a docker container with an Apache HTTP Server as a reverse proxy server in front):

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;

}

Any ideas what could cause the 404 response?


Solution

  • So, the trailing slash after admin-ajax.php was the issue. I just added a rewrite rule specifically for wp-admin/admin-ajax.php/ on the server level and everything works fine now:

    rewrite  ^/wp-admin/admin-ajax.php/(.*)$  /wp-admin/admin-ajax.php$1;
    

    Though a working regex in the location block (or fastcgi_split_path_info ? ) would be far more elegant.