nginx and php-fpm configuration

elsparkodiablo

Active member
I am simply trying to move an XF 2 installation and use nginx instead of Apache.

Following the link here:
https://www.nginx.com/resources/wiki/start/topics/recipes/xenforo/

I configure nginx to pass off to php-fpm when it encounters a php file. However, using this directive:

location / {​
try_files $uri $uri/ /index.php?$uri&$args;​
}​

The php is never sent to php-fpm with fastcgi_pass, but instead the index.php file is served up as plain text. I've seen that this location directive is required with $uri&$args to allow Xenforo to work. Is there an updated page anywhere that suggests a different approach?

Any help would be greatly appreciated. Thanks!


Code:
    server {
        listen        80;
        listen        [::]:80;
        server_name   dev.mysite.com;
        #listen       443 ssl http2;
        #listen       [::]:443 ssl http2;
        root         /usr/share/nginx/mysite.com;
        index        index.php index.html;

        error_log /var/log/nginx/mysite-error.log info;

        location ~ / {
           try_files $uri $uri/ /index.php?$uri&$args;
        }

        location ~ \.php$ {
           fastcgi_split_path_info ^(.+?\.php)(/.*)$;
           if (!-f $document_root$fastcgi_script_name) {
               return 404;
           }

           # Mitigate https://httpoxy.org/ vulnerabilities
           fastcgi_param HTTP_PROXY "";

           fastcgi_pass    unix:/run/php-fpm/www.sock;
           fastcgi_index index.php;
           include         fastcgi_params;
        }

    }
 
Code:
# SCRIPT_FILENAME parameter is used for PHP FPM determining
    #  the script name. If it is not set in fastcgi_params file,
    # i.e. /etc/nginx/fastcgi_params or in the parent contexts,
    # please comment off following line:
    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
I think you missed a line.

try something like;
Code:
location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    fastcgi_param HTTP_PROXY "";
    fastcgi_pass unix:/run/php-fpm/www.sock; #check it's working
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

full:

Code:
server
{
  listen 80;
  listen [::]:80;
  server_name dev.mysite.com;
  root /usr/share/nginx/mysite.com;
  index index.php index.html;
  error_log /var/log/nginx/mysite-error.log info;

   #  files
   location ~ / {
        try_files $uri $uri/ /index.php?$uri&$args;
   }
   
   # security
   location ~ /(internal_data|library) {
        internal;
   }
   
   # php
   location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
        return 404;
        }
        fastcgi_param HTTP_PROXY "";
        fastcgi_pass unix:/run/php-fpm/www.sock; #check it's working
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
  }
}
 
Last edited:
Top Bottom