nginx를 사용하여 역방향 프록시를 설정하려고 합니다. 문제는 localhost로 갈 때 umami 로그인 페이지로 리디렉션하고 싶다는 것입니다. 하지만 502 잘못된 게이트웨이가 표시됩니다. nginx를 올바르게 구성했는지 잘 모르겠습니다. 내 docker-compose.yml은 다음과 같습니다.
version: '2'
services:
nginx-proxy:
image: nginx:latest
container_name: nginx-proxy
ports:
- "80:80"
- "443:443"
volumes:
- ./my_proxy.conf:/etc/nginx/conf.d/default.conf:ro
umami:
image: ghcr.io/mikecao/umami:postgresql-latest
ports:
- "3000:3000"
environment:
DATABASE_URL: postgresql://umami:umami@db:5432/umami
DATABASE_TYPE: postgresql
HASH_SALT: 1234567890
depends_on:
- db
restart: always
networks:
- nginx-net
db:
image: postgres:12-alpine
environment:
POSTGRES_DB: umami
POSTGRES_USER: umami
POSTGRES_PASSWORD: umami
volumes:
- ./sql/schema.postgresql.sql:/docker-entrypoint-initdb.d/schema.postgresql.sql:ro
- umami-db-data:/var/lib/postgresql/data
restart: always
networks:
- nginx-net
volumes:
umami-db-data:
conf:
vhost:
html:
dhparam:
certs:
networks:
nginx-net:
external:
name: nginx-net
여기 default.conf 위에 마운트하는 my_proxy.conf가 있습니다.
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://localhost:3000/login;
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;
}
}
다음은 내가 사용한 SQL 폴더 링크입니다. https://drive.google.com/file/d/1lvyLUve0SGpT1tXlsVoFwPmbmb75n8ER/view?usp=sharing
다음은 로그 파일의 짧은 화면입니다.
2022/12/13 17:58:09 [notice] 1#1: using the "epoll" event method
2022/12/13 17:58:09 [notice] 1#1: nginx/1.23.2
2022/12/13 17:58:09 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/12/13 17:58:09 [notice] 1#1: OS: Linux 5.15.0-56-generic
2022/12/13 17:58:09 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1024:524288
2022/12/13 17:58:09 [notice] 1#1: start worker processes
2022/12/13 17:58:09 [notice] 1#1: start worker process 21
2022/12/13 17:58:09 [notice] 1#1: start worker process 22
2022/12/13 17:58:09 [notice] 1#1: start worker process 23
2022/12/13 17:58:09 [notice] 1#1: start worker process 24
2022/12/13 17:58:09 [notice] 1#1: start worker process 25
2022/12/13 17:58:09 [notice] 1#1: start worker process 26
2022/12/13 17:58:09 [notice] 1#1: start worker process 27
2022/12/13 17:58:09 [notice] 1#1: start worker process 28
2022/12/13 17:58:10 [error] 21#21: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /nginx_status HTTP/1.1", upstream: "http://127.0.0.1:3000/loginnginx_status", host: "172.18.0.1"
2022/12/13 17:58:10 [warn] 21#21: *1 upstream server temporarily disabled while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /nginx_status HTTP/1.1", upstream: "http://127.0.0.1:3000/loginnginx_status", host: "172.18.0.1"
2022/12/13 17:58:10 [error] 21#21: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /nginx_status HTTP/1.1", upstream: "http://127.0.0.1:3000/loginnginx_status", host: "172.18.0.1"
2022/12/13 17:58:10 [warn] 21#21: *1 upstream server temporarily disabled while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /nginx_status HTTP/1.1", upstream: "http://127.0.0.1:3000/loginnginx_status", host: "172.18.0.1"
172.20.0.1 - - [13/Dec/2022:17:58:10 +0000] "GET /nginx_status HTTP/1.1" 502 157 "-" "Go-http-client/1.1" "-"
172.20.0.1 - - [13/Dec/2022:17:58:20 +0000] "GET /nginx_status HTTP/1.1" 502 157 "-" "Go-http-client/1.1" "-"
2022/12/13 17:58:20 [error] 22#22: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /nginx_status HTTP/1.1", upstream: "http://127.0.0.1:3000/loginnginx_status", host: "172.18.0.1"
2022/12/13 17:58:20 [warn] 22#22: *4 upstream server temporarily disabled while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /nginx_status HTTP/1.1", upstream: "http://127.0.0.1:3000/loginnginx_status", host: "172.18.0.1"
2022/12/13 17:58:20 [error] 22#22: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /nginx_status HTTP/1.1", upstream: "http://127.0.0.1:3000/loginnginx_status", host: "172.18.0.1"
2022/12/13 17:58:20 [warn] 22#22: *4 upstream server temporarily disabled while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /nginx_status HTTP/1.1", upstream: "http://127.0.0.1:3000/loginnginx_status", host: "172.18.0.1"
이 문제를 해결하려면 어떻게 해야 할까요?
답변1
모든 컨테이너에는 자체 네트워크가 있습니다. localhost
백엔드 주소를 제공 함으로써 nginx는 자체 컨테이너 내부에서 이 포트에 액세스하려고 시도합니다. 대신 다른 컨테이너를 제공해야 합니다.
proxy_pass http://umami:3000/login;
컨테이너의 이름은 다른 컨테이너 내에서 "호스트 이름"으로 사용할 수 있으므로 그 이상은 수행할 수 없습니다.
포트 3000에 대한 포트 정의를 제거할 수 있습니다. 이는 컨테이너 외부에서 포트에 액세스하는 데만 필요합니다.