Nginx not loading index.php - php

I'm using nginx and php5-fpm in my ubuntu pc, my site is not loading in browser i have configured everything, but i'm getting 500 internal server error in browser console when i was run my index.php.
This is my code (etc/nginx/sites-available/default)
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html/Inwiter;
index index.php index.htm index.html;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php;
#try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
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;
}
}

Few days back i also faced this kind of issue (not exactly this issue) that time i was added allow accessToken in my configuration file then it was fine for me.
add_header Access-Control-Allow-Origin *;

As the log you provided, nginx still try to use fastcgi://127.0.0.1:9000 as fastcgi_pass .
Did you restart nginx or reload configuration after modification ?
Besides, please check configuration file in /etc/php5/fpm/pool.d/.
There must be a line with listen = /var/run/php5-fpm.sock.
I recommend you to remove ipv6only=on , and try following sample configuraion:
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_intercept_errors on;
try_files $uri =404;
}

Related

nginx trying to specify different locations not the main root

I have a config that handles different "websites" a main root handles the requests for the main url and I specify a specific path for it, in another location /mailtracker I handle a different app and point it to a different directory. My problem is php-fpm is not finding this second location.
Basically I want to have several locations pointing to different roots.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/sites/dorero/;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
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
}
location /mailtracker {
alias /var/www/sites/mailtracker/web/;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 192.168.10.3:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
location ~ ^/(index|app|app_dev|config)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
fastcgi_pass 192.168.10.3:9000;
# # With php5-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
I tryied specifying instead of $document_root the path to the alias /var/www/sites/mailtracker/web/ and it didn't work.
Output from the php-fpm to this config says:
192.168.10.4 - 06/Feb/2018:19:04:49 +0000 "GET /mailtracker/index.php" 404
Put location /mailtracker above location / because nginx matched route /mailtracker for / before checking /mailtracker
You can remove location ~ \.php$ inside location /mailtracker
Add try_files $uri $uri/ =404; to location /mailtracker
edit:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/sites;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location /mailtracker {
alias /var/www/sites/mailtracker/web;
set $subfolder "mailtracker/web";
try_files $uri #rewriteapp;
}
location / {
root /var/www/sites/dorero;
set $subfolder "dorero";
# 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
}
location ~ ^/(index|app|app_dev|config)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
fastcgi_pass 192.168.10.3:9000;
# # With php5-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$subfolder/$fastcgi_script_name;
}
}

NGINX downloads PHP file instead of showing

I'm trying to display index.php and info.php on my NGINX webserver but i cant get it working for some reason, my browser keeps downloading the files instead of displaying them. I tried alot of tutorials but i dont know whats wrong.
OS: Ubuntu 17.10 Server
PHP version: 7.1
cgi.fix_pathinfo is set to 0
www.conf file is default except de cgi.fix_pathinfo
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 nginx1.domain.com;
location / {
try_files $uri $uri/ =404;
}
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/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
#location ~ /\.ht {
# deny all;
#}
}
Can someone please help me?
Please try this smaller example.
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
And double check if your php-fpm is running:
sudo systemctl status php7.1-fpm.service

Laravel 5.4 Nginx 1.10 PHP 7 route except "/" are returning 404

This is my config
server {
listen 80;
listen [::]:80;
server_name mysite.com;
root /var/www/site/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files \$uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
}
The root of my site is accessible, all other routes are returning a 404 error.
Thanks in advance.
Cheers.
A few changes ... I run php 7.1 ...
location... try /index.php?$query_string; as in the example below...
server {
listen 80;
listen [::]:80;
server_name mysite.com;
root /var/www/site/public;
index index.php index.html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Make sure your PHP-FPM runs same user as nginx
i.e. sudo vim /etc/php/7.1/fpm/pool.d/www.conf
...
user = nginx
group = nginx
...
listen.owner = nginx
listen.group = nginx
...
Then sudo systemctl restart php7.1-fpm.service
This is my configuration.
server {
listen 80;
root /home/user/projects/apple/public;
index index.php index.html index.htm;
server_name apple.dev;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I am not sure what the difference is, but I deleted the config file and made a copy of the default config, made changes to the lines I needed to. (i.e. server_name, root)
And it started working.

Nginx, default server block cannot see php files - but sub domain can

I'm not great with Nginx yet, so I would really appreciate a little bit of help here.
Now my problem is, my default server block domain.com, has all php files denied access to them.
At www.domain.com/index.php I get the correct 403 Forbidden page shown.
But if I head to www.SomeRandomSubDomain.domain.com/index.php I can see the file just fine.
I don't have any other server blocks. Am I missing a little tag of some sort in my first line below?
location ~ \.php$ {
deny all;
try_files $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 127.0.0.1:9000;
fastcgi_index index.php;
#include fastcgi_params;
include fastcgi.conf;
}
Please let me know if you need any other information to further assist me, this has kind of left me in a world of confusion, I didn't get lucky with Google.
Here's the full file:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name EXAMPLE.com www.EXAMPLE.com;
root /var/www/html;
index index.html index.php index.htm;
location / {
try_files $uri $uri/ $uri.html $uri.php?$query_string;
# Uncomment to enable naxsi on this location include
# /etc/nginx/naxsi.rules
}
location ~ .(css|img)/(.+)$ {
try_files $uri $uri/ /$1/$2;
}
error_page 404 403 /404.php;
location ~ \.php$ {
deny all;
try_files $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 127.0.0.1:9000;
fastcgi_index index.php;
#include fastcgi_params;
include fastcgi.conf;
}
}
try to remove deny all; from php configuration

Apache style "MutliViews" on nginx with php

I've looks up and down and, while this has been answered dozens of times, I can't get it to work. I'm trying to get apache style multiviews on my PHP site running under nginx. In this case I don't care about all file extensions, just php. So i have my try_files directive:
try_files $uri $uri.php $uri/ $1?$args $1.php?$args
which is all good and dandy, except that when I visit a PHP page without the PHP file extension, the PHP doesn't get rendered and just gets dumped straight to the browser. I see why (PHP is only being used when the location ends in .php, but I've got no idea how to fix it. Here's my config:
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name inara.thefinn93.com;
location / {
root /usr/share/nginx/www;
try_files $uri $uri.php $uri/ $1?$args $1.php?$args;
}
location ~ ^(.+\.php)$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
In your scenario, the location / is the last processed location setting. Having a try_files on it won't make it past the location ~ ^(.+\.php)$ setting (unless it ends with ".php"), therefore not being forwarded to the fastcgi upstream. You might use a named location for that purpose (locations starting with "#").
Here's an example based on your configuration:
# real .php files only
location ~ ^(.+\.php)$ {
# try_files is not needed here. The files will be checked at "location /"
# try_files $uri =404;
# do not split here -- multiviews will be handled by "location #php"
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# also not needed here/with try_files
# fastcgi_index index.php;
include fastcgi_params;
}
# pseudo-multiviews
location #php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# search for the split path first
# "$uri/" is not needed since you used the index
try_files $fastcgi_script_name $uri.php =404;
}
# this should also be before "location /"
location ~ /\.ht {
deny all;
}
location / {
root /usr/share/nginx/www;
# if file does not exist, see if the pseudo-multiviews work
try_files $uri #php;
}

Categories