ERR_EMPTY_RESPONSE with Docker and NGINX - php

I have a problem with my Docker and NGINX config?
Everything is running but my web page returns a ERR_EMPTY_RESPONSE
Here is my NGINX config:
server {
listen 8443;
server_name api.test.dv;
root /api/public;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_buffer_size 32k;
fastcgi_buffers 8 16k;
internal;
}
location ~ \.php$ {
return 404;
}
}
My docker-compose :
version: "3"
services:
nginx:
image: "bitnami/nginx:latest"
ports:
- "11083:8443"
volumes:
- "./docker/nginx/vhost.conf:/opt/bitnami/nginx/conf/vhosts/app.conf"
- .:/api
depends_on:
- php
php:
build: "docker/php"
volumes:
- .:/api:cached
working_dir: /api
user: 1000:1000
depends_on:
- database
- redis
database:
image: "postgres:latest"
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: app
ports:
- "11032:5432"
redis:
image: "redis:3.2.11"
NGINX logs are good, no error.
I use Docker desktop on Mac OS.
I have really no idea about my problem.

Related

docker nginx 404 not found

Why I get this error when visit localhost:8080!
Any suggestion to solve this issue is welcome because I spend many time for it and I changed PHP docker file and nginx file many times, similar version with laravel framework is ok for me.
*1 open() "/var/www/html/myapp404" failed (2: No such file or directory)
Note that myapp folder contains index.php and other files but it is not a Laravel framework.
My docker compose:
version: '3.8'
services:
nginx:
build:
context: .
dockerfile: nginx.dockerfile
ports:
- 8080:80
depends_on:
- php
- mysql
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ${PWD}/myapp:/var/www/html/myapp
working_dir: /var/www/html/myapp
ports:
- "9000:9000"
mysql:
image: yobasystems/alpine-mariadb
ports:
- "3308:3308"
volumes:
- ./mariadb/data:/var/lib/mysql
- ./mariadb/my.conf:/etc/my.cnf
environment:
MYSQL_ROOT_PASSWORD: "111"
MYSQL_DATABASE: myapp
MYSQL_USER: root
MYSQL_PASSWORD: "111"
restart: always
My default.conf => Nginx:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/myapp;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# pass the PHP scripts to FastCGI server listening on php:9000
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;
}
}
nginx:
FROM nginx:stable-alpine
ADD nginx/default.conf /etc/nginx/conf.d/default.conf
php :
FROM dwchiang/nginx-php-fpm:latest
Thanks.

Docker NGiNX/PHP services not serving PHP

I have set up a docker-compose.yml file to start a NGiNX server and serve PHP content, the thing is it keeps on showing the default NGiNX welcome page when I visit localhost:8080.
Here's my docker-compose.yml
version: "3"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./public:/public
- ./site.conf:/etc/nginx/conf.d/site.conf
links:
- php
php:
image: php:8-fpm
volumes:
- ./public:/public
And here's my site.conf
server {
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /public;
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;
}
}
If you have an index.php file inside the public folder, you just need to change the docker-compose.yml to:
volumes:
- ./public:/public
- ./site.conf:/etc/nginx/conf.d/default.conf

Running a different version of php per project directory on a LAMP stack enviroment

Is there a way to configure or a program that allow me to run a different version of php based on my project directories for example /project1 running on php 5 and /project2 running on php 7? I have the need to run both projects at the same time.
I followed a guide that used xampp but it didn't work always loaded the version of php that came with the xampp installer.
I'm open to suggestions of another programs that install that stack and allows that kind of configuration or pointed out to a guide to set it up with docker to which I have no experience.
You can run both of them simultaneously with simple docker-compose
version: '3'
services:
#PHP5 Service
app1:
build:
context: .
dockerfile: Dockerfile-php5
container_name: app1
restart: unless-stopped
tty: true
env_file:
- ./.env/php5.env
- ./.env/.env
working_dir: /var/www/project1
networks:
- app-network
volumes:
- ./project1:/var/www/project1
#PHP7 Service
app2:
build:
context: .
dockerfile: Dockerfile-php7
container_name: app2
restart: unless-stopped
tty: true
env_file:
- ./.env/php7.env
- ./.env/.env
working_dir: /var/www/project2
networks:
- app-network
volumes:
- ./project2:/var/www/project2
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "8081:8081" #For Project1
- "8082:8082" #For Project2
networks:
- app-network
volumes:
- ./project1:/var/www/project1
- ./project2:/var/www/project2
- ./nginx/conf.d:/etc/nginx/conf.d/
- ./nginx/log:/var/log/nginx
networks:
app-network:
driver: bridge
and you should use fastcgi to pass requests on your different apps as follow:
#Project1
server {
listen 8081;
index index.php index.html;
error_log /var/log/nginx/api-error-php5.log;
access_log /var/log/nginx/api-access-php5.log;
root /var/www/project1/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
and for project2
#Project2
server {
listen 8082;
index index.php index.html;
error_log /var/log/nginx/api-error-php7.log;
access_log /var/log/nginx/api-access-php7.log;
root /var/www/project2/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app2:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}

Docker + Nginx host not found in upstream in docker

I'm trying to run Nginx and PHP-FPM containers with network_mode: host in docker-compose, when defining a network_mode as "bridge" it works fine, but when defining network_mode as "host" I receive :
nginx: [emerg] host not found in upstream "ui" in /etc/nginx/conf.d/ui.conf:10
Everything works fine when setting network_mode to bridge, unfortunately I need to set it to host, as I need access to host network so I can access ueye camera.
This is my docker-compose file
version: '3'
services:
nginx:
image: nginx:alpine
container_name: nginx
tty: true
ports:
- "80:80"
- "443:443"
- "8000:8000"
volumes:
- ./../../ui/:/var/www/
- ./nginx/:/etc/nginx/conf.d/
network_mode: host
depends_on:
- ui
restart: always
ui:
build:
context: ./../../ui
dockerfile: Dockerfile
volumes:
- ./../../ui/:/var/www/
container_name: ui
tty: true
environment:
SERVICE_NAME: ui
working_dir: /var/www
network_mode: host
ports:
- "9000:9000"
The nginx conf file
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass ui:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
While using host network mode, your container will see Docker host as localhost & not the container itself because it's using the host network. Try replacing ui with localhost in your nginx conf -
fastcgi_pass localhost:9000;

How to setup php-fpm and nginx on 2 separate containers with different volume paths

I'm attempting to setup a development environment using docker using 2 containers: nginx and php7-fpm.
What I want to happen is when a user visits any URL that contains /api it uses php-fpm, but everything else is loaded from /var/www/html.
Here is my configuration:
site.conf:
server {
index index.html;
server_name impressive.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
location /api {
index index.php;
alias /var/www/api;
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;
}
}
}
docker-compose.yml
web:
image: nginx
volumes:
- ./frontend/public:/var/www/html
- ./site.conf:/etc/nginx/conf.d/site.conf
links: [ php ]
ports:
- "8080:80"
environment:
- NGINX_HOST=http://impressive.local
- NGINX_PORT=80
php:
image: php:7-fpm
volumes:
- ./api:/var/www/api
This doesn't work as expected and when I visit impressive.local/api I get the following error in logs:
web_1 | 2019/01/10 12:23:47 [error] 6#6: *1 "/var/www/api/index.php" is not found (2: No such file or directory), client: 172.17.0.1, server: impressive.local, request: "GET /api/ HTTP/1.1", host: "impressive.local:8080"
I realize that the php-fpm container is the one that contains the /var/www/api directory and not nginx. With my configuration nginx is trying to alias to a non existent path and is thus failing.
My question is how is possible to achieve this?
Yes, I use exactly this configuration for all of my Laravel apps.
Here is an example of my configuration...
version: '2'
services:
app:
container_name: app
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
- "DB_PORT=3306"
- "DB_HOST=x.x.x.x"
web:
container_name: web
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes_from:
- app
ports:
- 8080:80
As you can see you specify to use the volume from your web container.
I think the configuration file is invalid; try this code to fix [ docker-compose.yml and site.conf ]
docker-compose.yml
version: '2'
services:
web:
image: nginx
volumes:
- ./frontend/public:/var/www/html
- ./site.conf:/etc/nginx/conf.d/site.conf
ports:
- "8080:80"
environment:
- NGINX_HOST=impressive.local
- NGINX_PORT=80
links:
- php
php:
image: php:7-fpm
volumes:
- ./api:/var/www/api
- ./frontend/public:/var/www/html
site.conf
server {
index index.html index.php;
server_name impressive.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /api/ {
alias /var/www/api;
}
location ~ ^/api/(.+\.php)$ {
alias /var/www/api;
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;
}
# pass the PHP scripts to FastCGI server listening on php:9000
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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
finally, run docker-compose build and docker-compose up

Categories