Nginx+PHP-FPM: connection refused while connecting to upstream (502) - php

I know there's a ton of posts regarding 502 Bad Gateway, but I haven't been able to solve this problem. I'm using Docker Compose to create separate containers for Nginx and PHP-FPM.
Error I get loading PHP files in the browser (HTML files render fine):
tc-web | 2018/01/22 19:22:46 [error] 5#5: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://172.18.0.2:9000", host: "localhost:8080"
tc-web | 172.18.0.1 - - [22/Jan/2018:19:22:46 +0000] "GET /info.php HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
I've tried tweaking the various configs, using Unix socket, etc., for hours, and I still get 502 errors with PHP files. Can you spot what's wrong?
Here are all required files..
docker-composer.yml:
version: '3'
services:
web:
build:
context: ./docker/nginx
image: tc-web:0.1.0
container_name: tc-web
volumes:
# test files
- ./temp.html:/var/www/html/index.html
- ./temp.php:/var/www/html/info.php
ports:
- 8080:80
depends_on:
- php-fpm
php-fpm:
build:
context: ./docker/php-fpm
image: tc-php:0.1.0
container_name: tc-php
volumes:
- ./temp.html:/var/www/html/index.html
- ./temp.php:/var/www/html/info.php
docker/nginx/Dockerfile:
FROM nginx:1.13.8
# Install programs
RUN apt-get update
RUN apt-get install -y nano && \
apt-get install -y git && \
apt-get install -y procps
RUN mkdir -p /var/www/html
COPY nginx.conf /etc/nginx/nginx.conf
COPY default.conf /etc/nginx/conf.d/default.conf
docker/nginx/nginx.conf:
user www-data;
worker_processes 1;
# error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/default.conf;
}
docker/nginx/default.conf:
server {
listen 80;
server_name localhost;
root /var/www/html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
docker/php-fpm/Dockerfile:
FROM php:7.0-fpm
# Install programs
RUN apt-get update
RUN apt-get install -y nano && \
apt-get install -y procps
RUN mkdir -p /var/www/html
COPY php-fpm.conf /usr/local/etc/php-fpm.conf
COPY www.conf /usr/local/etc/php-fpm.d/www.conf
docker/php-fpm/php-fpm.conf:
[global]
include=etc/php-fpm.d/www.conf
docker/php-fpm/www.conf:
[global]
;daemonize = no
; if we send this to /proc/self/fd/1, it never appears
error_log = /proc/self/fd/2
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000
;listen = /var/run/php-fpm/php7-fpm.sock
;listen.owner = www-data
;listen.group = www-data
;listen.mode = 0660
access.log = /proc/self/fd/2
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
; Ensure worker stdout and stderr are sent to the main error log.
catch_workers_output = yes

Problem could be in the www.conf file.
You are listening to 127.0.0.1:9000 but this way the service won't be reachable outside the container.
Try binding to 0.0.0.0:9000:
listen = 0.0.0.0:9000

This may sound very obvious, but another common issue would be that php-fpm is not installed. This can happen even though systemctl might say that the service is running but it's not really running anything. In my case, I had also installed PHP 8 with it's own fpm package, so if you have multiple php installations, there could have been a mix up with which fpm version you installed.

Related

502 Bad Gateway Docker Symfony Nginx

I have a problem with my docker configuration, my home page on localhost return always 502 Bad Gateway.
Here you can see the architecture of the projet :
https://i.stack.imgur.com/Bfc24.png
the four container running :
https://ibb.co/XpQvnXj
I can access to phpmyadmin :
https://ibb.co/FXLqg8B
But when I go to the nginx port I have this issue :
https://ibb.co/2vs72jF
And this is the contents of the files.
docker-compose.yml :
version: '3'
services:
php:
build:
context: ./php
ports:
- "9000:9000"
volumes:
- ../symfony:/var/www
nginx:
container_name: "nginx"
build:
context: ./nginx
volumes:
- ../symfony:/var/www
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./logs:/var/log
depends_on:
- php
ports:
- "8081:80"
db:
image: mysql:5.6
restart: always
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8080:80
environment:
PMA_HOST: db
depends_on:
- db
php/Dockerfile :
FROM php:8.0.3-fpm
RUN apt-get update \
&& apt-get install -y --no-install-recommends locales apt-utils git libicu-dev g++ libpng-dev libxml2-dev libzip-dev libonig-dev libxslt-dev;
RUN curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
RUN curl -sSk https://getcomposer.org/installer | php -- && \
mv composer.phar /usr/local/bin/composer
RUN docker-php-ext-configure intl
RUN docker-php-ext-install mysqli
RUN docker-php-ext-install pdo pdo_mysql gd opcache intl zip calendar dom mbstring zip gd xsl
RUN pecl install apcu && docker-php-ext-enable apcu
WORKDIR /var/www
CMD composer install ; php-fpm
EXPOSE 9000
nginx/Dockerfile :
FROM nginx:alpine
WORKDIR /var/www
CMD ["nginx"]
EXPOSE 80
nginx/nginx.conf :
user nginx;
worker_processes 4;
daemon off;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
keepalive_timeout 65;
upstream phpserver {
server php:9000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /var/www/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass phpserver;
fastcgi_index index.php;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
deny all;
}
}
}
error.log :
2021/10/13 13:53:04 [error] 25#25: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.16.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://192.168.16.3:9000", host: "localhost:8081"
2021/10/13 13:53:04 [error] 25#25: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.16.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://192.168.16.3:9000", host: "localhost:8081", referrer: "http://localhost:8081/"
I'm new to Docker so the problem is probably obvious

Minimal Docker Nginx config for multiple PHP-FPM virtual hosts

I need to upgrade some very old PHP sites still running PHP 5.6, and since I have other sites running PHP 7 and 8, I figured I'd leverage Docker for this.
I am using the default Docker PHP-FPM and Nginx images from hub.docker.com.
I am running this on Docker for Windows with WSL2 under Ubuntu 20. In case you are also on Windows 10, here is the best guide I could find on setting up WSL2 with Docker for PHP Development to work flawlessly.
Dockerfile
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
COPY site1.conf /etc/nginx/conf.d/site1.conf
COPY site2.conf /etc/nginx/conf.d/site2.conf
nginx.conf
This is the default one in the nginx official image at /etc/nginx/nginx.conf, except for 2 lines where noted.
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# added this once some other SO posts suggested it for multiple server_name directives
server_names_hash_bucket_size 64;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
# had to add this, the default nginx.conf in the docker image doesn't include sites-enabled
include /etc/nginx/sites-enabled/*.conf;
}
site1.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example1.com www.example1.com;
root /var/www/example1.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
site2.conf
server {
listen 80;
listen [::]:80;
server_name example2.com www.example2.com;
root /var/www/example2.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Directory Structure
/home/me/nginx-test/Dockerfile
/home/me/nginx-test/site1/index.html
/home/me/nginx-test/site1/test.php
/home/me/nginx-test/site2/index.html
/home/me/nginx-test/site2/test.php
Put <h1>Site1</h1> into site1/index.html and <h1>Site2</h1> into site2/index.html respectively so it's obvious we're loading the right one.
/etc/hosts
127.0.0.1 example1.com
127.0.0.1 example2.com
Here is my method for standing this up and testing everything.
$ cd ~/nginx-test
$ docker pull nginx
$ docker pull php:5.6.40-fpm
$ docker build -t web:test .
$ docker run --name php -v /home/me/nginx-test/site1:/var/www/example1 -v /home/me/nginx-test/site2:/var/www/example2 -d php:5.6.40-fpm
$ docker run --name test -p 80:80 -v /home/me/nginx-test/site1:/var/www/example1 -v /home/me/nginx-test/site2:/var/www/example2 --link php:php -d web:test
Notice you have to launch the php container first, and name it php so that when you stand up the web container and --link it to the php container, it can find it. This is also required given the fastcgi_pass php:9000 directive in both nginx config files.
docker ps -a
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
37edb342d7b2 web:test "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp test
45ac2fb3d1d4 php:5.6.40-fpm "docker-php-entrypoi…" 10 seconds ago Up 9 seconds 9000/tcp php
Now, when I browse to http://example1.com and http://example2.com I the respective index.html pages.
docker logs test --follow
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
172.17.0.1 - - [23/Mar/2021:22:29:19 +0000] "GET / HTTP/1.1" 200 135 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" "-"
Now to make it a bit easier to grep logs, let's add the $server_name to nginx.conf log_format. More details on the offical list of nginx variables.
nginx.conf edit, line 16
log_format main '$remote_addr - $remote_user [$time_local] "$server_name $request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
Teardown and rebuild
$ docker stop test && docker rm test && docker stop php && docker rm php
$ docker build -t web:test .
$ docker run --name php -v /home/me/nginx-test/site1:/var/www/example1 -v /home/me/nginx-test/site2:/var/www/example2 -d php:5.6.40-fpm
$ docker run --name test -p 80:80 -v /home/me/nginx-test/site1:/var/www/example1 -v /home/me/nginx-test/site2:/var/www/example2 --link php:php -d web:test
$ docker logs test --follow
172.17.0.1 - - [23/Mar/2021:22:41:25 +0000] "example2.com GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" "-"
172.17.0.1 - - [23/Mar/2021:22:41:36 +0000] "example1.com GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" "-"
172.17.0.1 - - [23/Mar/2021:22:41:41 +0000] "example1.com GET /test.php HTTP/1.1" 200 85924 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" "-"
Here is some more diagnostic details.
did volumes mount?
$ docker exec -it test bash
root#37edb342d7b2:/# cd /var/www/example1
root#37edb342d7b2:/var/www/example1# ls
index.html test.php
yes.
in both containers?
$ docker exec -it php bash
root#45ac2fb3d1d4:/var/www/html# ls -la /var/www/example2
total 16
drwxr-xr-x 2 1000 1000 4096 Mar 23 21:47 .
drwxr-xr-x 1 root root 4096 Mar 23 22:21 ..
-rw-r--r-- 1 1000 1000 135 Mar 23 21:37 index.html
-rw-r--r-- 1 1000 1000 31 Mar 23 20:33 test.php
In the context of asking some questions, I figured everything out, so I decided to keep this up as a guide for future readers.
Read the guide above.

Nginx Alias Is Giving 'File not found' with Docker-Compose Containers

I have two Laravel projects
Laravel 4 with 664 user:www-data permission
Laravel 5 with 664 user:www-data permission
Note: moduletwo is the alias being used in this virtual host to point to the Laravel 5 files.
I am trying to configure both on my local machine where the Laravel 4 is the main project and Laravel 5 will be used as an alias in Nginx virtual host.
Laravel 4 is working good and no issue with that.
But when I try to access the Laravel 5 module it says File not found.and here are the logs:
access.log:
GET `/moduletwo/index.php/LFiveRoute HTTP/1.1" 404 27 "http://www.Laravel.dev/LFourRoute`
error.log:
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.22.0.1, server: www.Laravel.dev, request: "GET /moduletwo/index.php/LFiveRoute HTTP/1.1", upstream: "fastcgi://172.22.0.5:9000", host: "www.Laravel.dev", referrer: "http://www.Laravel.dev/LFourRoute
script.log:
/var/www/Laravel4/public/moduletwo/index.php > GET /moduletwo/index.php/LFiveRoute HTTP/1.1
Processes in Nginx Container
Nginx master/worker processes
ps -ef | grep 'nginx'
root 1 0 0 06:15 ? 00:00:00 nginx: master process nginx -g daemon off;
www-data 7 1 0 06:16 ? 00:00:00 nginx: worker process
root 19 8 0 07:45 ? 00:00:00 grep nginx
fpm Processes
ps -ef | grep 'fpm'
root 21 8 0 07:47 ? 00:00:00 grep fpm
Processes in PHP71 Container
Nginx master/worker processes
none
fpm Processes
ps -ef | grep fpm
root 1 0 0 06:15 ? 00:00:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
www-data 8 1 0 06:16 ? 00:00:01 php-fpm: pool www
www-data 9 1 0 06:17 ? 00:00:01 php-fpm: pool www
www-data 10 1 0 06:22 ? 00:00:01 php-fpm: pool www
root 22 13 0 07:49 ? 00:00:00 grep fpm
Docker Compose File:
version: '2'
services:
nginx:
container_name:
Laravel-nginx
restart: always
build:
context: ./
dockerfile: deploy/web.docker
volumes:
- ./:/var/www/Laravel4
- ./../Laravel_5:/var/www/Laravel5
- ./app/storage/logs/nginx:/var/log/nginx
ports:
- "80:80"
links:
- php71
networks:
Laravel_net:
ipv4_address: 172.22.0.2
database:
container_name:
Laravel-db
restart: always
image: mysql:5.7
environment:
- "MYSQL_ROOT_PASSWORD=secret"
- "MYSQL_ROOT_USER=root"
- "MYSQL_DATABASE=laravel_4"
ports:
- "3309:3306"
networks:
Laravel_net:
ipv4_address: 172.22.0.3
redis:
container_name:
Laravel-redis
image: redis:3.2
ports:
- "6372:6379"
networks:
Laravel_net:
ipv4_address: 172.22.0.4
php71:
container_name:
Laravel-php-71
restart: always
build:
context: ./
dockerfile: deploy/app.docker
volumes:
- ./:/var/www/Laravel4
- ./../Laravel_5:/var/www/Laravel5
links:
- database
- redis
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
networks:
Laravel_net:
ipv4_address: 172.22.0.5
networks:
Laravel_net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.22.0.0/16
gateway: 172.22.0.1
web.docker file:
FROM nginx:1.10
EXPOSE 80
EXPOSE 443
ADD ./deploy/vhost.conf /etc/nginx/conf.d/default.conf
ADD ./deploy/nginx.conf /etc/nginx/nginx.conf
WORKDIR /var/www
app.docker file:
FROM php:7.0-fpm
RUN apt-get update && apt-get install -y libmcrypt-dev mysql-client \
&& docker-php-ext-install mcrypt pdo_mysql
WORKDIR /var/www
nginx.conf file
user www-data;
worker_processes 1;
pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log warn;
events {
# A single worker process can handle 1024 connections
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
log_format scripts '$document_root$fastcgi_script_name > $request';
access_log /var/log/nginx/scripts.log scripts;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/*.conf;
}
vhost.conf file:
server {
listen 80;
server_name www.laravel.dev;
charset utf-8;
sendfile off;
client_max_body_size 100m;
index index.php;
root /var/www/Laravel4/public;
location ~ ^/moduletwo/(.+(?:css|js|woff|woff2|ttf))$ {
include fastcgi_params;
alias /var/www/Laravel5/public/$1;
#access_log off;
}
#moduletwo code in laravel5
location /moduletwo/ {
include fastcgi_params;
alias /var/www/Laravel5/public;
## Check for file existing and if there, stop ##
if (-f $request_filename) {
break;
}
## Check for file existing and if there, stop ##
if (-d $request_filename) {
break;
}
index index.php;
try_files $uri $uri/ #moduletwo;
# try_files $uri $uri/ /index.php?$query_string;
}
location #moduletwo {
include fastcgi_params;
rewrite /moduletwo/(.*)$ /moduletwo/index.php?/$1 last;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php71:9000;
fastcgi_index index.php;
set $php_root /var/www/Laravel4/public;
if ($request_uri ~ /moduletwo) {
set $php_root /var/www/Laravel5/public;
}
fastcgi_param PATH_TRANSLATED $php_root/index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
include fastcgi_params;
fastcgi_intercept_errors off;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_max_temp_file_size 0;
fastcgi_read_timeout 310;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ /\.ht {
deny all;
}
}
So, anybody is there? Please look into it and help me to resolve the issue. Thank you so much.

Can't make nginx and php-fpm dockers communicate

I have created two docker images to match my needs, here is the nginx one:
FROM alpine:3.3
RUN apk add --update nginx
EXPOSE 80 443
CMD nginx -c /www/dev/nginx/conf/nginx.conf -g 'daemon off;'
then the php-fpm
FROM php:7.0-fpm
RUN apt-get update
RUN apt-get install -y apt-transport-https ca-certificates dcmtk libgdcm-tools wkhtmltopdf libdbd-freetds libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
RUN apt-get install -y imagemagick --fix-missing
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
WORKDIR /www/public
CMD rm /etc/freetds.conf
ADD freetds.conf /etc/freetds.conf
CMD rm /var/cache/apk/*
Then the run commands in the following order
docker create -p 9000:9000 --name php -v /www:/www:rw php
docker start php
docker create --privileged=true -p 80:80 -p 443:443 --name nginx -v /www:/www --link php:php nginx
docker start nginx
So far all works good, containers are running and I can even get static content from nginx owever any php script fails with
[error] 6#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: platform.v2.vetology.net, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.17.0.2:9000", host: "localhost.localdomain"
doing
cgi-fcgi -bind -connect 127.0.0.1:9000
from the host machine works and php responds so I know both containers ar working and responsive they just don't communicate.
I read in a different thread that the issue is caused by php and nginx using a different root directory but here is not the case as both have the /www directory mounted and /www/public contains the index.php file I am trying to open.
And here is the nginx.conf
user nobody;
error_log /www/dev/nginx/log/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
worker_processes 1;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /www/dev/nginx/log/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
include /etc/nginx/conf.d/*.conf;
default_type application/octet-stream;
gzip on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
include /www/dev/nginx/conf/upstream.conf;
server {
listen 80;
listen 443 ssl;
listen [::]:443 default ipv6only=on;
listen [::]:80 default_server ipv6only=on;
ssl_certificate /www/dev/ssl/certificate.crt;
ssl_certificate_key /www/dev/ssl/key.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
root /www/public;
index index.php index.html index.htm;
server_name hostname.net;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
client_body_buffer_size 128k;
client_body_temp_path /www/dev/nginx/tmp/client_body_temp;
proxy_temp_path /www/dev/nginx/tmp/proxy_temp_path;
fastcgi_temp_path /www/dev/nginx/tmp/fastcgi_temp_path;
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param HTTP_PROXY "";
fastcgi_pass vetology;
fastcgi_index index.php;
include fastcgi_params;
}
}
}
Any ideas of what I maybe doing wrong?
First off, I'm not sure you want to start the nginx container with privileged. There's also no need to mount the php directive with the :rw switch. rw is the default mode.
Second, take a look at where you're telling nginx to send the php request -- specifically, which host, which is defined in nginx's fastcgi_pass directive. You have it sending the php request to a machine named vetology. But docker is linking the containers together with --link php:php. That means its taking the container named php and giving it the alias named php. That means it will be exposed to your nginx container as a machine named php.
You can try this out:
docker run -it --rm --link php:some_php nginx sh -c 'ping some_php'
Change
fastcgi_pass vetology;
to:
fastcgi_pass php:9000;
And your container should work.
For complex container assemblies like this one, have a look at docker-compose next.

Nginx and php-fpm docker compose

I'm trying to create a development environnement for PHP project with Docker-compose.
I just created a docker-compose file that contains 3 docker:
Application: that mounts my code volume
Php: that runs php5-fpm
Nginx: that runs the nginx web server.
I can get an html file through my web server but when I try with a php file I get a 502 Bad Request error.
Error.log:
2016/09/19 16:51:18 [error] 8#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: xxx, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.17.0.3:9000", host: "xxx"
I think that nginx can't connect to php.
Here is my docker-compose file:
# import the code and bind it on /var/www
application:
build: code
volumes:
- ./sites:/var/www/
tty: true
# php container
php:
build: php-fpm
volumes_from:
- application
expose:
- 9000:9000
volumes:
- ./logs/php5-fpm/:/var/log
# nginx container
nginx:
build: nginx
ports:
- 80:80
links:
- php
volumes_from:
- application
volumes:
- ./logs/nginx:/var/log/nginx
php-fpm/Dockerfile:
FROM debian:jessie
RUN apt-get update && apt-get install -y php5-common php5-cli php5-fpm php5-mcrypt php5-mysql php5-gd php5-imagick php5-curl php5-intl php5-memcache php5-imap
RUN usermod -u 1000 www-data
CMD ["php5-fpm", "-F"]
EXPOSE 9000
nginx/Dockerfile:
FROM debian:jessie
RUN apt-get update && apt-get install -y nginx
ADD nginx.conf /etc/nginx
ADD test.conf /etc/nginx/sites-available/
RUN ln -s /etc/nginx/sites-available/test.conf /etc/nginx/sites-enabled/test.conf
RUN rm /etc/nginx/sites-enabled/default
RUN echo "upstream php-upstream { server php:9000; }" > /etc/nginx/conf.d/upstream.conf
RUN usermod -u 1000 www-data
CMD ["nginx"]
EXPOSE 80
nginx/nginx.conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
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";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
daemon off;
nginx/test.conf:
server {
listen 80;
server_name xxx;
root /var/www/test;
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_log /var/log/nginx/test_error.log;
access_log /var/log/nginx/test_access.log;
}
There is no error when launching the docker-compose.
Can someone help me ?
Thanks

Categories