skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. It returns 404 because there’s no block that matches this URL. The PHP CGI handler parameters exist in this second block:

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

    This block matches any location ending with .php (not the case with the URL you provided). So you only need to modify the regex a little like this:

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

    This will also match a slash followed by any number of characters after .php. This should match your URL.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search