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

非Docker部署Nginx对接Docker-Compose Rails应用配置步骤咨询

Configuring Non-Docker Nginx to Proxy a Docker-Compose Rails App

Hey there, let's walk through this step by step since you're new to Nginx and Docker—no worries, it's simpler than it looks! Here's how to get your existing Nginx server routing traffic to your new Rails app running in Docker:

1. Verify Your Rails App is Accessible First

Before touching Nginx, make sure your Dockerized Rails app is up and reachable locally:

  • Start your production stack with:
    docker-compose -f docker-compose.prod up -d
    
  • Test if the app responds by running:
    curl http://localhost:3051
    
    You should see your Rails app's default page or content if everything's working.

2. Create an Nginx Reverse Proxy Configuration

Next, we'll set up Nginx to forward requests from your domain/IP to the Docker app's port.

Step 2.1: Create a New Nginx Config File

Navigate to your Nginx configuration directory (usually /etc/nginx/sites-available/ or /etc/nginx/conf.d/) and create a file like my-rails-app.conf:

server {
    listen 80;
    server_name your-domain.com; # Replace with your actual domain or server IP

    # Handle static assets directly with Nginx (more efficient than Rails)
    location /assets {
        root /path/to/your/rails/project/public; # Path to your Rails app's public folder on the server
        expires 1y;
        add_header Cache-Control public;
    }

    # Proxy all other requests to your Dockerized Rails app
    location / {
        proxy_pass http://localhost:3051;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Step 2.2: Update Rails Static File Setting

Remember that comment in your docker-compose.prod file? Go ahead and set RAILS_SERVE_STATIC_FILES=false in the environment section—we want Nginx to handle static assets instead of Rails.

3. Enable the Config & Restart Nginx

  • If you're using the sites-available/sites-enabled structure, link your config to the enabled directory:
    sudo ln -s /etc/nginx/sites-available/my-rails-app.conf /etc/nginx/sites-enabled/
    
  • Test if your Nginx config has no syntax errors:
    sudo nginx -t
    
  • If you get "test is successful", restart Nginx to apply changes:
    sudo systemctl restart nginx
    

4. Optional: Improve Your Docker Compose Setup

Your current network_mode: "host" works, but using a custom Docker network is more secure and flexible (especially if you add a database container later):

  • Remove the network_mode: "host" line from your docker-compose.prod
  • Add a custom network at the bottom of the file, and attach your app to it:
    version: '3' # Upgrade to a newer Compose version (v1 is outdated)
    services:
      app:
        build: .
        # Update the wait-for command to point to your DB service name (e.g., db:3306) if using a Docker DB
        command: "sh scripts/wait-for 127.0.0.1:3306 -- scripts/start.sh"
        environment:
          - RAILS_ENV=production
          - RAILS_SERVE_STATIC_FILES=false
          - MYSQL_USER=${DATABASE_USER}
          - MYSQL_PASSWORD=${DATABASE_PASSWORD}
        env_file:
          - .env.prod
        ports:
          - "3051:3000"
        networks:
          - rails-app-network
    
    # Uncomment and configure this if you want to run MySQL in Docker too
    # db:
    #   image: mysql:8.0
    #   environment:
    #     - MYSQL_ROOT_PASSWORD=${DATABASE_ROOT_PASSWORD}
    #     - MYSQL_DATABASE=${DATABASE_NAME}
    #   volumes:
    #     - mysql-data:/var/lib/mysql
    #   networks:
    #     - rails-app-network
    
    networks:
      rails-app-network:
        driver: bridge
    
    # volumes:
    #   mysql-data:
    

That's it! Now you should be able to visit your domain/server IP and see your Rails app served through Nginx.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 19:47:38