简体   繁体   中英

web browser is downloading index.php from my web server instead of executing it

I am setting up an nginx web server that can run PHP files, particularly for the selfoss rss viewer. For some reason, when I visit my website, it downloads the index.php file instead of executing it on the server. I am running this on a raspberry pi 1B.

Yes, I did restart nginx and clear my web browser cache. It didn't fix anything.

Here is my PHP version:

$ php -v
PHP 7.4.30 (cli) (built: Jul  7 2022 15:51:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies

Here is my nginx configuration:

server {
    server_name rss.getty.nz;
    root /var/www/rss.getty;
    access_log /var/www/rss.getty/rss.accesss.log;
    error_log /var/www/rss.getty/rss.error.log;

    index index.php;

    location / {
        try_files $uri  /public/$uri /index.php$is_args$args =404;
    }

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

    location ~* \ (gif|jpg|png) {
        expires 30d;
    }
    location ~ ^/(favicons|thumbnails)/.*$ {
        try_files $uri /data/$uri;
    }
    location ~* ^/(data\/logs|data\/sqlite|config\.ini|\.ht) {
        deny all;
    }

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/rss.getty.nz/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/rss.getty.nz/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    access_log /var/www/rss.getty/rss.accesss.log;
    error_log /var/www/rss.getty/rss.error.log;
}
server {
    if ($host = rss.getty.nz) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    listen [::]:80;
    server_name rss.getty.nz;
    return 404; # managed by Certbot
}

Here is the contents of /etc/nginx/snippets/fastcgi-php.conf:

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+?\.php)(/.*)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;

Thanks to Richard Smith in the comments, I have the answer. It was the =404 in this section:

location / {
    try_files $uri  /public/$uri /index.php$is_args$args =404;
}

Removing that stops the web browser from downloading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM