在上一篇的文章中,實做了多種 HTTP 負載均衡的分配方法,
在這幾篇的文章中只有修改 /etc/nginx/http.d/default.conf
這個檔案,那為什麼 Nginx 知道要到這個目錄底下讀取 default.conf
呢?
要解答這個問題就要先了解 nginx.conf
這個設定檔還有 include
這個指令了。
nginx.conf
我們可以先看一下 Alpine 打包的 nginx.conf
原始設定碼 Github。
# /etc/nginx/nginx.conf
user nginx;
# Set number of worker processes automatically based on number of CPU cores.
worker_processes auto;
# Enables the use of JIT for regular expressions to speed-up their processing.
pcre_jit on;
# Configures default error logger.
error_log /var/log/nginx/error.log warn;
# Includes files with directives to load dynamic modules.
include /etc/nginx/modules/*.conf;
# Include files with config snippets into the root context.
include /etc/nginx/conf.d/*.conf;
events {
# The maximum number of simultaneous connections that can be opened by
# a worker process.
worker_connections 1024;
}
http {
# Includes mapping of file name extensions to MIME types of responses
# and defines the default type.
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Name servers used to resolve names of upstream servers into addresses.
# It's also needed when using tcpsocket and udpsocket in Lua modules.
#resolver 1.1.1.1 1.0.0.1 [2606:4700:4700::1111] [2606:4700:4700::1001];
# Don't tell nginx version to the clients. Default is 'on'.
server_tokens off;
# Specifies the maximum accepted body size of a client request, as
# indicated by the request header Content-Length. If the stated content
# length is greater than this size, then the client receives the HTTP
# error code 413. Set to 0 to disable. Default is '1m'.
client_max_body_size 1m;
# Sendfile copies data between one FD and other from within the kernel,
# which is more efficient than read() + write(). Default is off.
sendfile on;
# Causes nginx to attempt to send its HTTP response head in one packet,
# instead of using partial frames. Default is 'off'.
tcp_nopush on;
# Enables the specified protocols. Default is TLSv1 TLSv1.1 TLSv1.2.
# TIP: If you're not obligated to support ancient clients, remove TLSv1.1.
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
# Path of the file with Diffie-Hellman parameters for EDH ciphers.
# TIP: Generate with: `openssl dhparam -out /etc/ssl/nginx/dh2048.pem 2048`
#ssl_dhparam /etc/ssl/nginx/dh2048.pem;
# Specifies that our cipher suits should be preferred over client ciphers.
# Default is 'off'.
ssl_prefer_server_ciphers on;
# Enables a shared SSL cache with size that can hold around 8000 sessions.
# Default is 'none'.
ssl_session_cache shared:SSL:2m;
# Specifies a time during which a client may reuse the session parameters.
# Default is '5m'.
ssl_session_timeout 1h;
# Disable TLS session tickets (they are insecure). Default is 'on'.
ssl_session_tickets off;
# Enable gzipping of responses.
#gzip on;
# Set the Vary HTTP header as defined in the RFC 2616. Default is 'off'.
gzip_vary on;
# Helper variable for proxying websockets.
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# Specifies the main log format.
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# Sets the path, format, and configuration for a buffered log write.
access_log /var/log/nginx/access.log main;
# Includes virtual hosts configs.
include /etc/nginx/http.d/*.conf;
}
這個 nginx.conf
是 nginx 預設的設定檔名稱,可以看到裡面有各式各樣的設定值但基本上可以分成兩種格式 簡單指令 與 區塊指令
1. 簡單指令
由一個名稱與參數組成需要使用分號作結尾 ex:
worker_processes auto;
2. 區塊指令
基本上與簡單指令相同但是需要改用一組大括號作為結尾 ex:
http {}
Context
使用區塊指令需要注意 Context
這個概念如果將簡單指令寫在區塊指令的大括號內部這樣就會改變簡單指令的作用環境,
另外有一些簡單指令只能放在特定的 Context
內,所以在撰寫設定檔時需要多注意官方文檔的設定語法與作用環境。
以下是官方文檔中 http 指令的範例, 從內容中可以看出語法是開頭寫 http 之後搭配一組大括號,並且作用環境是在 main 底下。
Syntax: http { ... }
Default: —
Context: main
當指令沒有放在任何 Context
底下就會被歸類到 main
底下,所以從上面的設定檔可以知道 http
events
與最上方的 include
這些指令都是作用在
main
環境底下。
所以根據文檔對 http 指令的限制,如果在 http 指令底下在寫一組 http 指令的寫法就是不允許的。
http {
http {}
}
Include
了解兩種指令與作用環境的概念後就可以來看 include
這個命令。
Syntax: include file | mask;
Default: —
Context: any
從文檔的作用環境限制中可以得知這個指令可以在任何作用環境中使用,所以從上方的 nginx.conf
的範例中就可以看到在 main
環境與 http
內部都有使用 include
,這個指令的用途是簡化 nginx.conf
的複雜度,我們可以將一些過長或需要重複使用的內容搬到獨立的設定檔內部,
就可以不用把所有的內容都寫在 nginx.conf
底下了。
像是 MIME type 就是一個很好的例子,我們可以將 MINE 類型都寫到另一個檔案以避免我們主要的 nginx.conf
變得太冗長,
這個也是上方這段指令所做的事情 include /etc/nginx/mime.types;
。
Summary
今天學習了 Nginx 的兩種指令與作用環境還有 Include
這個指令的相關概念,了解這些內容後就可以回答為甚麼我們前幾天只有修改
http.d/default.conf
這個設定檔而已,並且從 include /etc/nginx/http.d/*.conf
這段指令可以得知我們修改的設定檔都是作用在
http
這個作用環境底下。