You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker+Nginx+Puma部署应用后运行正常但浏览器无法访问求助

Troubleshooting: Rails App Unreachable via Browser (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.

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 public folder files: Verify the folder exists in the Nginx container with ls /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.

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 ufw or iptables) is blocking incoming traffic on port 80. Temporarily allow it with sudo ufw allow 80 if 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 .env file 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 links directive in your docker-compose.yml with depends_on to 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/assets in the app and Nginx containers to ensure Rails asset precompilation worked correctly.

内容的提问来源于stack exchange,提问作者Hardik Upadhyay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:23:01