Nginx - PHP file is downloaded instead of 404 error (Piwigo Docker) - php

I will preface this by saying that I almost always use Apache, but am trying Nginx while moving some stuff to a docker container. I am using the Piwigo Docker image from LinuxServer.io located here, and there is an open issue for this pro, but it has yet to be resolved. So, I am hoping that someone here could possibly point me in the right direction. For testing, if anyone is interested, the issue can be seen on a completely clean pull after setup.
Issue: Accessing any page or folder that doesn't exist gives one of two outcomes.
Outcome 1: attempt to access /fakepage.html or /fakepage = PHP file is downloaded instead of served.
Outcome 2: attempt to access /fakepage.php = empty page with "File not found" in the top left.
All configs are shown in the Github link above, but shown here for context.
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name _;
set $root /app/www/public;
if (!-d /app/www/public) {
set $root /config/www;
}
root $root;
index index.html index.htm index.php;
location / {
# enable for basic auth
#auth_basic "Restricted";
#auth_basic_user_file /config/nginx/.htpasswd;
try_files $uri $uri/ /index.html /index.php$is_args$args =404;
}
location ~ ^(.+\.php)(.*)$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
# deny access to .htaccess/.htpasswd files
location ~ /\.ht {
deny all;
}
}
When 404 pages should be shown/logged, instead everything is logged as a 200 regardless of file downloaded or "file not found" error shown.
This issue exists on HTTP or HTTPS attempts.
I have tried this in the following Docker Hosts; Rocky 8, Rocky 9, Windows 10, and Ubuntu 22.04
I have changed themes in Piwigo and checked permissions and paths as suggested in other posts.
Here is a sample of what the downloaded php file looks like.
<?php
// +-----------------------------------------------------------------------+
// | This file is part of Piwigo. |
// | |
// | For copyright and license information, please view the COPYING.txt |
// | file that was distributed with this source code. |
// +-----------------------------------------------------------------------+
//--------------------------------------------------------------------- include
define('PHPWG_ROOT_PATH','./');
include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );
include(PHPWG_ROOT_PATH.'include/section_init.inc.php');
// Check Access and exit when user status is not ok
check_status(ACCESS_GUEST);
// access authorization check
if (isset($page['category']))
{
check_restrictions($page['category']['id']);
}
if ($page['start']>0 && $page['start']>=count($page['items']))

Related

Second Wordpress site in subdirectory of first Wordpress site returns 404 using NGINX server block

I have a Wordpress site on root, and a second Wordpress site in subdirectory /blog. I have been tasked with migrating these two Wordpress installs from one server to another. Here is the folder structure on the Ubuntu server:
/var/www/html/example.com/html # WP install 1
/var/www/html/example.com/html/blog # WP install 2
The old server works fine, however visiting https://example.com/blog (or any folder within) on the new server returns a 404 error.
Below is the configuration file, /etc/nginx/sites-available/example.com
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/example.com/html;
index index.html index.htm index.php;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
While I have been very careful to ensure the databases and directory structure match across the servers, one difference is the php has been updated on the new server from 7.0 to 7.4.
I have tried a variety of different configurations to get this to work, but so far I have not been successful. Any assistance would be greatly appreciated.
Okay, SOLVED. I made a mistake and imported the same database for both WP installs, and went down this rabbit hole instead of checking the databases. The configuration file is correct, just as it always was. Whew!

nginx isn't parsing php in nextcloud install

I'm trying to set up a nextcloud server on a raspberry pi 3 (raspian stretch).
I've been generally successful in getting the stack built using nginx and mysql. However, I've encountered a problem with nginx not parsing php.
That is, when I navigate to the pi's nextcloud install, the browser prompts me to open / save index.php (the next cloud login screen), which tells me the server isn't parsing it at all. I found one link to the nextcloud documentation which suggested adding:
index index.html index.htm index.php
to the nginx.conf file. Unfortunately, it appears to have no effect.
Any tips on how I can go about troubleshooting this or other ideas to try?
EDIT: based on comments and first answer, I have updated my /etc/nginx
EDIT #2: Reverted to the default nginx.conf file and added #Mason Stedman's code in his answer below to /etc/nginx/sites-enabled/default. Also, used the following tutorial (through step 7) to set up the stack after re-flashing raspian stretch:
https://linoxide.com/debian/install-nextcloud-10-nginx-debian-8/
For completeness, my /etc/nginx/sites-enabled/default file is below. For the server name, I had no idea what to put in, so I used the static IP address I assigned to it on my network (I'm not exposing this to the internet, BTW):
server {
listen 80;
listen [::]:80;
server_name 192.168.xx.xx;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /var/www/;
index index.php index.html;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
Try adding this to your nginx config:
Edit: Here is an example of the full thing you will need to put in there to work. I can edit again if you need SSH support, or a more comprehensive example, but this example will work on just about any configuration.
server {
listen 80;
listen [::]:80;
server_name site.com www.site.com;
access_log /var/log/nginx/site.com.access.log;
error_log /var/log/nginx/site.com.error.log;
root /var/www/site.com/htdocs/;
index index.php index.html;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
You need to send php files to a parser. You can change the version in that to the version of php you wanna use.

Why nginx always redirects to one of my virtual hosts?

I have several virtual hosts configured with nginx. They are very simiral to each other and for most of them, everything seems to be working fine.
However,
If I hit the server through a url that does not have any virtual host configured, nginx always redirects to one of the existing virtual hosts (seems to be the first one in the list.
Same happens if I simply go there using IP address.
Most of the virtual hosts look like this:
server {
server_name iibs.co;
return 302 $scheme://www.iibs.co$request_uri;
}
server {
server_name www.iibs.co;
root /var/www/iibs.co;
index index.php;
include global/restrictions.conf;
client_max_body_size 64M;
# Additional rules go here.
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
# Only include one of the files below.
# include global/wordpress.conf;
# include global/wordpress-ms-subdir.conf;
# include global/wordpress-ms-subdomain.conf;
}
I just tried adding another one, very similar just with different domain and root. And that particular one, also getting redirected to the same first vhost on the list.
At this point, I have no idea where and what should I looks for.
This is an example of default NGINX config for prevent redirects to the first of the existing virtual hosts .
### Block all illegal host headers. Taken from a discussion on nginx
### forums. Cf. http://forum.nginx.org/read.php?2,3482,3518 following
### a suggestion by Maxim Dounin. Also suggested in
### http://nginx.org/en/docs/http/request_processing.html#how_to_prevent_undefined_server_names.
server {
listen 80 default_server; # IPv4
#listen [::]:80 default_server ipv6only=on; # IPv6
server_name _;
server_name "";
return 444;
}

Creating your first laravel project on web server

ive recently just follwed this guide and "installed" laravel on my lamp server: https://www.digitalocean.com/community/tutorials/how-to-install-laravel-with-an-nginx-web-server-on-ubuntu-14-04
Now when im finished it says at the bottom of their guide
"You now have Laravel completely installed and ready to go. You can
see the default landing page by visiting your server's domain or IP
address in your web browser:
http://server_domain_or_IP"
When i visit my website there is no new landing page and there is no new files in the var/www/html that would act as a landing page except for the standard apache one.
For me this is fine but i still dont know how to create my first project, i want to do this to check my install is finished and i am ready for bed.
Here is how my www directory looks like at the moment:
And here is my nginx config which i believe plays a part in this whole thing:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/public;
index index.php index.html index.htm;
server_name www.mysite.me;
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:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
The files are in the /var/www/laravel folder rather than the /var/www/html folder which is visible in the screenshot you provided.
From my own experience when this happens, it is due to having multiple 'default server' attributes in multiple nginx config files for different websites so when looking at the server via the IP it is not selecting your new laravel build.
The server needs to restart for some of the settings to take place.
You can try accessing its console and type
To reboot a server from the command line, run:
sudo shutdown -r now
To restart Apache, run:
sudo service apache2 restart

net::ERR_CONNECTION_REFUSED with Nginx and Laravel 5

I have just installed a fresh copy of Laravel 5 into /var/www.
When I browse to the server I get net::ERR_CONNECTION_REFUSED.
My Nginx config (default) is:
server {
listen 80;
root /var/www/public;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
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;
}
}
Any idea what I'm doing wrong?
I am also confused about sites-enabled and sites-available. Where should default actually go?
I have moved default from sites-available to sites-enabled and I am now getting a 403 with "Access denied".
You probably got net::ERR_CONNECTION_REFUSED because you hadn't told nginx what port to listen on (note the listen 80 line in your config file), so you were trying to a port that wasn't open - hence the connection refused error.
As for sites-available vs sites-enabled, that's a Debian/Ubuntu thing to make sites easier to manage - you can have many sites configured in sites-available, but only run specific ones by adding a link in sites-enabled pointing at the respective config file in sites-available.
As an example, my sites-enabled folder has
lrwxrwxrwx 1 root root 40 Feb 8 07:53 site.net -> /etc/nginx/sites-available/site.net
No copying, just a link to sites-available.
For your 403 error, look in your error log for what precisely is failing. It should be located at /var/log/nginx/error.log - look for error_log in your main conf file to get the exact location.

Categories