memos/Home Server/Network/nginx for reverse proxy.md

92 lines
1.8 KiB
Markdown
Raw Normal View History

2024-08-25 15:21:39 +00:00
[[DNS/CoreDNS|CoreDNS]]를 설정한 뒤 진행했음
2024-08-26 11:55:58 +00:00
# 1. Install using docker
## 1.1. Directory structure
2024-08-25 15:21:39 +00:00
```dirtree
- /mnt/md0/infra
- /coredns
- 파일 생략
- /nginx
- /conf.d
- default.conf
- locations
- ns1.conf
- nginx.conf
- .env
- compose.yml
```
2024-08-26 11:55:58 +00:00
## 1.2. dotenv file
2024-08-25 15:21:39 +00:00
/.env
```
BASE_PATH=/mnt/md0/infra
```
2024-08-26 11:55:58 +00:00
## 1.3. Docker compose
2024-08-25 15:21:39 +00:00
/compose.yml
```yml
name: infrastructure
service:
nginx:
image: nginx:1.26-alpine3.20
container_name: nginx-reverse-proxy
restart: always
ports:
- 80:80
volumes:
- ${BASE_PATH}/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ${BASE_PATH}/nginx/conf.d:/etc/nginx/conf.d:ro
networks:
- infra
depends_on:
- dns
networks:
infra:
driver: bridge
```
2024-08-26 11:55:58 +00:00
# 2. nginx configuration
## 2.1. /nginx/nginx.conf
2024-08-25 15:21:39 +00:00
기본 설정 파일을 그대로 사용함
2024-08-26 11:55:58 +00:00
## 2.2. /nginx/conf.d/default.conf
2024-08-25 15:21:39 +00:00
```nginx
server {
listen 80;
server_name localhost;
error_page 404 500 502 053 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# 추후 다른 세팅을 쉽게 추가하기 위해 변경
include /etc/nginx/conf.d/locations/*.conf;
}
```
2024-08-26 11:55:58 +00:00
## 2.3. subdomain
2024-08-25 15:21:39 +00:00
```nginx
server {
listen 80;
server_name gitea.home.server;
location / {
proxy_set_header Connection $http_connection;
proxy_set_header Upgrade $http_upgrade;
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;
proxy_pass http://gitea:3000;
}
}
```
2024-08-26 11:55:58 +00:00
## 2.4. reload
2024-08-25 15:21:39 +00:00
```shell
2024-08-25 15:35:59 +00:00
docker exec -it nginx-reverse-proxy nginx -s reload
2024-08-25 15:21:39 +00:00
```