0

My nginx virtual host fails to serve PHP files. Instead, it downloads the index.php file when run in a browser. Here's my config:

/etc/nginx/sites-available/laravel.local

server {
    listen 80;
    server_name laravel.local www.laravel.local;
    root /var/www/laravel.local/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.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; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

/etc/nginx/sites-available/default

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        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.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
           fastcgi_pass unix:/run/php/php8.1-fpm.sock;
           include snippets/fastcgi-php.conf;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
}

Other virtual hosts are working when serving static .html files. It's when I try serving PHP ones, then the problems start.

enter image description here

1
  • did you restart nginx? is php-fpm running and is /run/php/php8.1-fpm.sock existing? and Moreover what is the business related issue in here?
    – djdomi
    Sep 24 at 15:47

1 Answer 1

-1

It appears to be a browser issue particularly Chrome. Chrome doesn't like serving documents over HTTP it's cries HTTPS only and downloads rather than serving docs. Even following a browser cache refresh. Proof, it works in Firefox for example.

enter image description here

Not the answer you're looking for? Browse other questions tagged .