nginx 1.23.3 기준 docker container 내부에 있는 기본 설정 파일
/etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/nginx.conf 설정 파일 구조
user nginx; events {
worker_connections 1024;
}user : nginx의 worker 프로세스에서 사용되는 user 정보worker_processes : worker 프로세스의 수를 정의error_log 파일 위치 레벨 : 로깅 설정pid : 메인 프로세스의 PID가 저장되는 파일 위치, nginx 종료 시 사용됨events : 연결 처리에 영향을 주는 지시문을 event 안에 정의하게 됨. worker_connections : worker process가 열 수 있는 동시 접속 최대수http : HTTP 트래픽 처리를 위한 지시문을 http 안에 정의include : 외부 파일을 설정에 포함시킴default_type : response의 기본 MIME 유형 정의sendfile : sendfile() 사용할지 말지 설정on : nginx에서 정적파일을 보내도록 설정on으로 설정해주자off 해주자sendfile_max_chunk 설정으로 sendfile() call 하나 당 데이터 전송량을 제한해둘 수 있음tcp_nopush : TCP_NOPUSH socket option을 활성화 할지 말지 설정on 상태일 때 사용 가능off인데 on 으로 바꾸면 tcp_nodelay : TCP_NODELAY를 활성화 할지 말지 설정on tcp_nopush와 반대로 작용keepalive_requests : 하나의 keep-alive 연결을 통해 처리할 수 있는 최대 요청 수를 설정keepalive_timeout : keep-alive 클라이언트 연결이 서버에 열려있는 시간을 제한log_format 이름 형식 : ~형식을 가진 log_format을 이름이라고 한다고 정의하는 곳인듯access_log 파일 위치 format 이름 : access_log를 기록할 파일 위치와 로그 형식을 정의. log_format에서 선언한 이름을 여기에 적는듯on : gzip 압축 방식 사용/etc/nginx/conf.d/default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
/etc/nginx/conf.d/default.conf 설정 파일 구조
http core 모듈
server : 가상 서버(virtual server)를 설정
listen : 서버가 request를 받을 IP address와 port를 설정하거나 UNIX-domain 소켓의 경로를 설정
listen 127.0.0.1:8000;
listen 127.0.0.1;
listen 8000;
listen *:8000;
listen localhost:8000;
# IPv6 주소
listen [::]:8000;
listen [::1];
# UNIX-domain 소켓은 unix: 접두어를 붙여야함
listen unix:/var/run/nginx.sock;
server_name : 가상 서버의 이름을 설정
# example.com이 기본 서버 이름
server {
server_name example.com www.example.com;
}
# 이름의 처음과 마지막 부분에 *가 붙을 수 있음 -> wildcard 이름
server {
server_name example.com *.example.com www.example.*;
}
# 첫번째 예제와 두번째 예제를 합치면 아래처럼 요약 가능
server {
server_name .example.com;
}
location : request URI에 따라 설정
접두사나 정규식으로 정의 가능
주어진 request와 일치하는 location을 찾기 위해서 nginx는 접두사 문자열을 사용해 정의된 위치를 확인
~* : 대소문자를 구분하지 않는 일치
~ : 대소문자를 구분하는 일치
= : URI의 정확한 위치가 매칭되게 하고 싶을 때 지정
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /documents/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
위 예시에 해당하는 요청
/ 요청은 A 설정/index.html 요청은 B설정/documents/document.html 요청은 C/images/1.gif 요청은 D. E에도 해당되지만 D가 E보다 앞에 있어서 D가 됨/documents/1.jpg 요청은 E만약 location 이 /로 끝나고 요청이 proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, or grpc_pass 중에 하나라면 특별한 방식이 실행됨
/가 없는 요청에 대한 응답으로 301이 포함된 permanent redirection이 /가 추가되어서 요청된 URI로 반환됨=를 사용해서 정확한 위치가 일치하도록 설정하면 됨location /user/ {
proxy_pass http://user.example.com;
}
location = /user {
proxy_pass http://login.example.com;
}
proxy_pass URL : 프록시 서버의 프로토콜 및 주소와 위치를 매핑해야 하는 URI를 설정http or httpsupstream directive를 사용해 server group으로 지정할 수 있음upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}proxy_pass가 URI로 지정되어 있으면 request가 이 서버로 전달됨# http://localhost/name -> http://127.0.0.1/remote/
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}proxy_pass가 URI로 지정되어 있지 않으면 request URI가 클라이언트에서 보냈던 형식 그대로 서버로 전달됨# http://localhost/some/path -> http://127.0.0.1/some/path
location /some/path/ {
proxy_pass http://127.0.0.1;
}location을 표현하고 named location이 안에 있을 때 : proxy_pass 에 URI를 제외해야 함rewrite를 사용해 프록시된 location 안에서 URI가 변경 되고 request(break)를 처리할 때 같은 구성일 때proxy_pass) directive에 적힌 URI는 무시되고 전체 변경된 request URI가 서버로 전달됨 location /name/ {
rewrite /name/([^/]+) /users?name=$1 break;
proxy_pass http://127.0.0.1;
}proxy_pass에 변수가 사용될 때proxy_pass) directive에 적힌 URI가 있으면 원래 request URI를 대체하고 그대로 서버에 전달location /name/ {
proxy_pass http://127.0.0.1$request_uri;
}proxy_redirect : 프록시 서버 응답의 location과 refresh 헤더 필드에서 변경되어야 하는 텍스트를 설정Location: http://localhost:8000/two/some/uri/로 리턴했다고 가정하면proxy_redirect http://localhost:8000/two/ http://frontend/one/;: 이 directive는 Location: http://frontend/one/some/uri/ 으로 변경해줌기본값 : default
location과 proxy_pass의 parameter를 사용함location /one/ {
proxy_pass http://upstream:port/two/;
proxy_redirect default;
location /one/ {
proxy_pass http://upstream:port/two/;
proxy_redirect http://upstream:port/two/ /one/;
proxy_pass가 변수를 사용하면 default 값을 사용할 수 없음 off 값으로 설정하면 이전 설정 레벨에서 상속받은 proxy_redirect 영향을 취소함
proxy_set_header : 프록시 서버로 전달되는 request header를 재정의하거나 추가할 수 있음proxy_set_header Host $proxy_host;
proxy_set_header Connection close;$host 변수는 request header 필드의 "Host"에 있는 서버 이름과 같거나 이 필드가 없으면 primary server 이름이 됨proxy_set_header Host $host;proxy_set_header Host $host:$proxy_port;참고