File not found nginx php-fpm - php

I've looked through every question like this here and tried to apply the stated fixes with no success.
I'm using the wordpress:4.7.3-php7.0-fpm-alpine docker image with a separate nginx container in front of it.
When I curl wordpress I get:
File not found.
When I check the wordpress container logs, I get:
127.0.0.1 - 16/Mar/2017:06:26:24 +0000 "GET /index.php" 404
127.0.0.1 - 16/Mar/2017:06:31:27 +0000 "GET /index.php" 404
127.0.0.1 - 16/Mar/2017:06:32:16 +0000 "GET /index.php" 404
127.0.0.1 - 16/Mar/2017:06:37:17 +0000 "GET /index.php" 404
127.0.0.1 - 16/Mar/2017:06:39:09 +0000 "GET /index.php" 404
The actual nginx error is:
2017/03/16 06:26:24 [error] 17#17: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 10.128.0.7, server: k8wp, request
: "GET / HTTP/1.0", upstream: "fastcgi://127.0.0.1:9000"
I'm using php 7
/var/www/html # php-fpm -v
PHP 7.0.16 (fpm-fcgi) (built: Mar 3 2017 23:07:56)
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.16, Copyright (c) 1999-2017, by Zend Technologies
My nginx config is
server {
root /app;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
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;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I'm running nginx as the user www-data:
user www-data;
According to /usr/local/etc/php-fpm.d/www.conf the user and group are uncommented and set to www-data

The error indicates that your SCRIPT_FILENAME is incorrect. Your comment:
in the wordpress container it's at /var/www/html/index.php in the
nginx container it's at /app
suggests that nginx and php-fpm are seeing a different document root.
In which case, use:
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;

You can try to include include snippets/fastcgi-php.conf;. It should fix your problem.

The issue I had with this was that my wordpress install was in a subdirectory. So the /blog/ main index of wordpress would load, but none of the published blog entries or pages in deeper directories such as /blog/wp-admin/ would load.
To fix this, I added this block:
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
This is actually explained in "Location Strategies" of the nginx wordpress recipes page.

My issue was due to serving the website's folder from within user's home folder.
Once I moved it from /home/ubuntu/my-website to /srv/my-website all worked fine.

Related

Have php server some .js and .css files

I am using two containers in AWS ECS, nginx and php and I would like to have php serve some files like for example:
/app/plugins/sitepress-multilingual-cms/templates/language-switchers/legacy-list-horizontal/style.min.css
This is my nginx.conf:
load_module modules/ngx_http_perl_module.so;
env PHP_SERVICE;
http {
perl_set $php_service 'sub { return $ENV{"PHP_SERVICE"}; }';
resolver 169.254.169.253;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
index index.php index.html index.htm;
listen 80;
listen [::]:80;
root /var/www/html/web;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
add_header Access-Control-Allow-Origin *;
}
location ~ \.php$ {
fastcgi_pass ${php_service}:9000;
include fastcgi.conf;
add_header Access-Control-Allow-Origin *;
}
location ~* \.(css|js|gif|ico|jpeg|jpg|png)$ {
try_files $uri /index.php?$query_string;
include /etc/nginx/mime.types;
expires max;
log_not_found off;
}
}
}
The problem is that I get 404 for that file and a few more:
https://xxxxxxx.xxxxxxxxx.xx/app/plugins/sitepress-multilingual-cms/templates/language-switchers/legacy-list-horizontal/style.min.css?ver=1
Request Method: GET
Status Code: 404
As I understand it nginx will use the last location to match this request and will try first to find the file locally (with the try_files) and if not find (it shouldn't since file is not there) it will pass it to /index.php.
This file doesn't exist in the nginx container but exists in the php container.
request path:
/app/plugins/sitepress-multilingual-cms/templates/language-switchers/legacy-list-horizontal/style.min.css?ver=1
nginx container:
/var/www/html/web # cat web/app/plugins/sitepress-multilingual-cms/templates/language-switchers/legacy-list-horizontal/style.min.css
cat: can't open 'web/app/plugins/sitepress-multilingual-cms/templates/language-switchers/legacy-list-horizontal/style.min.css': No such file or directory
php container
/var/www/html # cat web/app/plugins/sitepress-multilingual-cms/templates/language-switchers/legacy-list-horizontal/style.min.css
.wpml-ls-legacy-list-horizontal{border:1px solid transparent;padding:7px;clear:both}.wpml-ls-legacy-list-horizontal>ul{padding:0;margin:0 !important;list-style-type:none}.wpml-ls-legacy-list-horizontal .wpml-ls-item{padding:0;margin:0;list-style-type:none;display:inline-block}.wpml-ls-legacy-list-horizontal a{display:block;text-decoration:none;padding:5px 10px 6px;line-height:1}.wpml-ls-legacy-list-horizontal a span{vertical-align:middle}.wpml-ls-legacy-list-horizontal a span.wpml-ls-bracket{vertical-align:unset}.wpml-ls-legacy-list-horizontal .wpml-ls-flag{display:inline;vertical-align:middle}.wpml-ls-legacy-list-horizontal .wpml-ls-flag+span{margin-left:.4em}.rtl .wpml-ls-legacy-list-horizontal .wpml-ls-flag+span{margin-left:0;margin-right:.4em}.wpml-ls-legacy-list-horizontal.wpml-ls-statics-footer{margin-bottom:30px}.wpml-ls-legacy-list-horizontal.wpml-ls-statics-footer>ul{text-align:center}
What I noticed is that the content-type of all the files giving me 404 is text/html; charset=UTF-8 even if they are .js or .css so I was thinking this can be the issue. But in the php container logs I see the request as 404:
10.80.18.143 - 10/Aug/2022:11:00:55 +0000 "GET /index.php" 200
10.80.18.143 - 10/Aug/2022:11:00:57 +0000 "GET /index.php" 404
10.80.18.143 - 10/Aug/2022:11:00:57 +0000 "GET /index.php" 404
10.80.18.143 - 10/Aug/2022:11:00:57 +0000 "GET /index.php" 404
10.80.18.143 - 10/Aug/2022:11:00:57 +0000 "GET /index.php" 404
10.80.18.143 - 10/Aug/2022:11:00:59 +0000 "GET /index.php" 404
This matches the number of the files I am getting 404 for, so not sure if it's cause of PHP cannot find the file or I am getting the 404 cause of the content-type.
Thank you

( solved ) Cant get php to run with nginx // whatevery i try i get 502 Bad Gateway

i tryed so much and followed countless guides for that problem but just cant find a solution, so i hope that you can find a problem in my configs or have a other idea what the problem could be.
Beside my planned webpage i run the AMP-Webinterface for server-administration on a supdomain amp.EXAMPLE.COM, on my debian10 server, and that always works like a charm, only my own test-php-site (see below) wont show correctly, i tested with a example html site and that works perfectly.
php8.0-fpm is installed and the only php version (had 7.3 too before and removed it already)
#######################################################
index.php
<html>
<head>
<title>PHP-Test</title>
</head>
<body>
<?php echo '<p>Hallo Welt</p>'; ?>
</body>
</html>
#######################################################
EXAMPLE.COM.conf
server {
if ($host = www.EXAMPLE.COM) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = EXAMPLE.COM) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80; listen [::]:80;
server_name EXAMPLE.COM www.EXAMPLE.COM;
root /var/www/EXAMPLE.COM;
index index.php index.html index.htm index.nginx-debian.html;
location / {
location ~* \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
try_files $uri #yii =404;
}
} }
server {
listen 443; listen [::]:443;
server_name EXAMPLE.COM www.EXAMPLE.COM;
root /var/www/EXAMPLE.COM;
index index.php index.html index.htm index.nginx-debian.html;
location / {
location ~* \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
try_files $uri #yii =404;
}
}
ssl_certificate /etc/letsencrypt/live/EXAMPLE.COM/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/EXAMPLE.COM/privkey.pem; # managed by Certbot
}
#######################################################
nginx -t
nginx: [warn] conflicting server name "amp.EXAMPLE.COM" on [::]:443, ignored
nginx: [warn] conflicting server name "amp.EXAMPLE.COM" on 0.0.0.0:443, ignored
nginx: [warn] conflicting server name "amp.EXAMPLE.COM" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "amp.EXAMPLE.COM" on [::]:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Nginx Error log isnt showing any errors too, except the "conflicting server name thing"
#######################################################
php8.0-fpm is installed and working ("php-fpm" cant be found by systemctl, could this be a problem?)
systemctl status php8.0-fpm
● php8.0-fpm.service - The PHP 8.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2021-03-16 14:08:33 UTC; 18h ago
Docs: man:php-fpm8.0(8) Main PID: 100460 (php-fpm8.0) Status: "Processes active: 0, idle: 2, > Requests: 11, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 9508) Memory: 17.6M CGroup: /system.slice/php8.0-fpm.service
├─100460 php-fpm: master process (/etc/php/8.0/fpm/php-fpm.conf)
├─100461 php-fpm: pool www
└─100462 php-fpm: pool www
Mar 16 14:08:33 localhost systemd[1]: Starting The PHP 8.0 FastCGI Process Manager... Mar 16 14:08:33 localhost systemd[1]: Started The PHP 8.0 FastCGI Process Manager.
#######################################################
tail -f /var/log/nginx/error.log
[error] 129821#129821: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 217.254.148.38, server: EXAMPLE.COM, request: "GET / HTTP/2.0", upstream: "fastcgi://127.0.0.1:9000", host: "EXAMPLE.COM"
#######################################################
Thank you i finally found the solution! breaksdownintearsofjoy
Problem has been that fastcgi wasnt installed >_>
For my debian 10 i just did: apt-get install fcgiwrap
problem was the localhost ip it kinda didnt liked; so i just replaced "fastcgi_pass 127.0.0.1:9000;" with the complete dir of the php fpm sock "fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;"

Nginx shows correct rewrite data, but not executing as expected

I have a laravel app running behind Nginx using fastcgi.
There are images available at www.example.com/media/images/someImage.jpeg
There are also images of different size of the same image available at www.example.com/media/images/660x440/someImage.jpeg
There is a script running on laravel which makes this image available for this size IF its not existing available at www.example.com/api/images/660x440/someImage.jpeg
For example if www.example.com/media/images/660x440/someImage.jpeg does not exist, www.example.com/api/images/660x440/someImage.jpeg can be accessed which will show the image and now this image also exists under www.example.com/media/images/660x440/someImage.jpeg, hopefully this makes sense.
Ok so the issue I am trying to figure out right now is how to make this happen with nginx rewrite.
Idea is that when user tries to access www. example.com/media/images/660x440/someImage.jpeg and it does not exist, nginx rewrites this to /api/images/660x440/someImage.jpeg (the script) and shows the generated image, but all of this without changing the URL. So the URL requested stays like www. example.com/media/images/660x440/someImage.jpeg but under the hood it requests the script at /api/images/660x440/someImage.jpeg and shows the generated image at the same www. example.com/media/images/660x440/someImage.jpeg.
My nginx config is as below
server {
listen 8080;
root /var/www/public;
index index.php index.html index.htm;
client_max_body_size 100M;
resolver kube-dns.kube-system.svc.cluster.local; #this is for rancher purposes
set $target app-fpm.app.svc.cluster.local; #also this
error_log /var/log/nginx/error.log notice;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location /media/images/ {
alias /var/www/storage/app/public/media/images/;
try_files $uri /$uri #generator;
}
location #generator {
rewrite ^/media/images/(.*) /api/images/$1 last;
}
location ~ ^/.+\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass $target:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I have enabled rewrite debug in nginx and in error log I can see this -
2020/09/11 23:14:28 [notice] 1034#1034: *161 "^/media/images/(.*)" matches "/media/images/660x440/someImage.jpeg", client: 10.42.0.0, server: , request: "GET /media/images/660x440/someImage.jpeg HTTP/1.1", host: "www. example .com"
2020/09/11 23:14:28 [notice] 1034#1034: *161 rewritten data: "/api/images/660x440/someImage.jpeg", args: "", client: 10.42.0.0, server: , request: "GET /media/images/660x440/someImage.jpeg HTTP/1.1", host: "www. example .com"
As you can see rewritten data is correct, it is /api/images/660x440/someImage.jpeg, but it shows 404 error as per access.log
10.42.0.0 - - [11/Sep/2020:23:33:43 +0300] "GET /media/images/660x440/someImage.jpeg HTTP/1.1" 404 622 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"
But it should show the new generated image instead.
Is this even possible what I am trying to achieve here?
I would really appreciate the help,
Thank you!

Nginx doesn't serve PHP files

I have a new fresh CentOS7 installed and running as a VM. I am playing with Nginx since I have being using Apache my whole life and now just for fun and learn I decide to switch to Nginx. I am following this two guides:
How to install LEMP on CentOS7 by Digital Ocean
How to install LEMP on CentOS7 by a blog named IfNotTrueThenFalse
And as part of my previous research before I get out of ideas I did read this which is not helpful at all.
Before continue I should said that I took what I need for each of them because I want to use PHP 7.0.x instead of the default that comes with CentOS 7 repos (5.4 I think).
So, this is how my config files looks like:
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name centos7.localdomain;
root /var/www/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_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;
}
}
/etc/php-fpm.d/www.conf
[www]
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
...
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server.
; Default Values: user and group are set as the running user
; mode is set to 0660
listen.owner = nobody
listen.group = nobody
For www.conf other than those values you see in here are the default ones. The full file is shared here
I have created the file /var/www/html/index.php with nothing else than:
<?php
phpinfo();
As soon as I try the URL http://centos7.localdomain/index.php (or without the index.php) the file is downloaded instead of display it content.
Of course after all those changes I have restarted nginx and php-fpm services and check them by runing systemctl status nginx.service and systemctl status php-fpm.service
The permissions for /var/www/html are as follow:
$ ls -la /var/www/html/
total 4
drwxr-xr-x. 2 root root 22 Oct 9 20:53 .
drwxr-xr-x. 3 root root 17 Oct 9 20:24 ..
-rw-r--r--. 1 root root 18 Oct 9 20:53 index.php
This is the PHP version I am running:
$ php -v
PHP 7.0.11 (cli) (built: Sep 14 2016 08:28:52) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.11, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.4.1, Copyright (c) 2002-2016, by Derick Rethans
I am missing something here? If so what is it? Or what is wrong on this setup that I am playing with?
Remove "try_files $uri =404" from the last segment. This may solve your problem.
location ~ \.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;
}

unable to get hhvm working with nginx

I have nginx and hhvm installed and running as processes on a ubuntu VM. The configs are copied below. I have an index.php file in /usr/share/nginx/html (pointed to by nginx) but when I try to access the site beign served by nginx, I get the following error about hhvm:
/var/log/nginx/error.log
2016/10/04 12:03:05 [crit] 12443#0: *1 connect() to unix:/var/run/hhvm/hhvm.sock failed (2: No such file or directory) while connecting to upstr
eam, client: xx.xxx.xxx.xxx, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/hhvm/hhvm.sock:", host: "xx.xx.xx.xx"
Nginx config (part of config)
server {
listen 80 default_server;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
index index.php;
try_files $uri $uri/ $uri/index.php$args /index.php$args;
}
location /mysql {
index index.php;
}
location ~* \.(php)$ {
try_files $uri = 404;
location ~ \..*/.*\.php$ {return 404;}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_keep_conn on;
fastcgi_pass unix:/var/run/hhvm/hhvm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I have checked - bothh nginx and hhvm processes are running. Any thoughts on how to troubleshoot this further?
Update:
I changed the server.ini file in /etc/hhvm to the following and restarted the service but it does not appear to have done the job
hhvm.server.file.socket=/var/run/hhvm/hhvm.sock
I commented out the 9000 port being used previously.
Update:
After making the server.init change and restarting hhvm, I get the following error in /var/log/hhvm/error.log:
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to start page server
Shutting down due to failure(s) to bind in HttpServer::runAndExitProcess
The /var/run/hhvm directory is owned by www-data
I ended up running hhvm on port 9000 and pointed nginx (fasgcgi_pas) to point to the port (instead of using the file socket option).

Categories