-
Nginx - default.confInfra/서버 2023. 1. 6. 13:23
지시어
예시 nginx : default.conf
user nginx; # 프로세스의 실행되는 권한. 보안상 root를 사용하지 않습니다. worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; upstream docker-server { server server:8080; } server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html =404; } location /api { proxy_pass http://docker-server; proxy_redirect off; 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 /socket { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_pass http://docker-server; } } 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; server_tokens off; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; }
1.worker_processes : 몇 개의 워커 프로세스를 생성할 것이지 지정하는 지시어이다. 예를 들어 1로 설정하면 모든 요청을 하나의 프로세스로 실행하겠다는 뜻으로, 만약 해당하는 일을 처리하는 시스템의 CPU가 멀티코어 시스템일 경우 하나의 코어 만으로 요청을 처리하겠다는 의미이다. 보통 auto로 놓는 경우가 많다.
2. error_log : 로그 레벨을 설정하는 지시어. 로그레벨은 [debug | info | notice | warn | error | crit ]같은 종류가 있다.
3. pid : nginx의 마스터 프로세스 id정보가 저장된다.
event 블록
4. worker_connections : 하나의 프로세스가 처리할 수 있는 커넥션의 숫자
http블록
5. include : 옵션 항목을 설정해둔 파일의 경로를 지정하는데 보통 파일 확장명고 MIME 타입 목록을 지정한다.
6. default_type : 옷텟 스트림 기반의 http를 사용한다는 지시어
upstream 블록
origin 서버라고도 하는데, 위 예시에서는 WAS, 웹어플리케이션 서버를 의미하며 nginx는 downstream에 해당한다고 할 수 있다. nginx와 연결한 웹 어플리케이션 서버를 지정하는데 사용된다. 하위에 있는 server 지시어는 연결할 웹 어플리케이션 서버의 host주소:포트를 지정한다.
server블록
하나의 웹 사이트를 선언하는데 사용된다. server블록이 여러개 이면 한 개의 머신(호스트)에 여러 웹사이트를 서빙할 수 있다. 이러한 개념을 가상 호스트라고 한다. (실제로 호스트는 1개인데, 여러 개인것 처럼 보이게 만든다.)
7. listen: 해당 웹 사이트가 바라보는 포트를 의미한다.
8. server_name : 클라이언트가 접속하는 서버(주로 도메인)가 주로 이것과 실제로 들어온 request의 header에 명시된 값이 일치하는지를 확인해서 server를 분기해준다.
location 블록
server블록 안에서 특정 웹 사이트의 url을 처리하는데 사용한다. 예를들어 https://web.com/internal과 https://web.com/external로 접근하는 요청을 다르게 처리하고 싶을 때 사용합니다.
내부의 root는 웹사이트가 바라보는 root의 경로를 의미한다.
10. server_tokens: 헤더에 nginx버전을 숨기는 기능을 합니다. 보안상 off로 설정을 권장합니다.
11. keepalive_timeout:접속시 커넥션 유지 시간을 지정합니다.