Nginx
Nginx(엔진 x라 읽는다)는 웹 서버 소프트웨어로, 가벼움과 높은 성능을 목표로 한다. 웹 서버, 리버스 프록시 및 메일 프록시 기능을 가진다.
Netcraft의 2011년 1월 웹서버 설문조사에 따르면, nginx는 전체 도메인에서 4번째(7.50%)로 많이 쓰이는 웹서버이며, 활성화된 웹 사이트에 대한 통계에서도 역시 4번째(8.23%)로 많이 사용된다.
Nginx는 요청에 응답하기 위해 비동기 이벤트 기반 구조를 가진다. 이것은 아파치 HTTP 서버의 스레드/프로세스 기반 구조를 가지는 것과는 대조적이다. 이러한 구조는 서버에 많은 부하가 생길 경우의 성능을 예측하기 쉽게 해준다.
Categories
ETC
How to install
Ubuntu 14.04
PGP 서명을 추가한다.
/etc/apt/sources.list
파일에 아래 저장소를 추가한다.
deb http://nginx.org/packages/ubuntu/ trusty nginx
deb-src http://nginx.org/packages/ubuntu/ trusty nginx
그리고 아래와 같이 설치한다.
Files
- Document Root
-
/usr/share/nginx/html/
- 설정 파일
-
/etc/nginx/
- 로그파일
-
/var/log/nginx/
Configuration
- Understanding the Nginx Configuration File Structure and Configuration Contexts
- NGINX - Alphabetical index of directives
Keepalive
요약하면,
client_max_body_size
Proxy
- Stackoverflow: NGinx config for redirecting domain
- Stackoverflow: route different proxy based on subdomain request in nginx
- Joinc: 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 또는 포트번호가 달라진다면 아래와 같이 설정한다.
HTTPS
- How To Configure Nginx with SSL as a Reverse Proxy for Jenkins
- How To Create a SSL Certificate on Apache for Ubuntu 14.04
- OpenSSL 참조.
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
- How To Set Up HTTP Authentication With Nginx On Ubuntu 12.10
- How to use nginx to proxy to a host requiring authentication?
- Nginx - Forward HTTP AUTH - User?
Create User and Password:
Update Nginx configuration:
(Optional) Proxy forward:
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
- 고성능 Nginx를위한 튜닝 - (1) 디스크의 I/O 병목 줄이기
- 고성능 Nginx를위한 튜닝 - (2) 프로세스 처리량 늘리기
- 고성능 Nginx를위한 튜닝 - (3) TCP 관련 처리량 늘리기
- [고성능 Nginx를위한 튜닝 - (4) 메모리 및 CPU 튜닝하기 (Processor)]
무중단 배포
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
- NGINX web site
- Wikipedia (en) NGINX에 대한 설명
- Github - NginX - read-only mirror
- [추천] NGINX 소개
- 우분투에서 nginx 설치부터 무료 SSL 적용까지
- HTTP/2 성능 향상을 위한 NGINX 구조 개선 3
Documentation
- Module ngx_http_core_module Embedded Variables (내장변수) 포함.
Online tools
- nginx playground - Nginx 설정을 웹에서 테스트하고 공유할 수 있다.