Docker环境下Nginx与PHP容器通信失败:连接被拒绝求助
Hey there, let's work through this 502 error you're facing—this is super common when setting up Nginx and PHP in Docker for the first time, so we'll get it sorted out step by step.
First, let's break down the key detail from your error log:
web | 2018/03/21 09:25:40 [error] 6#6: *6 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: web, request: "GET / HTTP/1.1", upstream: "fastcgi://172.18.0.4:9000", host: "localhost:8080"
This tells us Nginx can't establish a connection to the PHP-FPM instance at 172.18.0.4:9000. Here are the most likely fixes to try:
1. Verify PHP-FPM is listening on an accessible port
First, make sure your PHP container is actually running PHP-FPM and listening on a port that other containers can reach:
- Check if the PHP container is active: Run
docker psand confirm the container status showsUp. If it's not running, check its startup logs withdocker logs <your-php-container-name>to fix any boot issues. - Inside the PHP container, check what port PHP-FPM is using:
You should see output showing PHP-FPM listening ondocker exec -it <your-php-container-name> bash # Run either of these commands to check listening ports: netstat -tulpn | grep 9000 # OR ss -tulpn | grep 90000.0.0.0:9000(not just127.0.0.1—if it's only listening on localhost, other containers can't connect to it). If it's stuck on 127.0.0.1:- Edit the PHP-FPM config (usually
/usr/local/etc/php-fpm.d/www.confin official PHP images) and changelisten = 127.0.0.1:9000tolisten = 0.0.0.0:9000. - Restart the PHP container with
docker restart <your-php-container-name>.
- Edit the PHP-FPM config (usually
2. Ensure Nginx and PHP are on the same Docker network
Docker containers can't communicate with each other unless they're on the same network (the default bridge has limitations, so using a custom network is more reliable):
- Create a custom network if you haven't already:
docker network create app-network - Restart both containers attached to this network:
docker run --network app-network --name php-container ... # Your PHP container run command docker run --network app-network --name nginx-container ... # Your Nginx container run command - Test connectivity from Nginx to PHP: Run
docker exec -it nginx-container ping php-container(use your actual PHP container name). If it pings successfully, Docker's internal DNS is working correctly.
3. Use container names instead of static IPs in Nginx config
Docker container IPs can change when containers restart, so hardcoding 172.18.0.4 is unreliable. Instead, use your PHP container's name as the upstream address:
- Update your Nginx server block to point to the container name:
location ~ \.php$ { root /var/www/html; fastcgi_pass php-container:9000; # Replace with your PHP container's name fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } - Restart the Nginx container to apply the config:
docker restart nginx-container
4. Confirm file path alignment between containers
While your error is connection-related, it's worth double-checking that the web root path in Nginx matches the path in PHP. If you're mounting a local directory, make sure both containers use the same target path:
# Example run commands with shared volume docker run --network app-network -v ./your-project:/var/www/html --name php-container ... docker run --network app-network -v ./your-project:/var/www/html --name nginx-container ...
After trying these steps, test your site again—this should resolve the connection refused error and get your setup working.
内容的提问来源于stack exchange,提问作者NoGall

