[NGINX] Nginx 이해하기 (리버스 프록시)
개인 공부 목적으로 작성된 글입니다. 왜곡된 내용이 포함되어 있습니다.
Nginx
Nginx는 HTTP 웹서버, 리버스 프록시, 로드벨런서를 지원하는 기술이다. 글쓴이의 경우 리버스 프록시 서버의 용도로 해당 기술을 사용하였다.
설치는 다음 Nginx 문서를 참고 하였다.
https://nginx.org/en/linux_packages.html#Ubuntu
nginx: Linux packages
nginx: Linux packages Supported distributions and versions nginx packages are available for the following Linux distributions and versions: RHEL and derivatives Version Supported Platforms 8.x x86_64, aarch64/arm64 9.x x86_64, aarch64/arm64 Debian Version
nginx.org
Nginx는 기본적으로 마스터 프로세스를 실행하기 위한 루트권한이 필요하다. sudo 또는 루트 계정으로 nginx를 실행한다.
sudo nginx
Nginx를 실행하면 80번 포트에 Nginx 기본 HTML Welcome 페이지를 확인할 수 있다.
ps 명령어를 통해 Nginx 명령어로 실행되는 프로세스를 확인해보자
root@ip-10-0-0-00:/etc/nginx# ps -ef | grep nginx
root 119693 1 0 16:52 ? 00:00:00 nginx: master process nginx
nginx 119694 119693 0 16:52 ? 00:00:00 nginx: worker process
nginx 119695 119693 0 16:52 ? 00:00:00 nginx: worker process
root 119734 119685 0 17:12 pts/1 00:00:00 grep --color=auto nginx
root 계정에서 실행한 Nginx 마스터 프로세스와 마스터 프로세스를 부모 프로세스로 가지는 자식 프로세스인, 워커 프로세스를 확인할 수 있다. 워커 프로세스의 경우 ngnix 계정을 통해 실행되는 것을 확인할 수 있다.
Nginx 디렉토리 (설정 파일)
conf.d/default.conf
server {
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;
#}
}
Nginx는 공백과 세미콜론(;)으로 분류하여 사용되는 simple directive와 중괄호 기준으로 분류되는 block directive를 문법으로 사용한다. 기본 설정 파일을 보면 이해할 수 있다.
conf.d 디렉토리에는 기본 HTTP 서버 설정 파일인 default.conf파일을 확인할 수 있다.
파일에 주석을 제외하고, 기본 주소에 대한 index.html 주소와 예외 처리에 대한 50x.html의 주소설정을 확인할 수 있다 (root directive를 통해 로컬 파일 시스템에서 요청 파일이 설정되어 있다)
Nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /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;
}
Nginx.conf는 Nginx 기본 설정 파일로, 모든 설정에 대한 진입점이다. Nginx에서 사용하는 유저 정보, 워커 프로세스 갯수와 같이 설정 정보를 명세하고 마스터 프로세스가 이를 실행한다.
include /etc/nginx/conf.d/*.conf 와 같이 http 서버 설정 파일을 include하는 것을 볼 수 있고, /etc/nginx/mime.types을 통해 클라이언트에게 제공할 파일의 형식을 확인할 수 있다.
https://nginx.org/en/docs/http/ngx_http_limit_conn_module.html
Module ngx_http_limit_conn_module
Module ngx_http_limit_conn_module The ngx_http_limit_conn_module module is used to limit the number of connections per the defined key, in particular, the number of connections from a single IP address. Not all connections are counted. A connection is coun
nginx.org
리버스 프록시
리버스 프록시란 클라이언트의 요청을 받은 웹서버 대신 요청을 받아 웹서버에게 요청을 전달하는 프록시 서버를 말한다.
리버스 프록시를 사용함으로써, 동일한 엔드포인트에 대해서 로드 밸런싱을 제공할 수 있고, SSL 인증처리와 같이 보완 설정을 추가할 수 있다. 글쓴이의 경우 8080포트에서 대기중인 스프링 부트 서버로 리버스 프록시서버를 구성하였다.
아래 Nginx 문서를 참고하였다.
https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/
NGINX Reverse Proxy | NGINX Documentation
MyF5 Your key to everything F5, including support, registration keys, and subscriptions
docs.nginx.com
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:8080;
}
#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;
}
기존의 default.conf 가 아닌 새로운 conf 파일을 생성하였다. 모든 http 요청에 대해서 8080 포트로 프록시 패스 설정을 해준다(만약 prefix를 추가하고 싶다면 location /path 와 같이 진입점에 대한 리버스 프록시 경로를 생성하거나 목적지 경로를 proxy_pass http://127.0.0.1:8080/path 와 같이 추가할 수 있다)
HTTPS
HTTPS를 사용하기 위해서는 인증서를 다운받아야한다. 인증서는 Certbot를 통해 다운 받았다.
아래 문서를 참고 하였다
https://certbot.eff.org/instructions?ws=nginx&os=ubuntufocal
Certbot Instructions
Certbot Instructions
certbot.eff.org
Certbot은 Nginx에 대한 https 설정을 인증서와 함께 제공해주고 있는데, 글쓴이는 직접 Nginx 설정을 위해 인증서만 다운 받았다.
sudo certbot certonly --nginx
명령어 실행후 도메인 네임과 기타 주소를 입력하면 인증서를 생성할 수 있다. /etc/letsencrypt/live/에 인증서(fullchain)과 private key를 확인할 수 있다.
생성한 인증서와 비밀키를 Nginx 설정에 추가한다
server {
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/domain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
변경된 설정을 적용하기 위해 Nginx을 reload하면 적용되는 것을 확인할 수 있다. 이때 reload는 restart와 달리 마스터, 워커 프로세스를 유지한대로 설정을 반영한다.
nginx -t
nginx -s reload