Index and root pages are not working, 502 Bad Gateway

Viewed 364

The URL example.org/index.php (and any example.org/etc.php) fail, but example.org/foo working fine, for any foo folder.

server {
        server_name example.org example.com;
        access_log /var/log/nginx/example.org.access_log;
        root /var/www/example.org/;
        index  index.php index.html index.htm;

        location / {
          try_files $uri $uri/ =403;
        }

        location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
}

  • nginx version: 1.17.10 (Ubuntu)
  • ? /var/log/nginx/error.log (no specific for the server?)

sudo tail /var/log/nginx/error.log show nothing, only errors of other site,

...
2020/06/19 12:58:02 [error] 871296#871296: *23 "/var/www/example2.org/index2.php" is not found (2: No such file or directory), client: 181.177.112.xx, server: example2.org, request: "GET / HTTP/1.0", host: "example2.org"

MAIN CLUES TO THE SOLUTION (see my answer)

The file /run/php/php7.0-fpm.sock not exists, so the fail is at fastcgi_pass unix:/run/php/php7.0-fpm.sock;.

ls -l /run/php/*.sock shows

lrwxrwxrwx 1 root     root     30 May 17 05:18 /run/php/php-fpm.sock -> /etc/alternatives/php-fpm.sock
srw-rw---- 1 www-data www-data  0 May 29 06:40 /run/php/php7.4-fpm.sock

Ideal is a generic socket, that is php-fpm.sock, but it is not seems the same (not redirect to php7.4-fpm.sock?).

3 Answers

Ideal is a generic socket, but at now using the php7.4-fpm.sock:

server {
        server_name example.org example.com;
        ...
        location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        }
}

This solution is not perfect (big problem when PHP changes version) but is working fine.

If you're looking for a generic solution so you don't have to worry about the PHP version you could try:

server {
        server_name example.org example.com;
        ...
        location ~ \.php$ {
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include fastcgi_params;
        }
}

And check that in the file /etc/php/7.*/fpm/php.ini you have written the following line:

cgi.fix_pathinfo=0

Try this one (adapt to your needs):

server {
    listen 80;
    server_name example.org;
    root /var/www/example.org; //If Laravel /var/www/example.org/public
         
    index index.html index.htm index.php etc.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/example.org-error.log error;

    error_page 404 /index.php;

    sendfile off;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /\.ht {
        deny all;
    }
    
    client_max_body_size 1000m;
}

Notes:

  1. The server_name (doc) variable holds the value example.org. This value must exists in the hosts file. Something like this 127.0.0.1 example.org.
  2. In the root variable the NginX is expecting the path where it will find the entry point of your system /var/www/example.org/. Here example.org is the file name for your project. If your entry point file is index.php or etc.php you must confirm that these files exists inside your root. Something like this: /var/www/example.org/index.php or /var/www/example.org/etc.php. If you make your entry point inside another folder it will not work. Example if you setup the variable root to /var/www/example.org/public (like Laravel) you should have your entry point file inside public folder, not inside example.org folder
  3. The index variable is a list of possible entry point files in your root that NginX is expecting to find. In your case you should have something like this: index index.html index.htm index.php etc.php.
  4. The location for the php. If you are setting your NginX variable to /var/run/php/php7.0-fpm.sock you must confirm that the file php7.0-fpm.sock already exists inside /var/run/php/
  5. I put client_max_body_size, fastcgi_buffer_size, fastcgi_buffers if you want to perform an upload process to your server. If not, these variables are not necessaries.

And finally the error:

  1. example2.org/index2.php" is not found. It seems that NginX is expecting to find index2.php entry point file inside the root folder example2.org/. Make sure that you have this file there.
  2. I did not understand if you have php 7.0 or 7.4. In any case, You must pass the location of yours php7.x-fpm files. Just go there in the folder and confirm that they already exists.
  3. And finally another possible error that you may face is permissions. Make sure that www-data (the NginX user) has appropriate permissions WRX to your files.
  4. Here you will find how NginX process a request.
  5. And here a compreensive list of variables used by NginX.
Related