이번 포스트에서는 Dockerfile의 환경 ( dev or prod )에 따라 Nginx를 다르게 빌드하는 법을 알아보겠다.
1. dev.default.conf 생성
upstream client{
server client:3000;
}
upstream server{
server server:8080;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location /api {
proxy_pass http://server;
proxy_redirect off;
rewrite ^/api/(.*)$ /$1 break;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://client;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
2, prod.default.conf 생성
upstream client{
server client:3000;
}
upstream server{
server server:8080;
}
server {
listen 80;
listen [::]:80;
server_name [도메인이름];
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /var/www/certbot;
}
location / {
return 301 https://[도메인이름]$request_uri;
}
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name [도메인이름];
ssl_certificate /etc/nginx/ssl/live/[도메인이름]/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/[도메인이름]/privkey.pem;
#access_log /var/log/nginx/host.access.log main;
location /api {
proxy_pass http://server;
proxy_redirect off;
rewrite ^/api/(.*)$ /$1 break;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
}
location / {
proxy_pass http://client;
root /usr/share/nginx/html;
index index.html index.htm;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#allow deny ip
#allow 12.0.0.0;
#deny all;
}
3. .env 변수 추가
# Build 환경 변수 설정
BUILD_ENV=dev
4. docker compose 파일 수정
services:
nginx:
build:
context: ./docker/nginx
args:
BUILD_ENV: ${BUILD_ENV} // 이 부분 추가
ports:
- "80:80"
- "443:443"
restart: always
environment:
TZ: Asia/Seoul
BUILD_ENV: ${BUILD_ENV}
volumes:
- ${CERTBOT_WWW_DIR}:/var/www/certbot/:ro
- ${CERTBOT_CONF_DIR}:/etc/nginx/ssl/:ro
depends_on:
- client
- server
5. Dockerfile 수정
FROM nginx:latest
ARG BUILD_ENV=BUILD_ENV
COPY ./$BUILD_ENV.default.conf /etc/nginx/conf.d/default.conf
CMD ["nginx", "-g", "daemon off;"]
'Nginx' 카테고리의 다른 글
Nginx | SSL 인증 ( https ) 무료로 적용하기 Let's Encrypt | Docker Compose (0) | 2024.03.07 |
---|---|
Nginx | SSL 인증 ( https ) 무료로 적용하기 Let's Encrypt (0) | 2022.06.24 |