index.php not working with TOR nginx - php

all.
I'm using debian with nginx and php5-fpm. I had the site fully functional and then installed Tor to create and onion site. I was successful in configuring it to load an index.html however, when I use an index.php the Tor browser does not display the page. Instead, the Tor browser downloads the index.php. I'm not sure what configurations I need to make. The point of me doing this is for learning. I don't care about security or really using the .onion site. It's bothering me though not figuring it out. Thank you.
This is my server block config in /etc/nginx/sites-available/
server {
listen 127.0.0.1:80;
root /var/www/html/;
index index.php index.html index.htm;
server_name 4bgxjb2vkb7tvsgw.onion;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
root /var/www/html/;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
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;
Thank you for your help!

Try something like this:
location / {
try_files $uri $uri/ =404;
}
error_page 401 403 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.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;
}

Related

nginx downloads php files instead of executing them

I'm trying to set up a LEMP stack and after going through several tutorials i can't seem to get php working here is my nginx config:
default.conf:
server {
listen 80 0.0.0.0;
listen [::]:80 0.0.0.0 ipv6only=on;
# 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/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I believe your problem is the second use of try_files inside the PHP block:
location ~ \.php$ {
===>try_files $uri =404;<====
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Just take that line out and try this:
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
You probably need to add this to your location ~ .php$ {}
include snippets/fastcgi-php.conf;
So it should look like:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
...
}
I found this tut:
https://www.linuxbabe.com/linux-server/how-to-install-lemp-stack-linux-nginx-mariadb-php-on-centos7
It worked!!!!! Here is my default.conf now:
server {
listen 80;
server_name www.example.com example.com;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$query_string;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

nginx - codeigniter 404 error for all urls

I have uploaded my files(Codeigniter Framework) in abcd directory ( url : example.com/abcd ) . All the links inside give 404 error . For removing index.php, I have placed this nginx.conf file in my root directory ( on server : /.../abcd/)
Here is the code for the nginx.conf file :
server {
server_name example.com/abcd;
root /..../abcd;
index index.html index.php index.htm;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass example.com/abcd;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
In apache, it was easy using rewrite, i am now stuck with nginx. Please help, I am using nginx for the first time
Try below code.. taken from
https://serverfault.com/questions/695521/codeigniter-in-subdirectory-on-nginx-404
location /abcde/ {
alias /var/www/abcde/;
try_files $uri $uri/ /abcde/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass backend;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}

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.

The php file is download instead execute on nginx

I use the Symfony config of Nginx server:
server {
server_name cmf.localhost www.cmf.localhost;
root /mnt/hgfs/www/cmf/web;
error_log /var/log/nginx/cmf.error.log;
access_log /var/log/nginx/cmf.access.log;
# strip app.php/ prefix if it is present
rewrite ^/app_dev\.php/?(.*)$ /$1 permanent;
location = /libs/ckfinder/core/connector/php/connector.php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
location / {
index app_dev.php app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app_dev.php/$1 last;
}
# pass the PHP scripts to FastCGI server from upstream phpfcgi
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
and it work well, but I also need to execute /libs/ckfinder/core/connector/php/connector.php. Now when I try to execute them, it simply download.
How can I config Nginx to executed /libs/ckfinder/core/connector/php/connector.php file?
server {
listen 80;
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ /index.html;
}
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 the php-fpm socket
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;
}
}

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