You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Nginx与Docker Compose无法返回静态文件问题求助

Troubleshooting Static File Issues with Nginx + Docker Compose

Hey there, let's work through this static file problem you're hitting. Looking at your docker-compose.yml, I can point out some common culprits and actionable steps to fix them:

1. Fix Your Nginx Configuration (Most Likely Root Cause)

The biggest missing piece here is your Nginx server config in ./conf.d — without setting up a proper location block to serve static files directly, Nginx will just pass those requests to Gunicorn, which isn't designed to handle static assets efficiently.

Create or update your Nginx config file (like ./conf.d/app.conf) with this setup:

server {
    listen 80;
    # Replace with your domain if you have one
    server_name localhost;

    # Serve static files directly from the mounted volume
    location /static/ {
        # Important: Root points to the parent folder of your static directory, not the static folder itself
        root /flask-app/src/app;
        # Add caching headers for better performance
        expires 30d;
        add_header Cache-Control "public, max-age=2592000";
    }

    # Pass all other requests to your Gunicorn web service
    location / {
        proxy_pass http://web:8000;
        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;
    }
}

Why this works: You mounted ./src/app/static to /flask-app/src/app/static in the Nginx container. Setting root /flask-app/src/app means Nginx will look for /static/your-file.css at /flask-app/src/app/static/your-file.css — a common mistake is setting root /flask-app/src/app/static, which would make Nginx look for /flask-app/src/app/static/static/your-file.css (notice the duplicate static folder).

2. Verify File Permissions in the Nginx Container

Nginx runs as the nginx user (UID 101 by default) in the container, so it needs read access to your static files. Check permissions with these commands:

# List static files in the Nginx container to confirm they're accessible
docker-compose exec nginx ls -l /flask-app/src/app/static
# Check the nginx user's UID/GID
docker-compose exec nginx id nginx

If permissions are locked down, you have two options:

  • Update your Web service's Dockerfile to set read permissions on the static folder (e.g., RUN chmod -R 755 /flask-app/src/app/static)
  • Ensure your host machine's static files have read permissions for all users (e.g., chmod -R 755 ./src/app/static on Linux/macOS)

3. Confirm Volume Mounts Are Correct

Double-check that your host's static files are actually showing up in the Nginx container:

docker-compose exec nginx ls /flask-app/src/app/static

If this returns an empty list, your mount path is wrong. Verify that ./src/app/static exists on your host machine and matches the path in your docker-compose.yml. Remember, Linux containers are case-sensitive — make sure folder/file names match exactly.

4. Disable Flask's Static File Serving (For Production)

Even though Nginx should handle static files, make sure your Flask app isn't trying to serve them in production. If you have something like app.run(debug=True) in your code, that's for development only — in production, Gunicorn shouldn't serve static assets, so remove any debug mode or explicit static file serving config.

5. Restart Containers to Apply Changes

After updating your Nginx config or fixing permissions, restart your containers to ensure changes take effect:

docker-compose down && docker-compose up -d --build

Avoid just reloading Nginx unless you're sure the config is valid — a full restart ensures all mounts and configs are fresh.


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

火山引擎 最新活动