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
Related
I am using the default config with Nginx installed on my manjaro machine. I just added some simple configurations down below.
nginx.conf:
user http;
worker_processes auto;
worker_cpu_affinity auto;
.....
http{
.....
server {
listen 9000;
server_name localhost;
root /usr/share/nginx/html/exam;
location / {
index index.php index.html index.htm;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
if (!-e $request_filename){
rewrite ^/(.+)$ /index.php?url=$1 break;
}
}
location /. {
return 404;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_read_timeout 864000;
}
location ~ /\.ht {
deny all;
}
}
....
}
since the user for Nginx is http, I also changed the ownership of the folder, subfolders and files as HTTP with:
chmod -R http:http exam/
it looks like this:
1838248 lrwxrwxrwx 1 http http 42 Eyl 13 17:42 exam
but still gives 403 Forbidden on browser with this error:
2021/09/13 17:49:22 [error] 493923#493923: *4 open() "/usr/share/nginx/html/exam/index.php" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /exam HTTP/1.1", host: "localhost:9000"
I have tried every solution I found, but it did not work.
I also tried simple PHP files that runs like "phpinfo();" even they are not working.
I moved the project file under /srv/http and redirect the root in the conf file to it. Now it is working.
I can not figure out, how to connect jwilder/nginx-proxy directly to a fastcgi backend. As i am using docker stack, this is the corresponding compose-file:
php-fpm:
image: some/php-app
working_dir: /var/www/application
environment:
VIRTUAL_HOST: php-fpm.example
VIRTUAL_PROTO: fastcgi
VIRTUAL_PORT: 9000
VIRTUAL_ROOT: /var/www/application/public
This config leads to a 404 error:
2018/10/30 07:58:13 [error] 304: *5357 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 10.255.0.2, server: php-fpm.example, request: "GET /api/settings HTTP/2.0", upstream: "fastcgi://10.0.0.203:9000", host: "php-fpm.example"
If I understand the situation correctly, something like this is missing:
server {
root /var/www/application/public;
index index.php;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
}
When i place this location file under /etc/nginx/vhost.d/default_location or even under /etc/nginx/vhost.d/{VIRTUAL_HOST}_location, i get a error saying server directive is not allowed here. When i only use the "location block", i get an 502 Bad Gateway Error.
Since i have multiple backends, which are mostly identical, some default config which covers most settings would be great.
Has anyone got something like this working?
I'm trying to configure nginx to display a page (index.html) inside my folder /var/www/Exercises/PHP/taskList but it's only displaying 404 Not Found. This is my configuration file inside sites-enabled folder:
server {
listen localhost:80;
server_name taskList.dev *.taskList.dev;
root /var/www/Exercises/PHP/taskList;
access_log /var/log/taskList.access.log;
error_log /var/log/taskList.error.log warn;
add_header Strict-Transport-Security "max-age=2592000; includeSubdomains";
add_header X-Frame-Options DENY;
add_header 'Access-Control-Allow-Origin' '*';
include conf.d/server/remove-trailing-slash.conf;
include conf.d/server/symfony2.conf;
include conf.d/server/fastcgi.conf;
include conf.d/server/chromeframe.conf;
include conf.d/server/deny.conf;
}
And I've got this in my host file:
127.0.0.1 localhost
127.0.0.1 taskList.dev
I'm not sure what I'm doing, I did some research but I'm stuck with the 404 error page.
Add a location block, you'll probably want to use try_files as well, something like this:
location / { try_files $uri =404; }
I'm trying to let Nginx serve my authenticated admin users some static files with PHP. This works great, I get back the file. But it's served with a 404 status code..
I'm using the following (Symfony / Silex) php code:
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
$filePath = '/usr/share/nginx/project/src/path/to/my/protected/static/dir/' . $file;
if (empty($file) || !is_readable($filePath)) {
$app->abort(404);
}
$response = new BinaryFileResponse($filePath);
$response->trustXSendfileTypeHeader();
$response->setPrivate();
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
$file,
iconv('UTF-8', 'ASCII//TRANSLIT', $file)
);
$response->headers->addCacheControlDirective('must-revalidate', true);
return $response;
And here's my nginx config:
server {
listen 443;
listen [::]:443;
root /usr/share/nginx/project/web;
index index.php;
error_page 401 403 404 /404.html;
server_name example.com;
rewrite ^/(.*)/$ /$1 permanent;
location / {
# First attempt to serve request as file, then
# as directory, then let php handle the file
try_files $uri $uri/ /index.php$is_args$args;
index index.php;
autoindex off;
location ~* \.(svg|jpg|jpeg|png|gif|ico|css|js)$ {
expires 150d;
}
}
location ~ \.php$ {
set $path_info $fastcgi_path_info;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
try_files $uri $uri/ /index.php$is_args$args;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param APP_ENV production;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
The protected dir lies outside of the root in the nginx config (/usr/share/nginx/project/web).
I've found these kind of error messages in the logs:
open() "/usr/share/nginx/project/web/admin/static/admin.js" failed
(2: No such file or directory),
request: "GET /admin/static/admin.js HTTP/1.1"
Where /admin/static/admin.js was indeed the requested url.
Update 1
It looks like nginx always tries to open the url and adds an entry to the error log even php handles the response just fine.
Even if I replace all the php code for just: return new Response('test', 200); the response code for the body 'test' is still 404...
I also tried adding an extra location block in my nginx config:
location /protected_admin_files {
internal;
alias /usr/share/nginx/project/src/path/to/my/protected/static/dir;
}
And then try to redirect to the file like this:
return new Response('', 200, [
'X-Accel-Redirect' => '/protected_admin_files/' . $file
]);
But also without luck. Same 404 with the right response body...
I've found the cause myself..
This was what I found in the error log once I set the error level to debug (here's the full log)
...
[debug] rewrite phase: 1
[debug] http script regex: "^/(.*)/$"
[notice] "^/(.*)/$" does not match "/admin/static/admin.js", request: "GET /admin/static/admin.js HTTP/1.1", host: "example.com"
[debug] test location: "/"
[debug] test location: "protected_admin_files"
[debug] test location: ~ "\.(svg|jpg|jpeg|png|gif|ico|css|js)$"
[debug] using configuration "\.(svg|jpg|jpeg|png|gif|ico|css|js)$"
...
[debug] http filename: "/usr/share/nginx/project/web/admin/static/admin.js"
[debug] add cleanup: 00000000025AE108
[error] open() "/usr/share/nginx/project/web/admin/static/admin.js" failed (2: No such file or directory), client: 24.132.134.203, server: example.com, request: "GET /admin/static/admin.js HTTP/1.1", host: "example.com"
[debug] http finalize request: 404, "/admin/static/admin.js?" a:1, c:1
[debug] http special response: 404, "/admin/static/admin.js?"
[debug] internal redirect: "/index.php?"
...
Apparently, the nested location in my first first location / block was causing the 404.
location / {
try_files $uri $uri/ /index.php$is_args$args;
index index.php;
autoindex off;
location ~* \.(svg|jpg|jpeg|png|gif|ico|css|js)$ {
expires 150d;
}
}
It got matched because of the extension, making Nginx go looking for the file. Since it's then not found, the 404 is set and apparently not overwritten later in the process when php returns the X-Accel-Redirect header :(.
I have a nginx and php-fpm config but when i access it from browser, only index.php is getting executed but rest of the files i am not able to call .
nginx config
{
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
keepalive_timeout 15;
keepalive_requests 2048;
server_tokens off;
upstream php
{
server unix:/tmp/php-cgi.socket;
server serverip:9000;
}
access_log /var/log/nginx/access.log main;
include /etc/nginx/conf.d/*.conf;
}
config in /etc/nginx/conf.d/
server {
root /var/www/Cachet/public/;
location / {
try_files $uri $uri/ /index.php index.php;
}
server_name serverip ; # Or whatever you want to use
listen 80 default;
location ~* \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_keep_conn on;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
These are few lines from error.log and access.log
2015/11/06 12:40:53 [error] 19346#0: *1 FastCGI sent in stderr:
"Unable to open primary script: /var/www/Cachet/public/dashboard.php
(No such file or directory)" while reading response header from
upstream, client: Client IP, server: Server IP, request: "GET
/dashboard.php HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php5-fpm.sock:", host: "Server IP"
2015/11/06 12:41:05 [error] 19346#0: *1 FastCGI sent in stderr:
"Unable to open primary script: /var/www/Cachet/public/autoload.php
(No such file or directory)" while reading response header from
upstream, client: Client IP, server: Server IP, request: "GET
/autoload.php HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php5-fpm.sock:", host: "Server IP"
since there was no response here then with help from my colleague i was able to find two problem here in config file because of which i was not able to call multiple php files in separate folder ..
try_files $uri $uri/ /index.php index.php;
instead it needed
try_files $uri $uri/ /index.php$is_args$args;
Alos since it was not loading the images the line which was missing was
include /etc/nginx/mime.types; in location block of conf.d/default.conf.
Check if your installed PHP version and PHP version inside config.d do not match each other. If that is the case, change PHP version inside conf.d file to your installed PHP version. Reload nginx.
fastcgi_pass unix:/var/run/php5-fpm.sock;