출처(Source) 및 참고
#[ nginx.conf file 경로 ]
# 아래의 3개중 하나
#1. /etc/nginx
#2. /usr/local/nginx
#3. /usr/local/etc/nginx
# [ *.conf ]
# 1. naming rule : *.conf (아무거나 상관없고 .conf 확장자로 끝내면 됌)
# 2. [ nginx.conf 경로] 에서 설명한 경로 아래의 conf.d directory 아래 작성하면 include 됌
# 기본적으로 nginx.conf 파일의 http block 내부에 include /etc/nginx/conf.d/*.conf 가 선언되어있어 자동 include 됌
# [ load balencer ]
# 1. default round robin 방식으로 동작
# 2. weight 방식으로 동작 가능. weight=N(N 은 숫자) 방식으로 표기 (weight 는 default 1).
upstream api_server {
server 127.0.0.1:8080;
server 127.0.0.1:8180;
keepalive 100;
}
#upstream api_server {
# server 127.0.0.1:8080 weight=5;
# server 127.0.0.1:8180;
# keepalive 100;
#}
# [ Reverse Proxy ]
server {
listen 80;
listen [::] 80;
server_name www.minhyuk-playground.io;
# Nginx 의 Version 을 오류 페이지 또는 서버 응답(Response) 헤더 필드에 명시하거나 명시하지 않는 옵션.
# Nginx 보안취약점이 발견되어 업데이트되더라도 현재 내가 사용하는 Nginx 의 버전은 최신이 아닐 수 있음.
# 이런 상황에서 Nginx Version 이 응답에 포함되어진다면 이를 보고 보안취약점을 이용해 공격을 가할 수 있음.
# 따라서 악의적인 공격을 방지하기 위해 off.
server_tokens off;
gzip on; # 서버 응답(Response) 압축 활성화/비활성화
gzip_comp_level 4; # 1~9 까지 설정 가능. 숫자가 클수록 압축률 증가 but 압축 속도 느려짐
gzip_min_length 1000; # 압축을 적용할 응답의 최소 길이. 기본값 20 byte. 이 값보다 작은 응답은 압축하지 않음.
# 기본적으로 Nginx 는 Proxy 요청(Proxy server 에서 오는 요청) 에 대한 응답을 압축하지 않음.
# 프록시 서버에서 요청이 전송되는 것인지에 대한 여부는 요청에 Via Header field 가 있는지 여부에 의해 결정됌.
# 이런 응답을 압축하고 싶을 때 gzip_proxied 지시자 사용.
# 다만, 프록시 서버에 의해 캐시되지 않는 요청에 대해서만 응답을 압축하는 것이 합리적.
# 설정값.
# 1. off : 프록시 요청에 대한 압축 사용 X
# 2. expired : 응답 헤더에 캐싱을 비활성화하는 값을 갖는 "Expires" 필드가 포함된 경우 압축 활성화
# 3. no-cache : 응답 헤더의 "Cache-Control" 헤더값이 "no-cache" 라면 압축 활성화
# 4. no-store : 응답 헤더의 "Cache-Control" 헤더값이 "no-store" 라면 압축 활성화
# 5. private : 응답 헤더의 "Cache-Control" 헤더값이 "private" 이라면 압축 활성화
# 6. no_last_modified: 응답 헤더가 "Last-Modified" 헤더 필드를 포함하고 있지 않을 경우 압축 활성화
# 7. no_etag : 응답 헤더에 "ETAG" 필드가 포함되어있지 않을 경우 압축을 활성화
# 8. auth : 요청 헤더에 "Authorization" 헤더가 포함된 경우 압축 활성화
# 9. any : 항상 프록시 요청에 대해 압축 활성화
gzip_proxied cache no-store private expired auth;
# gzip 을 적용할 mime-type 나열
gzip_types text/plain text/css text/xml application/json application/javascript application/x-javascript application/xml application/xhtml+xml application/xml+rss text/javascript;
#gzip_disable regex; # 지정된 정규 표현식과 일치하는 User-Agent header 와 매칭되는 요청은 압축하지 않음.
# static files
location / {
root /home/service/comp/public; # 정적 파일을 찾을 위치
try_files $uri /index.html; # 지정된 파일 또는 디렉토리가 존재하는지 확인.
index index.html index.htm; # index file 이름 지정
expires -1; # 이 페이지를 브라우저에 캐싱하지 않음
}
location /api {
proxy_pass http://api_server;
proxy_set_header X-Domain-Name $domain;
proxy_set_header Host $host;
# Nginx 가 수신한 클라이언트의 IP
# User -> Nginx -> Tomcat 일 경우 Tomcat 에서 X-Real-IP 는 User 의 IP
# User -> Proxy Server -> Nginx -> Tomcat 일 경우 Tomcat 에서 X-Real-IP 는 Proxy 서버의 IP
proxy_set_header X-Real-IP $remote_addr;
# 프록시 서버나 로드밸런서를 통해 웹 서버에 접속하는 클라이언트의 본래 IP 주소를 식별하기 위해 사용.
# 클라이언트와 서버 중간에 프록시나 로드 밸런서를 거치면서 서버 접근 로그에는 프록시나 로그 밸런서의 IP 주소만 담겨있음.
# 따라서 클라이언트의 본래 IP 주소를 알기 위해 X-Forwarded-For 요청 헤더가 사용됌
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /app1 {
proxy_pass http://127.0.0.1:3000
proxy_set_header X-Domain-Name $domain;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /app2 {
proxy_pass http://127.0.0.2:3000
proxy_set_header X-Domain-Name $domain;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 요고는 공부 하자
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
주석 제거
upstream api_server {
server 127.0.0.1:8080;
server 127.0.0.1:8180;
keepalive 100;
}
#upstream api_server {
# server 127.0.0.1:8080 weight=5;
# server 127.0.0.1:8180;
# keepalive 100;
#}
server {
listen 80;
listen [::] 80;
server_name www.minhyuk-playground.io;
server_tokens off;
gzip on;
gzip_comp_level 4;
gzip_min_length 1000;
gzip_proxied cache no-store private expired auth;
gzip_types text/plain text/css text/xml application/json application/javascript application/x-javascript application/xml application/xhtml+xml application/xml+rss text/javascript;
#gzip_disable regex; # 지정된 정규 표현식과 일치하는 User-Agent header 와 매칭되는 요청은 압축하지 않음.
# static files
location / {
root /home/service/comp/public;
try_files $uri /index.html;
index index.html index.htm;
expires -1;
}
location /api {
proxy_pass http://api_server;
proxy_set_header X-Domain-Name $domain;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /app1 {
proxy_pass http://127.0.0.1:3000
proxy_set_header X-Domain-Name $domain;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /app2 {
proxy_pass http://127.0.0.2:3000
proxy_set_header X-Domain-Name $domain;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 요고는 공부 하자
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}