Nginx non-standard URLs in PHP

Apperantly some PHP powered applications (WordPress, Davical…) make use of URLs that look like this: http://youserver.com/script.php/extra-info. The /extra-info part doesn’t work with most default webserver+PHP configurations that I’ve come across. Including Nginx.

To make URL like that work with PHP-FPM+Nginx you can put this part in your Nginx site configuration file:

location ~ ^(.+\.php)(.*)$ {
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_pass unix:/var/run/php5-fpm.sock;

  fastcgi_split_path_info ^(.+\.php)(.*)$;
  fastcgi_param PATH_INFO $fastcgi_path_info;
  fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

  fastcgi_read_timeout 180;
  fastcgi_buffers 4 256k;
  fastcgi_buffer_size 128k;
}

location ~ \.php {
  try_files $uri =404;
  fastcgi_pass unix:/var/run/php5-fpm.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}

For Debian user who has installed Nginx via apt-get. The site configuration files can be found here: /etc/nginx/sites-enabled

This a functional site config:

server {
listen 80; ## listen for ipv4; this line is default and implied
 listen [::]:80 default_server ipv6only=on; ## listen for ipv6

server_name yourserver.se;
 root /var/www;

  index index.php index.html index.htm;

  location ~ ^(.+\.php)(.*)$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php5-fpm.sock;

    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

    fastcgi_read_timeout 180;
    fastcgi_buffers 4 256k;
    fastcgi_buffer_size 128k;
  }

  location ~ \.php {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *