Nginx try_files prompt for download PHP - php

I'm setting up an Nginx + PHP web server on AWS Linux 2. It is a fresh install with nginx and PHP7.4 installed. Below is the virtual host config file in nginx.
I need to redirect all the traffic to index.php because it is a Single Page App.
When I go to www.xxx.com/index.php, the PHP page renders fine (so PHP is definitely running).
When I go to www.xxx.com/login/, the browser prompts for download of the index.php file instead of executing it.
Can anyone please help? (I've tried to clear my browser cache).
/etc/nginx/conf.d/myapp.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/vhosts/app.userback.io/frontend/;
index index.php index.html index.htm;
server_name www.myapp.com;
location / {
try_files $uri $uri/ /index.php =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass php-fpm;
}
}

Can you try:
location ~ \.php$ {
try_files $uri /404.html;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;// change it if not valid
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

I've found the answer here: NGINX try_files does not pass to PHP.
It turns out it is the order of the location plus removing the 404 to make the redirect internal.

Related

Nginx php files are downloading instead of execute

I'v configured Ngnix server on CentOs 7 and it is working find with html but when I'm trying to open .php file it is downloaded.
I'm using Php7.2.
Here is my configuration(I'm using 123.123.123.123 instead my real ip):
server {
listen [::]:80 default_server ipv6only=on;
listen 123.123.123.123:80 default_server;
root /var/www/example.com/public_html;
index index.php index.html;
server_name 123.123.123.123;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
So when I'm trying to access to 123.123.123.123/test/index.php I'm geting file download.
How could I force ngnix to execute php files?
You also need fastcgi_pass:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

laravel inner pages are not working on AWS EC2 Nginx

I am working on a laravel V5.1.11 site which is hosted on AWS EC2 ubuntu with ngnix server. I successfully setup the site but my inner page are not working.
Config is:
server {
listen 82;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 83;
server_name www.example.com;
root /home/in4matic/example-website-dev/public;
location / {
index index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
#fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
How can I fix that.
Moving laravel app from apache to nginx doesn't require many modification, but because laravel uses .htaccess file for url rewrite which won't work in nginx, you have to modify nginx config file so nginx can rewrite url. here is the example of my config file :
server {
listen 80;
server_name your-website.com;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000 # Keep this as per your old config;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I have been using this config for all of my laravel app.
and make sure that Storage directory has proper permission.

NGINX FASTCGI/PHP - Primary script unknown while reading response header from upstream, client

I have a development box that each user has a www folder in the home dir. NGINX is hosting those dirs from http://IP ADDRESS/USERNAME. And this works great. I want to get PHP working in the same fashion.
/etc/nginx/sites-available-default:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
#root /usr/share/nginx/html;
#index index.html index.htm;
# Make site accessible from http://IP ADDRESS/
server_name IP ADDRESS;
#location ~ ^/(.+?)(/.*)?$ {
location ~ ^/(.+?)(/.*)?$ {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
alias /home/$1/www$2;
index index.html index.htm index.php;
include /etc/nginx/php.fast.conf;
autoindex on;
}
php.fast.conf file:
location ~ \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /home/$1/www$2$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
As you can see I have tried a few variations but seem to continue to receive the following error in the log: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client.
When attempting to render a simple PHP info page The page displays "file not found"
Other info:
Server = ubuntu 14.x
Latest Nginx
Digital Ocean Droplet
Thanks in advance.
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 ~* \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $fastcgi_script_name =404;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi_params;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location ~* ^/(.+?)/www(/.*|/|)$ {
root /home;
index index.php index.html;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# 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;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 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_params;
}
location ~* ^/(.+?)(/.*|/|)$ {
index index.php index.html;
try_files $uri $uri/ #home;
}
location #home {
rewrite ^/([^/]+?)$ /$1/www/ last;
rewrite ^/(.+?)(/.*|/)$ /$1/www$2 last;
}
}
The cons of this config, you can't use URI like /<anything_you_want>/www.
Maybe it helps. But it's weird, non-optimized and ugly config.

how to make php scripts executable instead of downloadable using nginx

Recently I set up a virtual machine through vagrant and I'm using nginx as a web server. The OS I'm using for the VM is Ubuntu Linux 12.04. The problem is that any script I write in PHP gets downloaded, instead of being executed on the browser. Seeing as the PHP interpreter is normally installed I figured I'd post here the config file of nginx, as this is where the problem is likely to be found. Being a novice in the world of web development, I can't figure out what is out of the ordinary, can you guys take a look and tell me if you see something wrong? Thanks.
server {
listen 80;
server_name localhost;
root /vagrant/www/web;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
#strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location / {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# pass the PHP scripts to FastCGI server listening socket
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_pass unix://var/run/php5-fpm.sock;
fastcgi_keep_conn on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
# enable global phpMyAdmin
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix://var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
}
Looks like you're having double // to indicate the fastcgi_pass path of php in your location block, try this instead
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_keep_conn on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
As was my suspicion the problem was in this location block:
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_pass unix://var/run/php5-fpm.sock;
fastcgi_keep_conn on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
The parameter to the location directive is a regular expression that defines only two possible file names: app.php and app_dev.php, thus making any other filename non-executable. For this to change, one needs to either add the names of one's php scripts to the parameter or change the regular expression to a more inclusive form as per Frederic Henri's suggestion.

NGINX shows script code instead of execute

I have a troble with NGINX that makes me insane. I have Ubuntu 12.04.3 LTS on VirtualBox. I installed NGINX-MYSQL-PHP following these instructions:
install php5-fpm;
set cgi.fix_pathinfo=0 in /etc/php5/fpm/php.ini;
set listen = var/run/php5-fpm.sock in /etc/php5/fpm/pool.d/www.conf
Then I set in /etc/nginx/sites-available/default:
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Both localhost and info.php are executed properly. Now i want to set up a local website and try to make some redirect. So I created a new folder in /usr/share/nginx/www called mywebsite, put a index.php inside with a echo instruction, a php script i make redirect to, added a new config file in sites-available and a link to that in sites-enabled, and disable application/octet-stream in nginx.conf in order to avoid to downloading the script file. So i set my new config file as following:
server {
listen 80;
root /usr/share/nginx/www/mysite;
index index.php index.html index.htm;
server_name www.mysite.com;
location / {
try_files $uri $uri/ /index.html;
if (!-e $request_filename){
rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/$ /senseddata.php break;
rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/lasthour/$ /senseddata.php break;
rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/daily/$ /senseddata.php break;
}
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
try_files $uri =404;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
So what happened after all? Index.php is executed properly. Rewrite is done well but the script is not executed. I only see the entire code i wrote displayed in the browser. What's the problem? Thanks in advance.
Also check the opening tags in your PHP-files. If you use <? instead of <?php and the short tag is not available, the PHP-code will not be parsed and you will see the source.
Try add fastcgi_split_path_info ^(.+\.php)(/.+)$; to location ~ \.php$ and restart nginx.
Example from my config
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Categories