Skip to content

Nginx

Nginx(엔진 x라 읽는다)는 웹 서버 소프트웨어로, 가벼움과 높은 성능을 목표로 한다. 웹 서버, 리버스 프록시 및 메일 프록시 기능을 가진다.

Netcraft의 2011년 1월 웹서버 설문조사에 따르면, nginx는 전체 도메인에서 4번째(7.50%)로 많이 쓰이는 웹서버이며, 활성화된 웹 사이트에 대한 통계에서도 역시 4번째(8.23%)로 많이 사용된다.

Nginx는 요청에 응답하기 위해 비동기 이벤트 기반 구조를 가진다. 이것은 아파치 HTTP 서버의 스레드/프로세스 기반 구조를 가지는 것과는 대조적이다. 이러한 구조는 서버에 많은 부하가 생길 경우의 성능을 예측하기 쉽게 해준다.

Categories

ETC

How to install

Ubuntu 14.04

PGP 서명을 추가한다.

$ curl -O http://nginx.org/keys/nginx_signing.key
$ sudo apt-key add nginx_signing.key

/etc/apt/sources.list파일에 아래 저장소를 추가한다.

deb http://nginx.org/packages/ubuntu/ trusty nginx
deb-src http://nginx.org/packages/ubuntu/ trusty nginx

그리고 아래와 같이 설치한다.

$ sudo apt-get update
$ sudo apt-get install nginx

Files

Document Root
/usr/share/nginx/html/
설정 파일
/etc/nginx/
로그파일
/var/log/nginx/

Configuration

Keepalive

요약하면,

nginx를 앞 단에 놓고 proxy_pass와 upstream으로 서비스를 구성한다면 keepalive 설정을 꼭 잊지 않고 하시기 바랍니다.

client_max_body_size

http {
    # ...
    client_max_body_size 20M;
    # ...
}

Proxy

http {
    server {
        listen        80; 
        server_name       service1.domain.com;
        location / { 
            proxy_pass       http://192.168.0.2:8181;
            proxy_set_header   host  service1.domain.com
        }   
    }
    server {
        listen        80; 
        server_name       service2.domain.com;
        location / { 
            proxy_pass       http://192.168.0.3:8080;
            proxy_set_header     host service2.domain.com;
        }   
    }
}

전달되는 서버에서 Redirect할 경우 URL 또는 포트번호가 달라진다면 아래와 같이 설정한다.

http {
    server {
        listen  1443;
        # ...
        proxy_redirect  http://$host  https://$host:1443;
    }
}

HTTPS

server {
    listen 80;
    return 301 https://$host$request_uri;
}

server {

    listen 443;
    server_name jenkins.domain.com;

    ssl_certificate           /etc/nginx/cert.crt;
    ssl_certificate_key       /etc/nginx/cert.key;

    ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;

    access_log            /var/log/nginx/jenkins.access.log;

    location / {

      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;

      # Fix the “It appears that your reverse proxy set up is broken" error.
      proxy_pass          http://localhost:8080;
      proxy_read_timeout  90;

      proxy_redirect      http://localhost:8080 https://jenkins.domain.com;
    }
  }

HTTP Authentication

Create User and Password:

$ sudo apt-get install apache2-utils
$ sudo htpasswd -c /etc/nginx/.htpasswd exampleuser

Update Nginx configuration:

auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;

(Optional) Proxy forward:

proxy_set_header Authorization $http_authorization;
proxy_pass_header  Authorization;

Example

server {
  listen       portnumber;
  server_name  ip_address;
  location / {
      root   /var/www/mywebsite.com;
      index  index.html index.htm;
      auth_basic "Restricted";                                #For Basic Auth
      auth_basic_user_file /etc/nginx/.htpasswd;  #For Basic Auth
  }
}

NginX Cache

Reverse proxy항목을 우선적으로 참조.

NginX WebSocket Proxy

Load Balancing

아래 항목을 참조.

Performance Tunning

무중단 배포

user  nginx;
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-express {
        server express1:3000;
        server express2:3000;
    }

    server {
        listen 80;
        server_name localhost;

        location / {
        proxy_http_version 1.1;
                proxy_pass         http://docker-express;
                proxy_redirect     off;
            proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;         
        }

    }

    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;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

Live Streaming

See also

  • Upgrade - 실행중에 실행파일 업그레이드하기 (Nginx 가 사용하는 업그레이드 방식)

Favorite site

Documentation

Online tools

  • nginx playground - Nginx 설정을 웹에서 테스트하고 공유할 수 있다.

References


  1. Brunch.co.kr_-nginx_upstream_optimization-_keepalive.pdf 

  2. NGINX-Conf-2018-slides_Choi-streaming.pdf 

  3. Nginx-structural-enhancements-for-http-2-performance-ko.pdf