Solution to so many redirects error executing independent PHP script in a Drupal 8 site running under nginx

The issue is when executing a standalone PHP script in a Drupal 8 site running under nginx gets "The page isn't redirecting properly" error in Firefox and getting this error in Chrome: "This page isn't working [domain] redirected you too many times." but the PHP scripts that executes under Drupal 8 bootstrap don't get this issue. The bug must be on nginx webserver configuration. Investigating the nginx configuration file php_pass.conf that handles my Drupal 8 site PHP scripts:

  
## Fastcgi configuration

## 1. Parameters.
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_NAME /index.php;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
## PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param HTTP_HTTPS $https;
fastcgi_param HTTPS $https;
fastcgi_pass $phppass;

## 2. Nginx FCGI specific directives.
fastcgi_buffers 256 4k;
fastcgi_intercept_errors on;
## Allow 4 hrs - pass timeout responsibility to upstream.
fastcgi_read_timeout 14400;
fastcgi_index index.php;
## Hide the X-Drupal-Cache header provided by Pressflow.
fastcgi_hide_header 'X-Drupal-Cache';
## Hide the Drupal 7 header X-Generator.
fastcgi_hide_header 'X-Generator';
  

The configuration shows that all the PHP scripts always calls the Drupal bootstrap even those standalone scripts that don't need it. The solution is to create separate nginx configuration for standalone PHP scripts. We can still call the nginx configuration above which is php_pass.conf and just append the commands that force to always call the Drupal bootstrap to override. We can now have:

  
location ~* ^.+\.php$ {
  include php_pass.conf;
  ## Bypass Drupal bootstrap
  fastcgi_param PATH_INFO $fastcgi_path_info;
  fastcgi_param SCRIPT_FILENAME $request_filename;
  fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
}
  

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.