I run a NGINX-PHP7-COMPOSER image that works well but when using Slim Framework I had to change Nginx's default config to make the URL rewrites work.
And now it show this error in the logs:
2017-01-21 14:38:34,357 INFO success: php-fpm7 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2017-01-21 14:38:34,359 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2017/01/21 14:38:37 [error] 15#15: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: auth-api, request: "GET /hello HTTP/1.1", upstream: "fastcgi://172.18.0.6:9000", host: "localhost:9100"
172.18.0.1 - - [21/Jan/2017:14:38:37 +0000] "GET /hello HTTP/1.1" 502 537 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36"
The config file I mounted is loaded (I checked the bash with nginx -T):
server {
listen 80;
server_name auth-api;
index index.php;
root /var/www/html;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass auth-api:9000;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
My Dockerfile (just the image i am loading from the repository):
FROM richarvey/nginx-php-fpm
And my docker-compose.yml:
version: '2'
services:
#############
## MARIADB ##
#############
mariadb:
image: mariadb
restart: always
volumes:
- "./log/mariadb:/var/log/mysql:rw"
- "./data/mariadb:/var/lib/mysql:rw"
environment:
- "MYSQL_ROOT_PASSWORD=pass"
ports:
- "3306:3306"
##############
## FRONTEND ##
##############
frontend:
image: skiychan/nginx-php7:latest
volumes:
- ./services/frontend/src:/data/www
links:
- mariadb:mysql
ports:
- "9001:80"
##############
## AUTH API ##
##############
auth-api:
build: ./services/api/auth/
volumes:
- ./services/api/auth/code/:/var/www/html
- ./services/api/auth/:/etc/nginx/sites-available/
links:
- mariadb:mysql
ports:
- "9100:80"
- "9000:9000"
Do you have any idea of what is going wrong?
Related
I'm trying to configure nginx config in Docker but when I start "sudo docker-compose up -d" and try to connect to "http://localhost:8098/" I see: File not found. and logs like:
docker-infrastructure_php-fpm_1 | 172.19.0.3 - 03/Mar/2022:21:35:35 +0000 "GET /index.php" 404
docker-infrastructure_nginx_1 | 172.19.0.1 - - [03/Mar/2022:21:35:35 +0000] "GET / HTTP/1.1" 404 27 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36" "-"`
There are my:
nginx.conf
server {
listen 80;
server_name dgtips-backend-local;
root /home/sergey/PhpstormProjects/dgtips-backend/public;
index index.php;
location / {
try_files $uri /$uri /index.php?$query_string;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME /home/sergey/PhpstormProjects/dgtips-backend$fastcgi_script_name;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
}
}
nginx.Dockerfile
FROM nginx
ADD docker/nginx/nginx.conf /etc/nginx/conf.d/default.conf
WORKDIR /home/sergey/PhpstormProjects/dgtips-backend/public
docker-compose.yml
version: "3.3"
services:
nginx:
build:
context: .
dockerfile: docker/nginx/nginx.Dockerfile
ports:
- 8098:80
volumes:
- ./:/home/sergey/PhpstormProjects/dgtips-backend
links:
- php-fpm
php-fpm:
build:
context: .
dockerfile: docker/php-fpm/fpm.Dockerfile
volumes:
- ./:/home/sergey/PhpstormProjects/dgtips-backend
And finally
fpm.Dockerfile
FROM php:7.4-fpm
RUN apt-get update \
&& docker-php-ext-install pdo pdo_mysql
I'm completely sure that I have correct paths to my (local) project and to my index.php file
this is my first time working with nginx and I'm using it to access my dockerised drupal appliction from a production subdomain.
So before everything, I'm currently using docker-compose to create my sql, app, and webservice containers, here is my docker-compose file :
version: '3'
services:
app:
image: osiolabs/drupaldevwithdocker-php:7.4
volumes:
- ./docroot:/var/www/html:cached
depends_on:
- db
restart: always
container_name: intranet
db:
image: mysql:5.5
volumes:
- ./mysql:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=linux1354web
- MYSQL_USER=root
- MYSQL_PASSWORD=linux1354web
- MYSQL_DATABASE=intranet
container_name: intranet-db
web:
build: ./web
ports:
- 88:80
depends_on:
- app
volumes:
- ./docroot:/var/www/html:cached
restart: always
container_name: webIntranet
I don't think the containers are the problem, as when I go to the drupal containers the site works, but my main problem is the link with the nginx container. Here is my nginx.conf :
# stuff for http block
client_max_body_size 1g;
# fix error: upstream sent too big header while reading response header from upstream
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
server {
listen 80;
listen [::]:80 default_server;
server_name _;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
#RENVOYER LA PAGE DE GARDE DE APACHE2
location / {
# try to serve file directly, fallback to app.php
try_files $uri /index.php?$query_string; # For Drupal >= 7
}
location /intranet {
# try to serve file directly, fallback to app.php
try_files $uri $uri/;
}
#RENVOYER LE FICHIER ROBOTS.TXT
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location ~ \.php(/|$) {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:80;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $document_root;
}
}
And this is my DockerFile to build nginx image:
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
When I go to localhost:88/ I currently have a apache2 hub page, but the moment I'm trying for another page I always get a 502 bad gateway error and the logs are saying :
webIntranet | 2021/03/11 08:44:55 [error] 30#30: *1 upstream sent unsupported FastCGI protocol version: 72 while reading response header from upstream, client: 172.26.0.1, server: _, request: "GET /index HTTP/1.1", upstream: "fastcgi://172.26.0.3:80", host: "localhost:88"
To go more into details, my docker folder looks like that, docroot containes the drupal website.
I have tried sovling the problem by changing the ports as some solution mentionned it but it did nothing, I don't understand what could be wrong, I've tried many things with the conf but none of them works, and I still even can't have a single page of the drupal site showing up.
The drupaldevwithdocker-php project isn't using php-fpm, hence the response is unsupported as it's from apache rather than php-fpm. I'd imagine you'd need something more like this?
proxy_pass http://app:80;
See https://gist.github.com/BretFisher/468bca2900b90a4dddb7fe9a52143fc6
I'm trying to build a simple LEMP setup. Nginx and MySQL work fine. But I can't manage to add PHP. Here are all the relevant files :
docker-compose.yml
version: '3.7'
services:
db:
image: mariadb
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: lemp
phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- db
restart: always
ports:
- 8080:80
nginx:
build: ./docker/nginx/
depends_on:
- db
ports:
- 8000:80
volumes:
# - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
# - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./log:/var/log/nginx
- ./code:/usr/share/nginx/html
php:
image: phpdockerio/php73-fpm
depends_on:
- db
Dockerfile for Nginx
FROM nginx:latest
COPY nginx.conf /etc/nginx/
COPY default.conf /etc/nginx/conf.d
nginx.conf
http {
include /etc/nginx/conf.d/*.conf;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
error_log off;
gzip on;
gzip_disable "msie6";
open_file_cache max=100;
}
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
user www-data;
worker_processes 4;
pid /run/nginx.pid;
default.conf
server {
server_name localhost;
root /usr/share/nginx/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
When I run a docker-compose up, everything is going fine. Can create whatever HTML file in the shared folder, it's OK. But, if I create a PHP file and try to access it, I get a File not found..
Here is what I can see in the logs:
access.log
192.167.150.1 - - [16/Mar/2020:08:17:17 +0000] "GET /test.php HTTP/1.1" 404 47 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0"
error.log
2020/03/16 08:16:39 [error] 6#6: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 192.168.160.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:8000"
2020/03/16 08:16:39 [error] 6#6: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.160.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://192.168.160.4:9000", host: "localhost:8000"
2020/03/16 08:17:17 [error] 6#6: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.160.1, server: localhost, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://192.168.160.4:9000", host: "localhost:8000"
I can see the error about the folder access being forbidden, but I can found a way to fix that (if that's even really the issue.
try nginx: depends_on: php
or something like
nginx:
build: ./docker/nginx/
depends_on:
- php
ports:
- 8000:80
volumes:
- ./:/usr/share/nginx/html
- ./log:/var/log/nginx
- ./code:/usr/share/nginx/html
php:
image: phpdockerio/php73-fpm
working_dir: /usr/share/nginx/html
depends_on:
- db
ports:
- "9000"
Have been trying to setup my own LEMP stack locally , nginx and php both seem to be working well alone, however on trying to integrate php in nginx is failing...!!! getting error
403 Forbidden
nginx error logs:
2018/07/22 12:06:48 [error] 9#9: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 172.19.0.4, server: localhost, request: "GET / HTTP/1.1", host: "laradock.localhost:8000"
172.19.0.4 - - [22/Jul/2018:12:06:48 +0000] "GET / HTTP/1.1" 403 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" "172.19.0.1"
Been using below docker image versions:
PHP_TAG=7.1-fpm-alpine
NGINX_TAG=alpine
my docker-compose.yml file
version: "2"
services:
php:
image: php:$PHP_TAG
# restart: always
container_name: "${PROJECT_NAME}_php"
environment:
PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
DB_HOST: $DB_HOST
DB_USER: $DB_USER
DB_PASSWORD: $DB_PASSWORD
DB_NAME: $DB_NAME
DB_DRIVER: $DB_DRIVER
volumes:
- /var/www/ro_www/src/sample_site/:/usr/share/nginx/html
nginx:
image: nginx:$NGINX_TAG
container_name: "${PROJECT_NAME}_nginx"
depends_on:
- php
# - mysqld
environment:
NGINX_STATIC_CONTENT_OPEN_FILE_CACHE: "off"
NGINX_ERROR_LOG_LEVEL: debug
NGINX_BACKEND_HOST: php
NGINX_SERVER_ROOT: /var/www/html/
# NGINX_DRUPAL_FILE_PROXY_URL: http://example.com
volumes:
- /var/www/ro_www/src/sample_site/:/usr/share/nginx/html
- ./config/site.conf:/etc/nginx/conf.d/site.conf:ro
# - ./etc/ssl:/etc/ssl
# ports:
# - "8000:80"
# - "3000:443"
labels:
- 'traefik.backend=nginx'
- 'traefik.port=80'
- 'traefik.frontend.rule=Host:${PROJECT_BASE_URL}'
mailhog:
image: mailhog/mailhog
container_name: "${PROJECT_NAME}_mailhog"
labels:
- 'traefik.backend=mailhog'
- 'traefik.port=8025'
- 'traefik.frontend.rule=Host:mailhog.${PROJECT_BASE_URL}'
adminer:
container_name: "${PROJECT_NAME}_adminer"
image: wodby/adminer:$ADMINER_TAG
environment:
ADMINER_SALT: adminer-salt
volumes:
- /var/www/ro_www/src/sample_site/adminer/:/usr/share/nginx/html
labels:
- 'traefik.backend=adminer'
- 'traefik.port=9000'
- 'traefik.frontend.rule=Host:adminer.${PROJECT_BASE_URL}'
portainer:
image: portainer/portainer
container_name: "${PROJECT_NAME}_portainer"
command: --no-auth -H unix:///var/run/docker.sock
volumes:
- /var/run/docker.sock:/var/run/docker.sock
labels:
- 'traefik.backend=portainer'
- 'traefik.port=9000'
- 'traefik.frontend.rule=Host:portainer.${PROJECT_BASE_URL}'
traefik:
image: traefik
container_name: "${PROJECT_NAME}_traefik"
command: -c /dev/null --web --docker --logLevel=INFO
ports:
- '8000:80'
- '8080:8080' # Dashboard
volumes:
- /var/run/docker.sock:/var/run/docker.sock
The nginx site.conf file is as follows:
server {
listen 80;
server_name nginx;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root /usr/share/nginx/html;
# }
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
#root html;
#fastcgi_pass php:9000;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
Thanks in advance for your help !!! ;)
Since I did not get much feedback, after much trial and error, I got my solution!!
So am posting this, in case somebody may have similar issue.
It seems the issue here was the nginx site.conf file.
server {
listen 80;
listen [::]:80;
index index.php index.html;
server_name laradock.localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /usr/share/nginx/html;
location / {
autoindex on; #to list file in the directory if the index file is missing
}
#### this was where i was facing the issue, the php block
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Another this to note is that nginx can have multiple .conf config files, and it dosent support .htaccess file like apache (the htaccess logic needs to be re-written to a .conf file in the nginx conf.d directory here)
The complete code for my solution can be found at this git repository
I'm fiddling with Docker, trying to setup a Docker composition with Nginx and PHP-FPM running on separate Alpine containers. My setup is available on GitHub at https://github.com/sparkbuzz/lemp_docker, my docker-compose.yml looks as follows:
version: '3'
services:
alpine_nginx:
build: ./nginx
container_name: alpine_nginx
links:
- alpine_php
ports:
- "80:80"
alpine_php:
build: ./php
container_name: alpine_php
ports:
- "9000:9000"
I am able to build the images successfully, and when I visit localhost in my browser, I can see the index.html served by Nginx. However, when trying to access phpinfo.php, I get a 502 - Bad Gateway error
I can docker exec -it ... /bin/ash into both the running instances, and it seems the services are running happily, however, it's clear PHP-FPM on port 9000 is never even hit.
Here's some feedback from my console:
Recreating alpine_php ...
Recreating alpine_php ... done
Recreating alpine_nginx ...
Recreating alpine_nginx ... done
Attaching to alpine_php, alpine_nginx
alpine_php | [06-Nov-2017 21:46:39] NOTICE: fpm is running, pid 1
alpine_php | [06-Nov-2017 21:46:39] NOTICE: ready to handle connections
alpine_nginx | 2017/11/06 21:46:46 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.20.0.2:9000", host: "localhost"
alpine_nginx | 172.20.0.1 - - [06/Nov/2017:21:46:46 +0000] "GET /index.php HTTP/1.1" 502 568 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "-"```
I'm so close, but not sure why Nginx isn't happy with the PHP upstream. Nginx config is as follows:
server {
listen 80;
server_name localhost;
location / {
root /var/www/localhost/htdocs/;
index index.html;
}
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass alpine_php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
How do I get Nginx talking to PHP FPM?
You are listen 127.0.0.1 in your php-fpm config.
Add this to php/Dockerfile
RUN sed -i 's/127.0.0.1:9000/0.0.0.0:9000/g' /etc/php7/php-fpm.d/www.conf