Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
1 of 3

nginx - cURL retrieves PHP files but accessing via web browser gives source code

I'm having some trouble with nginx. I setup nginx on a Raspberry Pi B+ (Raspbian Jessie) and PHP with FastCGI. When I try using cURL to retrieve pages, it returns the PHP-generated HTML.

nginx server block that serves fastcgi requests

server { listen 80 default_server; listen [::]:80 ipv6only=on default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /usr/share/nginx/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 the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { # include snippets/fastcgi-php.conf; include fastcgi_params; try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; # # # 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; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } # deny access to .htaccess files, if Apache's document root #oncurs with nginx's one # #location ~ /\.ht { # deny all; #} } 

PHP FILE

<?php echo "PHP Test"; ?> 

Browser Response: Either downloads it (chrome) or displays it as source code (other browsers)

cURL:

  • Hostname was NOT found in DNS cache
  • Trying ::1...
  • Connected to localhost (::1) port 80 (#0)

GET /test_2.php HTTP/1.1 User-Agent: curl/7.38.0 Host: localhost Accept: /

< HTTP/1.1 200 OK

  • Server nginx is not blacklisted < Server: nginx < Date: Fri, 12 May 2017 04:21:20 GMT < Content-Type: text/html; charset=UTF-8 < Transfer-Encoding: chunked < Connection: keep-alive < PHP Test
  • Connection #0 to host localhost left intact