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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.