Cloud/Nginx
Nginx
jheaon
2024. 7. 25. 13:27
Nginx
nginx는 오픈 소스 웹 서버로 고성능 및 확장가능한 프로그램을 제공하기 위해 설계된 소프트웨어이다. 주로 아래와 같은 용도로 사용한다.
- 웹 서버: 정적 및 동적 웹 콘텐츠를 서빙하는데 사용한다. 정적 콘텐츠를 빠르게 처리할 수 있고, 요청에 따라 동적 웹 응용 프로그램 (WAS)로 연결하여 처리 할 수도 있다. WAS에서 처리하는 정적 콘텐츠를 웹 서버가 제공 함으로써 WAS에 일어나는 작업량을 낮춰 부담을 줄여주는 용도로 사용한다.
- 리버스 프록시 : 백엔드 서버 앞단에서 클라이언트 요청을 전달하는 역활을 수행하며, 부하 분산 및 고가용성을 실현하는데 사용된다.
- SSL/TLS 지원 : HTTPS을 지원하여 웹 서버 보안을 강화하는데 역활을 한다.
- 높은 성능, 커뮤니티 지원이 높다.
Nginx.conf
NGINX의 주 설정 파일은 /etc/nginx/nginx.conf로 해당 파일은 전체 서버의 기본 설정을 포함하고 있으며, 서버 블록을 통해 개별 웹사이트의 설정을 관리 할 수 있다.
📁 nginx.conf
worker_processes auto;
events {
worker_connections 1024;
}
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;
index index.html index.htm;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
location /api/ {
proxy_pass http://backend;
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;
}
error_page 404 /404.html;
location = /404.html {
root /var/www/html;
}
rewrite ^/old/(.*)$ /new/$1 permanent;
}
server {
listen 443 ssl;
server_name secure.example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
root /var/www/secure_html;
index index.html index.htm;
access_log /var/log/nginx/secure.example.com.access.log;
error_log /var/log/nginx/secure.example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
root /var/www/secure_html;
}
}
- work_processes: nginx가 사용할 워커 프로세스의 수를 설정한다. Nginx가 실제 연결할 수 있는 최대 연결수는 connections x processes로 결정된다.
worker_processes auto;
- events: 이벤트 관련 설정을 포함한다.
- worker_connections: 각 워커 프로세스가 동시에 처리할 수 있는 최대 연결 수를 지정한다. 이 설정은 NGINX가 동시에 얼마나 많은 클라이언트 요청을 처리할 수 있는지를 결정짓는 중요한 요소이다. 서버의 자원에 따라 부족해 질 수 있으므로 적절한 값을 지정하여야 한다.
events {
worker_connections 1024;
}
- upstream: 로드 밸런싱을 위해 백엔드 서버 그룹을 정의한다.
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
- server: 하나의 서버 블록을 정의하며. 특정 포트 및 도메인에 대한 설정을 포함한다.
- listen: 서버가 수신할 포트 번호와 주소를 지정한다.
- server_name: 서버의 도메인 이름을 지정한다. 여러 도메인 이름을 쉼표로 구분하여 지정할 수 있다.
- root: 요청된 파일의 기본 디렉터리를 지정한다.
- index: root경로를 기반으로,인덱스 파일을 지정한다.
- location: 특정 URL 패턴에 대한 설정을 정의한다.
- error_page: 오류 페이지를 사용자 정의한다.
- access_log 및 error_log:접근 로그 및 에러 로그 파일의 위치와 형식을 설정한다.
- proxy_pass: 프록시 요청을 다른 서버로 전달한다.
- rewrite: URL 리다이렉션을 정의한다.
- ssl_certificate 및 ssl_certificate_key: SSL 인증서와 키 파일의 경로를 지정한다.