Configuration

設定檔 nginx.conf

會依據安裝方式導致被放置的路徑不同,可以透過 nginx -t 來查詢

nginx -t
# nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
# 表示設定檔路徑為 /usr/local/etc/nginx/nginx.conf

config 檔是由一連串的 directive 所組成的。directive 針對特定的部分作設定,分為兩種:simple directiveblock directive

simple directive 要以分號 ; 結尾,而 block directive 會有一組大括號 {},包著其他的 directive(simple 或是 block)。

simple directive 會對把它包起來的 block directive 作設定,舉最簡單的例子:


# listen 是個 simple directive
# 對 server 這個 block directive 作設定
# 指定它監聽 port 80
server {
listen 80;
}

最底層的 block directive 只會有兩種:httpevent,稱之為 main context

http {
    # server 一定在 http 裡面
    server {
        # location 一定在 server 裡面
        location {
        }
    }
}
event {}

設定 proxy server

情境: app 使用的 port 為 2368,要讓當連到 www.example.com 時,交由這個 app 來處理。 nginx.conf 把 www.example.com 指到 2368 這個 port

設定 load balancer

使用 upstream 這個 directive。

假設 2 個 ip:192.168.1.1192.168.1.2 的 2368 port 都有相同的 app。

這樣子 nginx 會使用預設的 round-robin 原則來作 load balance,另外還有兩種 least-connectedip-hash 原則可用,詳情請見官方文件arrow-up-right的說明。

Last updated