Docker 编写 NGINX 反向代理 502
Docker Compose NGINX Reverse Proxy 502
我有以下设置,我一辈子都弄不明白为什么我无法连接到 api。
nginx.conf
worker_processes auto;
worker_rlimit_nofile 200000;
events {
use epoll;
accept_mutex on;
multi_accept on;
worker_connections 1024;
}
http {
error_log /etc/nginx/error_log.log warn;
client_max_body_size 20m;
server {
listen 80;
listen [::]:80;
location / {
proxy_pass http://api:8080/;
}
location /health {
return 200;
access_log off;
}
}
}
docker-compose.yml
version: "3.7"
services:
nginx:
container_name: nginx
image: "nginx:latest"
ports:
- "8000:80"
networks:
- internal_net
volumes:
- ./container/nginx.conf:/etc/nginx/nginx.conf
api:
container_name: api
build:
context: .
dockerfile: container/Dockerfile
expose:
- "8080"
networks:
- internal_net
depends_on:
- postgres
command: ["./wait-for-it.sh", "postgres:5432", "--timeout=60", "--", "./52-server-go"]
postgres:
container_name: postgres
image: "postgres:9.5-alpine"
expose:
- "5432"
networks:
- internal_net
networks:
internal_net:
driver: bridge
volumes:
container:
我的 api 在端口 8080 上设置为 运行,当我进入容器并 运行 针对该地址的 curl 请求时它起作用了。根据撰写文件,它应该将该地址公开给包括 nginx 在内的所有服务共享的本地撰写网络。
根据 nginx 配置,它应该将每个请求(/health
检查除外,它有效)传递给 api
服务。相反,返回的是来自 nginx 的 502。
我哪里搞混了?我做错了什么?
问题实际上出在我的 go 应用程序中。使用golang gorilla/mux
,只好改地址:
原装(破)
// Start server
address := "127.0.0.1:8080"
srv := &http.Server{
Handler: r,
Addr: address,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
修复
// Start server
address := "0.0.0.0:8080"
srv := &http.Server{
Handler: r,
Addr: address,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
我不知道为什么,但这解决了问题。
我有以下设置,我一辈子都弄不明白为什么我无法连接到 api。
nginx.conf
worker_processes auto;
worker_rlimit_nofile 200000;
events {
use epoll;
accept_mutex on;
multi_accept on;
worker_connections 1024;
}
http {
error_log /etc/nginx/error_log.log warn;
client_max_body_size 20m;
server {
listen 80;
listen [::]:80;
location / {
proxy_pass http://api:8080/;
}
location /health {
return 200;
access_log off;
}
}
}
docker-compose.yml
version: "3.7"
services:
nginx:
container_name: nginx
image: "nginx:latest"
ports:
- "8000:80"
networks:
- internal_net
volumes:
- ./container/nginx.conf:/etc/nginx/nginx.conf
api:
container_name: api
build:
context: .
dockerfile: container/Dockerfile
expose:
- "8080"
networks:
- internal_net
depends_on:
- postgres
command: ["./wait-for-it.sh", "postgres:5432", "--timeout=60", "--", "./52-server-go"]
postgres:
container_name: postgres
image: "postgres:9.5-alpine"
expose:
- "5432"
networks:
- internal_net
networks:
internal_net:
driver: bridge
volumes:
container:
我的 api 在端口 8080 上设置为 运行,当我进入容器并 运行 针对该地址的 curl 请求时它起作用了。根据撰写文件,它应该将该地址公开给包括 nginx 在内的所有服务共享的本地撰写网络。
根据 nginx 配置,它应该将每个请求(/health
检查除外,它有效)传递给 api
服务。相反,返回的是来自 nginx 的 502。
我哪里搞混了?我做错了什么?
问题实际上出在我的 go 应用程序中。使用golang gorilla/mux
,只好改地址:
原装(破)
// Start server
address := "127.0.0.1:8080"
srv := &http.Server{
Handler: r,
Addr: address,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
修复
// Start server
address := "0.0.0.0:8080"
srv := &http.Server{
Handler: r,
Addr: address,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
我不知道为什么,但这解决了问题。