Elastic Beanstalk이 자동으로 생성하는 nginx설정 뜯어보기

· 유창연 · 6 min read

AWS Elastic Beanstalk이 자동으로 생성하는 nginx 설정 파일을 분석하고, 각 디렉티브의 역할과 리버스 프록시 설정을 상세히 설명합니다.

Elastic Beanstalk이 자동으로 생성하는 nginx설정 뜯어보기

user                    nginx;
error_log               /var/log/nginx/error.log warn;
pid                     /var/run/nginx.pid;
worker_processes        auto;
worker_rlimit_nofile    200000;

events {
    worker_connections  1024;
}

http {
   server_tokens off;

    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"';
    include       conf.d/*.conf;
    map $http_upgrade $connection_upgrade {
        default     "upgrade";
    }
    server {
        listen        80 default_server;
        access_log    /var/log/nginx/access.log main;
        client_header_timeout 60;
        client_body_timeout   60;
        keepalive_timeout     60;
        gzip                  off;
        gzip_comp_level       4;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
        # Include the Elastic Beanstalk generated locations
        include conf.d/elasticbeanstalk/*.conf;
    }
}
  • user nginx;: Nginx 프로세스를 nginx 사용자 권한으로 실행합니다.
  • error_log /var/log/nginx/error.log warn;: 경고 수준 이상의 에러를 /var/log/nginx/error.log 파일에 기록합니다.
  • pid /var/run/nginx.pid;: Nginx의 프로세스 ID를 /var/run/nginx.pid에 저장합니다.
  • worker_processes auto;: 시스템의 CPU 코어 수에 맞게 워커 프로세스 수를 자동으로 설정합니다.
  • worker_rlimit_nofile 200000;: 각 워커 프로세스가 열 수 있는 파일의 최대 수를 200,000으로 설정합니다.

events 블록:

  • worker_connections 1024;: 각 워커 프로세스가 동시에 처리할 수 있는 최대 연결 수를 1,024로 설정합니다.

http 블록:

  • server_tokens off;: 응답 헤더에 Nginx 버전 정보를 숨깁니다.
  • include /etc/nginx/mime.types;: 파일 확장자에 따른 MIME 타입을 정의한 파일을 포함합니다.
  • default_type application/octet-stream;: MIME 타입이 지정되지 않은 파일의 기본 타입을 application/octet-stream으로 설정합니다.
  • log_format: 로그의 형식을 정의합니다.
  • include conf.d/*.conf;: conf.d 디렉토리 내의 모든 .conf 파일을 포함합니다.
  • map $http_upgrade $connection_upgrade: HTTP 업그레이드 요청에 따라 Connection 헤더를 설정합니다.

server 블록:

  • listen 80 default_server;: 포트 80에서 기본 서버로 요청을 수신합니다.
  • access_log /var/log/nginx/access.log main;: 접근 로그를 지정된 형식으로 기록합니다.
  • client_header_timeout, client_body_timeout, keepalive_timeout: 각각 클라이언트 헤더, 바디, 그리고 keep-alive 연결의 타임아웃을 60초로 설정합니다.
  • gzip off;: Gzip 압축을 비활성화합니다. 아래의 gzip_comp_level, gzip_types 설정은 정의되어 있지만, gzip off;에 의해 실제로는 적용되지 않습니다. 필요 시 gzip on;으로 변경하면 아래 설정이 활성화됩니다.
  • gzip_comp_level 4;: Gzip 압축 수준을 4로 설정합니다.
  • gzip_types: Gzip 압축을 적용할 MIME 타입을 지정합니다.
  • include conf.d/elasticbeanstalk/*.conf;: conf.d/elasticbeanstalk 디렉토리 내의 모든 .conf 파일을 포함합니다.

location 블록

location / {
    proxy_pass          http://127.0.0.1:5000;
    proxy_http_version  1.1;
    proxy_set_header    Connection          $connection_upgrade;
    proxy_set_header    Upgrade             $http_upgrade;
    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 / { … }: 루트 경로(/)로 들어오는 모든 요청에 대해 해당 블록 내의 지시어를 적용합니다.
  • proxy_pass http://127.0.0.1:5000;: 들어오는 요청을 로컬호스트의 포트 5000에서 실행 중인 어플리케이션 서버로 전달합니다. 이를 통해 Nginx는 리버스 프록시로 동작하며, 클라이언트의 요청을 백엔드 애플리케이션 서버로 프록시합니다.
  • proxy_http_version 1.1;: 백엔드 서버로의 프록시 요청 시 HTTP 버전 1.1을 사용합니다. 이는 지속적인 연결(keep-alive) 및 업그레이드된 프로토콜(WebSocket 등)을 지원하기 위해 필요합니다.
  • proxy_set_header 지시어들: 백엔드 서버로 전달되는 요청의 헤더를 설정합니다.
    • Connection $connection_upgrade;: $connection_upgrade 변수의 값에 따라 Connection 헤더를 설정합니다. 일반적으로 WebSocket과 같은 프로토콜 업그레이드를 지원하기 위해 사용됩니다.
    • Upgrade $http_upgrade;: Upgrade 헤더를 클라이언트의 Upgrade 헤더 값($http_upgrade)으로 설정합니다. 이는 프로토콜 업그레이드 요청 시 필요합니다.
    • Host $host;: Host 헤더를 원본 요청의 호스트 헤더 값($host)으로 설정합니다. 이를 통해 백엔드 서버는 원래의 호스트 정보를 알 수 있습니다.
    • X-Real-IP $remote_addr;: 클라이언트의 실제 IP 주소를 X-Real-IP 헤더에 설정하여 백엔드 서버로 전달합니다. 이는 백엔드 서버가 클라이언트의 실제 IP를 인식할 수 있도록 합니다.
    • X-Forwarded-For $proxy_add_x_forwarded_for;: 클라이언트의 원래 IP 주소를 X-Forwarded-For 헤더에 추가합니다. 이를 통해 백엔드 서버는 요청이 어떤 경로를 통해 왔는지 추적할 수 있습니다.
공유:

댓글

Back to Blog

관련 게시글

View All Posts »
AWS SSL 아마존 인증서 발급

AWS SSL 아마존 인증서 발급

AWS Certificate Manager에서 SSL 인증서를 발급받는 방법을 단계별로 안내합니다. DNS 검증과 Route 53 레코드 생성을 다룹니다.