Overview

Nginx 的 config 檔主要由 directive 組成,而又可以細分為:

配置檔案又可以細分為:

其中主要配置檔案內會 include 所需的子配置檔案,這個子配置檔案通常是不同域名的配置檔,例如 /etc/nginx/conf.d/yucheng.me.conf 是配置 yucheng.me 的域名,預設會建立一個 default.conf 配置 localhost 的域名。

配置檔 block directive 大致架構如下:

.
├── event
└── http
    ├── upstream
    └── server
        └── location

nginx.conf

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

最後一行將其他子配置檔匯入進主配置檔中,因此子配置檔都會被包含在 http 區塊中。

default.conf

upstream application {
		server 192.168.1.1:8080;
		server 192.168.1.1:8081;
}
server {
		listen                      80;
		server_name                 localhost;
		location / {
				proxy_pass              <http://application>;
				proxy_set_header        Host $http_host;
		}
}

SSL