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.
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 psto confirm your Portainer container's name—if it's something likeportainer-ce, use that instead ofportainerin theproxy_passline.
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_headerlines 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 thelocationblock to/portainerand addproxy_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

