Home >Backend Development >PHP Tutorial >Why is Nginx Downloading .php Files Instead of Executing Them, and How Can I Fix It?

Why is Nginx Downloading .php Files Instead of Executing Them, and How Can I Fix It?

DDD
DDDOriginal
2024-12-05 02:37:091043browse

Why is Nginx Downloading .php Files Instead of Executing Them, and How Can I Fix It?

Nginx Serves .php Files as Downloads Instead of Executing Them

In this scenario, Nginx incorrectly handles .php files, downloading them rather than executing them. To rectify this issue, certain configuration adjustments must be made.

1. Uncomment Listen Lines:

Modify /etc/nginx/sites-available/default to enable Nginx to listen on both IPv4 and IPv6 port 80:

listen 80; ## listen for ipv4;
listen [::]:80 default_server ipv6only=on; ## listen for ipv6

2. Set Server Name:

Ensure server_name reflects the actual server name, such as server_name example.com;.

3. Add index.php to Index Line:

In the root directive, add index.php to the list of index files:

root /var/www/html;
index index.php index.html index.htm;

4. Uncomment PHP Location Block:

Uncomment the location ~ .php$ {} block to enable PHP processing:

location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+?\.php)(/.+)?$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
}

5. Disable Pathinfo Fix in php.ini:

Edit /etc/php5/fpm/php.ini and set cgi.fix_pathinfo = 0.

6. Restart Nginx and PHP-FPM:

Restart Nginx and PHP-FPM:

sudo service nginx restart && sudo service php5-fpm restart

Revised Configuration:

server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/html;
        index index.php index.html index.htm;

        # Make site accessible from http://example.com/
        server_name example.com;

               location ~ \.php$ {
                    try_files $uri $uri/ =404;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                    #
                    #               # With php5-cgi alone:
                    #               fastcgi_pass 127.0.0.1:9000;
                    #               # With php5-fpm:
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
            }
  

              location / {
                    
                    try_files $uri $uri/ =404;
                    # Uncomment to enable naxsi on this location
                    # include /etc/nginx/naxsi.rules
            }

After implementing these changes, Nginx should correctly execute .php files.

The above is the detailed content of Why is Nginx Downloading .php Files Instead of Executing Them, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn