do what you like && brave
nginx.conf
檔案結構
主要由三塊組成
全域性塊
events塊
http塊
,http塊
中所有遊戲http全域性塊
狀語從句:多個server塊
,每個server
塊所有遊戲中server全域性塊
狀語從句:多個location塊
,採用就近原則
生效
全域性塊
配置一些影響整體執行的指令,通常有伺服器使用者組
允許生成的worker process
nginx.pid存放路徑
日誌存放路徑
型別
配置檔案引入
事件塊
影響nginx的伺服器與使用者的網路連線,這一塊的設定對服務的效能影響較大。通常有是否開啟對WP下的網路進行序列化
是否允許同時接受多個網路連線
事件驅動模型
每個WP可以同時支援處理的最大連線數
HTTP塊
重要組成部分,代理,快取和日誌定義,第三方模組的配置。通常配置 檔案引入
MIME-TYPE定義
日誌自定義
是否使用sendfile傳輸檔案
連線超時時間
單連線請求數上限
具體配置
配置使用者組
# 配置alleyz使用者 admin組下有啟停許可權
user alleyz [admin];
#所有使用者具有啟停許可權
user nobody nobody;
配置執行生成的worker process
# 如果設定為數字,啟動以後就具有多少個worker process # 設定為auto則自動檢測 worker_processes number | auto
配置pid
檔案存放路徑
預設存放在日誌/ nginx.pid
#必須包含檔名稱
pid logs/nginx_alleyz.pid;
配置錯誤日誌檔案的存放路徑
可在全域性塊、http塊 server塊 location塊配置
error_log file | stderr [ DEBUG | info | notice | warn | error | crit | alert | emerg];
配置檔案引入 任意地方
include file;
設定網路連線的序列化
驚群問題
當某刻只有一個請求進來時,會喚醒多個睡眠的程序,造成效能的損耗accept_mutex on |
關閉;
設定是否允許同事接受多個連線 event
multi_accept on | 關閉; #預設關閉
事件驅動模型的選擇 events
可選擇的內容有:
select poll kqueue epoll rtsig /dev/poll eventport
use epoll;
配置最大連線數 events
允許每一個WP同事開啟的最大連線數。此值不能大於作業系統支援的開啟的最大檔案控制代碼數。
worker_connections number; #預設值 512
MIME型別 http server location
default_type mime-type; #預設為 text/plain
自定義服務日誌
access_log 可在http server location中配置; log_format只能在http中配置
。注意
前文提到的error_log 配置的是nginx的執行日誌,此處指的是應答前端請求的服務日誌。可以對日誌的格式、大小、輸出進行配置,有access_log log_format
指令, log_format 的string整體需要用'
括起來,變數名稱使用雙引號括起來
log_format name string; access_log path [format [buffer=size]]; 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; #如果取消日誌記錄 access_log off;
配置
sendfile
方式傳輸檔案
相關指令
sendfile
sendfile_max_chunk
,預設開啟
sendfile on | off;
sendfile_max_chunk size; #設定為0為限制
配置連線超時時間
http塊 server塊 location塊
,設定使用者會話連線的保持時間
keepalive_timeout timeout[header_timeout];
單連線請求上限
server location
使用者與服務端建立連線後,通過此連線傳送的請求次數
keepalive_requests number;
配置網路監聽
- 配置監聽IP地址
listen address[:port] [default_server] [setfib=number] [backlog=number] [rcvbuf=size] [sendbuf=size] [deferred] [accept_filter=filter] [bind] [ssl];
- 配置監聽埠
listen port [default_server] [setfib=number] [backlog=number] [rcvbuf=size] [sendbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on | off] [ssl];
- 配置Unix Domain Socket
很少用
listen unix:path [default_server] [setfib=number] [backlog=number] [rcvbuf=size] [sendbuf=size] [accept_filter=filter] [deferred] [bind] [ssl];
平常使用:
listen 3021; listen 10.8.177.21; listen 10.8.177.21:3022 default_server backlog==1024;
引數說明:
- address ip地址,如果是ipv6需要使用[fe13:..]
格式
- port 埠號,如果只有ip則預設80
- path socket
檔案路徑
- default_server, 識別符號,將ip:port設定為預設的虛擬主機
- backlog 設定監聽函式listen()
最多允許多少網路連線同時處於掛起的狀態,預設511
(FreeBSD 為-1)
- rcvbuf 設定監聽socket接受快取區大小
- sendbuf 設定監聽socket傳送快取區大小
- deferred 識別符號,將accept()
設定為Deferred模式
- bind 識別符號,使用獨立的bind()
處理此adress:port,一般情況下埠相同而IP地址不同,只使用一個
- ssl 識別符號,設定會話使用ssl模式(https)
- accept_filter 設定監聽埠的請求過濾,被過濾的內容不能被接受和處理(只在FreeBSD NetBSD中有用)
- setfib 為監聽socket關聯路由表,只對FreeBSD
起作用
基於名稱的虛機主機設定
- 可以配置多個name
server_name name name1 name2 ....;
- 1
- 可以使用萬用字元
*
只能位於三段字串組成的首尾或者兩段字串的尾部
server_name *.alleyz.com www.alleyz.*
- 1
- 使用正則表示式,
~
作為正則開始的標記,並且正則支援捕獲
server_name ~([a-zA-Z\d]{1,4})\.alleyz.com$;
- 1
此時,通過如果
xisuo.alleyz.com
訪問的話教育,使用柯林斯$1
捕獲xisuo
;一個名稱若被多次匹配的訪問優先順序
: - 匹配方式不同時 1。精確匹配 2。萬用字元在開始 3。萬用字元在結尾 4。正則表示式匹配 - 相同匹配方式時,首次處理優先(順序)
基於IP的虛擬主機設定
需設定網絡卡能監聽多個IP地址
# 臨時生效
ifconfig em1:0 10.8.177.21 netmask 255.255.0.0 up
ifconfig em1:1 10.8.177.22 netmask 255.255.0.0 up
# 永久生效
echo "ifconfig em1:0 10.8.177.21 netmask 255.255.0.0 up" >> /etc/rs.local
# vi nginx.conf
...
server {
listen 3022;
server_name 10.8.177.32;
...
}
server {
listen 3022;
server_name 10.8.177.21;
...
}
kill -HUP `cat ../logs/nginx_alleyz.pid`
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
配置定位塊
location [ = | ~ | ~* | ^~] uri {...}
- 1
=
普通用於uri
之前,表示嚴格匹配~
uri
包含正則表示式,並且區分大小寫~*
表示包含正則表示式,並且不區分大小寫^~
如果找到與uri匹配度最高的位置,立即處理請求。會對uri
進行反編碼
配置請求的根目錄 http server location
伺服器收到請求後查詢資源的根目錄路徑,可以使用nginx的預設的大多變數,唯
$document_root $realpath_root
不能使用;在通常location
塊中使用。
root path;
- 1
更改位置的URI
使用除了
root
指定根目錄,可以還使用alias
指令改變位置接收到的請求路徑
alias path;
location ~ ^/data/(.+\.(htm|html))${
alias html/data/other/$1;
}
- 1
- 2
- 3
- 4
- 五
設定網站的預設首頁
可以針對不同的訪問設定不同的首頁
index index.html index.htm;
- 1
設定網站的錯誤頁面 http server location
如果URI是路徑的話,則路徑是以nginx的的安裝目錄為根路徑的;
error_page code ... [=[response]] uri;
error_page 404 /404.html;
error_page 404 http://someone.com/404.html;
- 1
- 2
- 3
如果希望錯誤路徑指向自定的位置:
error_page 404 /404.html
location /404.html{
root /home/alleyz/html;
}
- 1
- 2
- 3
- 4
基於IP配置訪問許可權 http server location
allow address | CIDR | all;
deny address | CIDR | all;
location / {
root html;
index index.html;
deny 10.8.177.26;
}
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
基於密碼配置訪問許可權
基於
HTTP Basic Authentication
協議認證,檔案中密碼得加密!
# 生成密文,-c建立密碼檔案 -d採用crypt加密 -b 命令列指定密碼
htpasswd -cdb passwd alleyz 123456
# string 開啟認證功能,並設定提示資訊
auth_basic string | off;
#file 包含使用者名稱資訊的檔案路徑
auth_basic_user_file file;
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
- 9
我自己的測試配置
user alleyz;
worker_processes 2;
pid logs/nginx_alleyz.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main1 '$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 main1;
sendfile on;
keepalive_timeout 65;
server {
listen 3022;
server_name 10.8.177.32;
deny 10.8.177.26;
location / {
auth_basic "it`s auth test msg!";
auth_basic_user_file passwd;
root html/22;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 3022;
server_name 10.8.177.21;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}