我有一个golang应用程序,它在本地运行得很好(我发送请求并得到正确的响应)。但是当我尝试在带有nginx代理服务器的容器中运行这个应用程序时,我得到了一个502错误:
[错误代码] 31#31:*1连接()失败(111:连接被拒绝),客户端:172.20.0.1,服务器:多边形。应用程序。本地,请求:“GET /v1/信用点HTTP/1.1”,上游:“http://172.20.0.5:8080/v1/credits“,主机:“多边形.应用程序.本地”
我已经尝试了来自谷歌的不同解决方案来修复这个问题,但还没有修复。
这是我的配置:
- docker-compose.yaml
version: "3.9"
services:
nginx:
image: nginx:alpine
volumes:
- ${PROJECT_DIR}/deployments/nginx.conf:/etc/nginx/conf.d/default.conf:delegated
- ${PROJECT_DIR}/configs/ssl:/etc/nginx/ssl/:delegated
ports:
- "80:80"
- "443:443"
swagger_ui:
image: swaggerapi/swagger-ui
environment:
SWAGGER_JSON: /spec/api.swagger.yaml
volumes:
- ${PROJECT_DIR}/api/openapi-spec/api.swagger.yaml:/spec/api.swagger.yaml
credit_server:
build:
context: ..
dockerfile: ${PROJECT_DIR}/deployments/Dockerfile
args:
BUILD_APP_NAME: credit-server
depends_on:
credit_service_db:
condition: service_healthy
credit_service_db:
image: mysql:8.0
container_name: credit_service_db
restart: always
environment:
MYSQL_DATABASE: credit_service
MYSQL_USER: credit_service
MYSQL_PASSWORD: credit_service
MYSQL_ROOT_PASSWORD: credit_service
ports:
- '3306:3306'
expose:
- '3306'
healthcheck:
test: [ 'CMD-SHELL', 'mysqladmin ping -h localhost' ]
interval: 5s
timeout: 20s
retries: 10
- nginx.conf
map $microservice $upstream {
credits credit_server:8080;
swagger swagger_ui:8080;
}
server {
listen 443 http2 ssl;
server_name polygon.application.local;
server_tokens off;
client_max_body_size 16m;
root /dev/null;
resolver 127.0.0.11 valid=30s;
ssl_certificate /etc/nginx/ssl/crt.pem;
ssl_certificate_key /etc/nginx/ssl/private.key.pem;
location / {
set $microservice "swagger";
proxy_pass http://$upstream;
}
location ~ ^/v1/(?<microservice>[\w\-]+) {
proxy_http_version 1.1;
proxy_set_header Host $http_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 http;
proxy_set_header X-Frame-Options SAMEORIGIN;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS,PATCH';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Access-Control-Allow-Headers,Access-Control-Allow-Origin,Authorization';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
add_header 'X-Microservice' '$microservice';
add_header 'X-Proxy-Pass' '$upstream';
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS,PATCH';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Access-Control-Allow-Headers,Access-Control-Allow-Origin,Authorization';
proxy_pass http://$upstream;
}
}
- credit_server的停靠文件
FROM alpine:latest
ARG BUILD_APP_NAME
ENV PROJECT_DIR=/go
RUN apk add tzdata
COPY ./build/${BUILD_APP_NAME} ${PROJECT_DIR}/bin/app
COPY ./configs ${PROJECT_DIR}/configs
COPY ./internal/migrations ${PROJECT_DIR}/migrations
CMD ${PROJECT_DIR}/bin/app -c ${PROJECT_DIR}/configs/config.yml -m container -p ${PROJECT_DIR}/migrations/
所有容器启动并正常工作。x1c 0d1x
我也会从斯瓦格和 Postman 那里寄我的请求
2条答案
按热度按时间68de4m5k1#
您收到的错误来自NGINX,看起来它正在寻找正确的容器进行通信,但该容器中的端口8080上没有任何侦听。这可能是因为您错误配置了侦听端口,也可能是因为应用程序需要一些时间来启动,因此在NGINX尝试连接时,它不接受连接。
尝试将
depends_on
声明放入nginx部分pxy2qtax2#
默认情况下,我的golang应用程序的rest-host是127.0.0.1或者只是localhost,但是在nginx中我们指定listen 80,它的意思是0.0.0.0:80,当我把我的host重写为0.0.0.0时,它就可以工作了!