Why php files are downloaded when using php-fpm with nginx?

I have a file index.php which is in /var/www/html/index.php
When you apply for http://[ip]/index.php it is simply downloaded instead of displayed.

file nginx.conf
default_type application/octet-stream;

 #upstream apch {
 # server 127.0.0.1:8080;
#}

##
 # SSL Settings
##

 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
 ssl_prefer_server_ciphers on;

##
 # Logging Settings
##

 access_log /var/log/nginx/access.log;
 error_log /var/log/nginx/error.log;

##
 # Gzip Settings
##

 gzip on;

 # gzip_vary on;
 # gzip_proxied any;
 # gzip_comp_level 6;
 # gzip_buffers 16 8k;
 # gzip_http_version 1.1;
 # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
 # Virtual Host Configs
##

 include /etc/nginx/conf.d/*.conf;
 include /etc/nginx/sites-enabled/*;
}


#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}


file
sites-available/default

# See: https://bugs.debian.org/773332
#
 # Read up on ssl_ciphers directives to ensure a secure configuration.
 # See: https://bugs.debian.org/765782
#
 # Self signed certs generated by the ssl-cert package
 # Don't use them in a production server!
#
 # include snippets/snakeoil.conf;

 root /var/www/html;

 # Add index.php to the list if you are using PHP
 index index.php index.html index.htm index.nginx-debian.html;

 server_name _;

 location / {
 # First attempt to serve request as file, then
 # as directory, then fall back to displaying a 404.
 #proxy_pass http://apch;
 try_files $uri $uri/ =404;
}

 # pass the PHP scripts to FastCGI server
#
 location ~ \.php$ {
 include snippets/fastcgi-php.conf;
 index index.php
 # With php-fpm (or other unix sockets):
 location unix:/var/run/php/php7.2-fpm.sock;
 # With php-cgi (or other tcp sockets):
 #location 127.0.0.1:9000;
}

 # deny access to .htaccess files, if Apache''s document root
 # human towers with nginx's one
#
 #location ~ /\.ht {
 # deny all;
#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
March 23rd 20 at 18:59
3 answers
March 23rd 20 at 19:01
Solution
Long suffered then, here I forgot to write the solution in the end still worked, the config put this:

location ~ [^/]\.php(/|$) {
 fastcgi_split_path_info ^(.+?\.php)(/.*)$;
 if (!-f $document_root$fastcgi_script_name) {return 404;}
 fastcgi_param HTTP_PROXY "";
 include fastcgi_params;
 location unix:/var/run/php/php7.2-fpm.sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $request_filename;
 }

and it is important to change the file owner to www-data + dances with a tambourine and restart somehow it worked
March 23rd 20 at 19:03
The request misses the desired location configuration file sites-available/default

location ~ \.php$ {


You need to understand why.

A trivial problem is connected not with this config.

All configs are connecting from the core: nginx.conf
And it says:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;


So check these directories for the presence of other configs, or links to other configs.
March 23rd 20 at 19:05

Find more questions by tags NginxPHP