寫在前面的話
Nginx 在安裝完成后自動為我們生成了一個展示歡迎頁的虛擬主機,除此之外,還附帶了很多基礎的配置,我們先來看看這些配置有什么用,順便添加一些常用但是配置文件中並未初始化進去的配置來專門談談。
基礎配置
以下是默認配置文件中的內容,並做了簡單的調整:
# Nginx 默認運行 worker 的用戶為 nobody,而 Master 用戶為 root user nobody; #工作進程,也就是 worker 數量,一般為 CPU 核數,可以寫 auto worker_processes 1; # 默認錯誤日志路徑,級別 error_log logs/error.log info; # 默認 PID 文件保存位置 pid logs/nginx.pid; # 一個進程打開的最大文件數,建議與 ulimit -n 一致 worker_rlimit_nofile 65535; events { # epoll 為一種多路復用 IO 模式,可以提升性能 use epoll; # 單個進程最大連接數 worker_connections 65535; } http { # 文件擴展名與文件類型映射表和默認文件類型 include 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 logs/access.log main; # 高效文件傳輸,普通設置為 on,下載服務或者高 IO 服務設置為 off sendfile on; # 長連接超時時間,單位是秒 keepalive_timeout 65; # gzip 壓縮輸出 # gzip on; # 虛擬主機 server { # 監聽端口 listen 80; # 域名,多個空格隔開 server_name localhost; # 單獨的日志 # access_log logs/host.access.log main; # error_log logs/error.log; # 匹配規則 location / { # 項目目錄和索引文件 root html; index index.html index.htm; } # 404 返回頁面 # error_page 404 /404.html; # 其他錯誤代碼返回頁面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
從上面的配置中我們需要知道:
1. nginx 進程由 master / worker 組成,master 通過 root 用戶運行,worker 通過配置中用戶運行,默認 nobody。
2. 並發總數 = worker_processes * worker_connections,但如果是反向代理需要除以 4。
3. worker_connections 受制於內存和 IO,也就是機器的性能。
4. 通過 cat /proc/sys/fs/file-max 查看服務器可以打開文件句柄數,一般 1G 內存為 10 萬左右。
5. worker_rlimit_nofile 需要大於等於 worker_connections,而且在系統優化時應該調大 ulimit 的值。
6. error log 可以在 http 外層配置,因為啟動 nginx 也會報錯寫日志,但 access 日志只能在 http 的下級配置中。
7. http 端口為 80,https 端口為 443。
實現基本的 WEB 服務
我們這里以單純的 HTML 文件來實現 nginx 作為輕量級 WEB 服務器的基本功能。
1. 創建相關目錄和頁面:
mkdir -p /data/www/demo-80 mkdir -p /data/www/demo-8080 echo '<h1>NGINX SERVER PORT 80</h1>' >/data/www/demo-80/index.html echo '<h1>NGINX SERVER PORT 80 404 NOT FOUND</h1>' >/data/www/demo-80/404.html echo '<h1>NGINX SERVER PORT 8080</h1>' >/data/www/demo-8080/index.html echo '<h1>NGINX SERVER PORT 8080 404 NOT FOUND</h1>' >/data/www/demo-8080/404.html
目錄結構如下:

2. 為了增強配置,我們將虛擬主機抽離到單獨的配置中,並刪除掉原本的 server 段:
user nginx; worker_processes auto; error_log /data/logs/nginx/error.log info; pid /data/logs/nginx/nginx.pid; events { worker_connections 65535; } http { include mime.types; default_type application/octet-stream; sendfile on; 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 /data/logs/nginx/access.log main; include vhosts/*.conf; }
該配置作用在於,在 http 段下面增加 /data/services/nginx/conf/vhosts 目錄下以 .conf 結尾的所有文件!
新建相關目錄:
cd /data/services/nginx/conf
mkdir vhosts
3. 去 vhosts 目錄下添加本次測試的配置:demo.conf
# 80 端口測試 server { listen 80; server_name localhost; location / { root /data/www/demo-80; index index.html index.htm; } error_page 404 /404.html; } # 8080 端口測試 server { listen 8080; server_name localhost; location / { root /data/www/demo-8080; index index.html index.htm; } error_page 404 /404.html; }
4. 重載配置生效:
/data/services/nginx/sbin/nginx -t
/data/services/nginx/sbin/nginx -s reload
我們要養成每次重載配置之前 -t 檢查配置的習慣。
另外 -s reload 可以平滑重啟 nginx,不會造成業務中斷,同理 -s stop 就是停止 nginx。
5. 查看端口,訪問測試:

訪問 80:

訪問 80 端口不存在的頁面:

訪問 8080:

訪問 8080 端口不存在的頁面:

日志配置
nginx 日志配置不同位置的不同含義:
1. 在 nginx 配置文件的最外層,我們可以配置 error_log,這個 error_log 能夠記錄 nginx 啟動過程中的異常,也能記錄日常訪問過程中遇到的錯誤。
2. 在 http 段中可以配置 error_log 和 access_log,可以用於記錄整個訪問過程中成功的,失敗的,錯誤的訪問。
3. 在 server 內部配置屬於專門 server 的 error_log 和 access_log,這是我們常用的,不同業務日志分開。
最后我們需要知道的,越往配置里層,優先級越高,意味着 server 的日志記錄以后並不會因為你在外層寫了日志而再次記錄。
在日志配置中,有如下關鍵字:
| 關鍵字 | 說明 |
|---|---|
| error_log | 錯誤日志,級別:debug/info/notice/warn/error/crit/alert/emerg |
| error_log logs/error.log error; | |
| access_log | 正常訪問日志,可以關閉:access_log off |
| access_log logs/access.log main; | |
| open_log_file_cache | 日志寫入都是經過打開,寫入,關閉文件。該參數用於設置日志文件的緩存,默認 off |
| open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2; | |
| log_not_found | 是否在 error_log 中記錄不存在的錯誤,默認 on |
| log_not_found on | off; | |
| log_subrequest | 是否在 access_log 中記錄子請求的記錄,默認 off |
| log_subrequest on | off; | |
| rewrite_log | 重寫的日志,該日志只有在 rewrite 時候起作用,一般用於調試,默認 off |
| rewrite_log on | off; | |
另外我們需要單獨拿出來談談的是日志格式關鍵字:log_format
我們需要先知道的是,在 nginx 中內置了很多變量,我們都可以直接拿來使用,在日志這里我們常用的變量:
| 變量 | 含義 |
|---|---|
| $remote_addr | 客戶端的 IP 地址 |
| $remote_user | 客戶端用戶名稱 |
| $time_local | 當前時區時間 |
| $time_iso8601 | ISO8601 標准格式下的本地時間 |
| $request | 請求的 URL 與 HTTP 協議 |
| $status | 請求狀態,成功 200 |
| $body_bytes_sent | 發送給客戶端的主體文件大小 |
| $http_referer | 從哪個頁面來的 |
| $http_user_agent | 客戶端瀏覽器信息 |
| $http_x_forwarded_for | 客戶端真實 IP |
| $connection | 連接的序列號 |
| $connection_requests | 當前通過一個連接獲取的請求數量 |
| $msec | 日志寫入時間 |
| $pipe | pipeline 標識 |
| $request_length | 請求長度 |
| $request_time | 請求處理時間 |
我們先看下默認的配置:
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
我們可以進行簡單的改寫,便於查看:
log_format mylog '$remote_addr - $status $remote_user [$time_local] "$request" ' '[Bytes: $body_bytes_sent - Time: $request_time] "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
此時我們刪除配置中原來的日志配置,修改為我們新的:
... http { ... log_format mylog '$remote_addr - $status $remote_user [$time_local] "$request" ' '[Bytes: $body_bytes_sent - Time: $request_time] "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /data/logs/nginx/access.log mylog; ... }
紅色部分需要特別注意,我們改了這個格式的名字,自然使用的時候也要換成這個名字。
重載 nginx 后訪問查看日志 /data/logs/nginx/access.log:

至於為啥狀態碼是 304 而不是 200,原因在於對於這種靜態文件,nginx 在第一次訪問的時候狀態碼 200,再次訪問就是 304 了。
如果你還想 200,可以 ctrl + F5 強制刷新瀏覽器就是 200 了。 而且 304 可以發現其實服務器發送大小是 0 的。
至於針對單個 server(虛擬主機) 增加專門的日志,我們可以修改 demo.conf:
# 80 端口測試 server { listen 80; ... error_log /data/logs/nginx/demo-error.log info; access_log /data/logs/nginx/demo-access.log mylog; } ...
重載配置訪問測試我們可以發現:
1. 原本的 access.log 無論專門刷新也沒有日志寫入了。
2. 在 /data/logs/nginx 目錄下生成了我們剛剛配置的日志:

日志已經寫到了新的文件里面!
小結
本節都是 nginx 的基礎,談了一下簡單的虛擬主機的配置,但是這些都是最簡單的實現。至於新日志的格式,更加有利於我們對日志進行處理和查看。
