Docker+Nginx+Puma部署应用后运行正常但浏览器无法访问求助
Let’s work through the most likely issues that could be blocking your app from loading, even when all containers appear to be running. I’ll break this down into actionable checks:
1. Verify Container-to-Container Connectivity
First, confirm Nginx can actually reach your Puma app container:
- Exec into your Nginx container:
docker exec -it <your-nginx-container-name> bash - Try curling the app service directly:
curl http://app:3000- If this fails, there’s a networking gap between containers. Double-check your app container logs with
docker logs <app-container-name>—Puma might be failing to start, or there could be Rails initialization errors.
- If this fails, there’s a networking gap between containers. Double-check your app container logs with
2. Check Puma’s Binding Configuration
Make sure Puma is listening on a network-accessible interface, not just localhost inside the container. Verify your config/puma.rb includes:
bind 'tcp://0.0.0.0:3000'
If Puma only binds to localhost, Nginx (running in a separate container) won’t be able to connect to it.
3. Validate Nginx Configuration & Logs
- Test Nginx config syntax for errors: Exec into the Nginx container and run
nginx -t - Check Nginx error logs for clues:
cat /home/ubuntu/myapp/log/nginx.error.log- Common issues here:
- Missing or incorrect
publicfolder files: Verify the folder exists in the Nginx container withls /home/ubuntu/myapp/public - Upstream resolution failures: If Nginx can’t resolve
app:3000, confirm Docker’s default network is working for your compose services.
- Missing or incorrect
- Common issues here:
4. Inspect Host Machine Port & Firewall
On your host server:
- Check if port 80 is actively listening:
sudo netstat -tulpn | grep :80 - Ensure no firewall (like
ufworiptables) is blocking incoming traffic on port 80. Temporarily allow it withsudo ufw allow 80if you’re using UFW to rule this out.
5. Check App Container Logs for Hidden Errors
Even if the app container shows as "running", Rails might be crashing silently after startup. Run docker logs <your-app-container-name> to look for:
- Database connection failures (check your
.envfile for correct credentials) - Missing environment variables
- Puma startup exceptions
6. Bypass Nginx to Test Direct App Access
Temporarily modify your docker-compose.yml to expose the app’s port directly on the host:
services: app: build: . command: bundle exec puma -e production env_file: - /home/ubuntu/myapp/.env ports: # Add this line - "3000:3000"
Restart the app container, then try accessing http://<your-host-ip>:3000 directly. If this works, the problem is isolated to Nginx configuration or container connectivity.
Quick Tweaks to Try
- Replace the legacy
linksdirective in yourdocker-compose.ymlwithdepends_onto ensure the app starts before Nginx, and explicitly define a network (compose creates one by default, but this avoids edge cases):
version: '2' services: app: build: . command: bundle exec puma -e production env_file: - /home/ubuntu/myapp/.env expose: - "3000" networks: - app-network web: build: context: . dockerfile: Dockerfile-nginx depends_on: - app ports: - "80:80" networks: - app-network sidekiq: build: . command: bundle exec sidekiq -e production -C config/sidekiq.yml env_file: - /home/ubuntu/myapp/.env networks: - app-network networks: app-network: driver: bridge
- Confirm your precompiled assets exist in both containers: Check
public/assetsin the app and Nginx containers to ensure Rails asset precompilation worked correctly.
内容的提问来源于stack exchange,提问作者Hardik Upadhyay

