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

Docker环境下Nginx反向代理配置问题求助

Hey Nicklas, let's tackle this Nginx reverse proxy issue for Portainer—sounds like you've already got the basics sorted (same network, port access, volume mappings working), so we just need to nail down the proxy config.

Fixing Nginx Reverse Proxy for Portainer in Docker

First, let's make sure your Nginx config has the right reverse proxy block. Since both containers are on the same Docker network, you should use Portainer's container name (or its service name if using Docker Compose) instead of localhost or your host IP in the proxy_pass directive—localhost inside the Nginx container points to itself, not your host or the Portainer container.

Step 1: Add a Server Block to nginx.conf

Inside the http block of your mapped nginx.conf, add a server block like this:

server {
    listen 80;
    server_name your-domain.local; # Or use _ to match any incoming host

    location / {
        proxy_pass http://portainer:9000; # Replace "portainer" with your actual Portainer container name
        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;
    }
}

Quick check: Run docker ps to confirm your Portainer container's name—if it's something like portainer-ce, use that instead of portainer in the proxy_pass line.

Step 2: Verify Docker Network Connectivity

Even though you mentioned no network issues, a quick sanity check won't hurt:

docker exec -it <your-nginx-container-name> ping portainer

If the ping goes through, the network is working as expected. If not, double-check that both containers are attached to the same custom Docker network (the default bridge network also supports container name resolution in newer Docker versions, but custom networks are more reliable).

Step 3: Test and Reload Nginx

After saving the config, reload Nginx inside the container to apply changes without restarting:

docker exec -it <your-nginx-container-name> nginx -s reload

Then try accessing http://your-host-machine-ip (or your domain if you set server_name and updated your local hosts file) — it should route directly to Portainer.

Common Pitfalls to Double-Check

  • Missing proxy headers: The proxy_set_header lines are critical for Portainer to recognize the correct request origin, especially if you plan to enable HTTPS later.
  • Custom base path in Portainer: If you've configured Portainer to use a base path like /portainer, adjust the location block to /portainer and add proxy_set_header X-Forwarded-Prefix /portainer; to the config.
  • Config syntax errors: Always validate your Nginx config before reloading with docker exec -it <your-nginx-container-name> nginx -t—this will catch typos or missing brackets immediately.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:40:03