Nginx的主配置文件由指令與指令塊構成,指令塊以{ }大括號將多條指令組織在一起
· 每條指令以;分號結尾,指令與參數間用空格分隔
· 支持include語句組合多個配置文件,提升可維護性
· #表示注釋,$表示變量,部分指令的參數支持正則表達式
3、編譯安裝Nginx,常用選項:
--prefix=path:設置Nginx的安裝路徑,不寫的話默認是在/usr/local/nginx
--sbin-path=path:設置Nginx的可執行文件路徑,默認路徑是prefix/sbin/nginx
--conf-path=path:設置Nginx配置文件路徑,默認路徑是prefix/conf/nginx.conf
--pid-path=path:設置Nginx pid文件路徑,默認路徑是prefix/logs/nginx.pid
--error-log-path=path:設置錯誤日志存放路徑,默認路徑是prefix/logs/error.log
--http-log-path=path:設置訪問日志存放路徑,默認路徑是prefix/logs/access.log
--user=name:設置運行Nginx的用戶,默認用戶是nobody
--group=name:設置運行Nginx的用戶組,默認用戶組是nobody
--with-http_ssl_module enable ngx_http_ssl_module
--with-http_v2_module enable ngx_http_v2_module
--with-http_realip_module enable ngx_http_realip_module
--with-http_addition_module enable ngx_http_addition_module
--with-http_xslt_module enable ngx_http_xslt_module
--with-http_xslt_module=dynamic enable dynamic ngx_http_xslt_module
--with-http_image_filter_module enable ngx_http_image_filter_module
--with-http_image_filter_module=dynamic
enable dynamic ngx_http_image_filter_module
--with-http_geoip_module enable ngx_http_geoip_module
--with-http_geoip_module=dynamic enable dynamic ngx_http_geoip_module
--with-http_sub_module enable ngx_http_sub_module
--with-http_dav_module enable ngx_http_dav_module
--with-http_flv_module enable ngx_http_flv_module
--with-http_mp4_module enable ngx_http_mp4_module
--with-http_gunzip_module enable ngx_http_gunzip_module
--with-http_gzip_static_module enable ngx_http_gzip_static_module
--with-http_auth_request_module enable ngx_http_auth_request_module
--with-http_random_index_module enable ngx_http_random_index_module
--with-http_secure_link_module enable ngx_http_secure_link_module
--with-http_degradation_module enable ngx_http_degradation_module
--with-http_slice_module enable ngx_http_slice_module
--with-http_stub_status_module enable ngx_http_stub_status_module
5、Nginx主配置文件(/usr/local/nginx/conf/nginx.conf)詳解:
events
│
http
├── server1
│ ├── location1 標簽是做匹配
│ ├── location2
│
├── server2
每個主機都配置在server標簽里
Location標簽的匹配規則:
= #精確匹配,優先級最高 ^~ #普通字符串匹配,禁止正則表達式,當匹配成功后停止其他location匹配,優先級高於正則 ~ #區分大小寫的正則匹配 ~* #不區分大小寫的正則匹配
未做任何配置的配置文件:
#user nobody; #默認運行Nginx的用戶名
worker_processes 1; #開啟的進程數,通常和cpu個數相等 events { worker_connections 1024; #每個進程的並發數 } http { include mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #長連接超時時間為65秒 server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
user nginx; #運行Nginx的用戶 worker_processes 2; #開啟的進程數,通常和cpu個數相等或者設置為auto worker_cpu_affinity auto; #自動進行CPU親和設置 #worker_cpu_affinity 0000000000000001 000000000000010 #手動進行CPU親和設置 error_log logs/error.log warn; #Nginx服務的錯誤日志路徑與記錄級別 pid /var/run/nginx.pid; worker_rlimit_nofile 65535; #設置Nginx進程文件句柄數 events { worker_connections 10240; #每個進程的並發數 } http { include mime.types; default_type application/octet-stream; charset utf-8; 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; #訪問日志存放路徑與日志記錄格式,這里main就是上一步log_format所定義的main sendfile on; tcp_nopush on; #一次傳輸多個數據包,提高傳輸效率 #tcp_nodeley off #與tcp_nopush相反,實時性要求比較高的場景會打開這個 keepalive_timeout 65; #長連接超時時間為65秒 gzip on; #打開gzip后通過瀏覽器開發者工具-網絡功能可以看到size大小被壓縮了,對文本類型的文件壓縮效率最高,可作用於location中 include /etc/nginx/conf.d/*.conf #conf.d目錄下的配置文件也會生效 server { listen 80; server_name localhost; #charset koi8-r; access_log logs/access.log main; #單獨對主機記錄日志 location ~ .*\.(jpg|gif|png)$ { gzip on; expires 24h; #開啟緩存,如果是取的緩存數據,瀏覽器開發者工具中返回狀態是304 root html; index index.html index.htm; } #error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
1.rewrite的介紹
nginx的rewrite規則就是使用正則匹配請求的url,然后根據定義的規則進行重寫和改變,需ngx_http_rewrite_module模塊來支持url重寫功能,該模塊是標准模塊,默認已經安裝。
url和uri的區別:
URI:Universal Resource Identifier ,通用資源標識符,用於對網絡中的各種資源進行標識,由存放資源的主機名、片段標志符和相對的URI三部分組成。存放資源的主機名一般由傳輸協議(Scheme)、主機和資源路徑三部分組成;片段標識符指向資源內容的具體元素、相對URI表示資源在主機上的相對路徑。一般格式為:Scheme://[用戶名][:密碼]@主機名[:端口號][/資源路徑]
URL:Uniform Resource Location,統一資源定位符,是用於在Internet中描述資源的字符串,是URI的子集,主要包括傳輸協議(Scheme)、主機(IP、端口號或者域名)和資源集體地址(目錄或文件名)等三部分,一般格式為:scheme://主機名[:端口號]/[資源路徑]
2.rewrite涉及的指令
執行順序:
1.執行server塊的rewrite指令(這里的塊指的是server關鍵字后{}包圍的區域,其它xx塊類似)
2.執行location匹配
3.執行選定的location中的rewrite指令
如果其中某步URI被重寫,則重新循環執行1-3,直到找到真實存在的文件
如果循環超過10次,則返回500 Internal Server Error錯誤
1)if指令
語法:if(condition){...}
默認值:無
作用域:server,location
對給定的條件condition進行判斷。如果為真,大括號內的rewrite指令將被執行。
if條件(conditon)可以是如下任何內容:
一個變量名;false如果這個變量是空字符串或者以0開始的字符串;
使用= ,!= 比較的一個變量和字符串,true/false
使用~, ~*與正則表達式匹配的變量,如果這個正則表達式中包含右花括號}或者分號;則必須給整個正則表達式加引號
使用-f ,!-f 檢查一個文件是否存在
使用-d, !-d 檢查一個目錄是否存在
使用-e ,!-e 檢查一個文件、目錄、符號鏈接是否存在
使用-x , !-x 檢查一個文件是否可執行
if指令實例
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $1;
}
if ($request_method = POST) {
return 405;
}
if ($slow) {
limit_rate 10k;
}
2)return指令
用於完成對請求的處理,直接給客戶端返回狀態碼,改指令后所有的nginx配置都是無效的,
語法:return code;
return code URL;
return URL;
默認值:無
作用域:server,location,if
3)set指令
語法:set variable value;
默認值:none
作用域:server,location,if
定義一個變量並賦值,值可以是文本,變量或者文本變量混合體。
4)uninitialized_variable_warn指令
語法:uninitialized_variable_warn on | off; 默認值:uninitialized_variable_warn on 作用域:http,server,location,if 控制是否輸出為初始化的變量到日志
5)rewrite指令
該指令通過正則來改變url,可以同時存在一個或者多個指令 語法:rewrite regex replacement [flag]; 默認值:無 作用域:server,location,if regex :用於匹配uri的正則表達式。使用括號()標記要截取的內容 replacement 匹配成功后用於替換uri中被截取內容的字符串,默認情況下,如果該字符串是由http://或者https://開頭的,則不會繼續向下對uri進行其他處理,而是直接將重寫后的uri返回給客戶端 flag 用來設置rewrite對uri的處理行為,常用的有 last 停止處理后續rewrite指令集,然后對當前重寫的新URI在rewrite指令集上重新查找。 break 停止處理后續rewrite指令集,並不在重新查找,但是當前location內剩余非rewrite語句和location外的的非rewrite語句可以執行。 redirect 如果replacement不是以http:// 或https://開始,返回302臨時重定向 permant 返回301永久重定向 補充:last和break標記的區別在於,last標記在本條rewrite規則執行完后,會對其所在的server { … } 標簽重新發起請求,而break標記則在本條規則匹配完成后,停止匹配,不再做后續的匹配。另外有些時候必須使用last,比如在使用alias指令時,而 使用proxy_pass指令時則必須使用break。 注意:rewrite 規則優先級要高於location,在nginx配置文件中,nginx會先用rewrite來處理url,最后再用處理后的url匹配location
6)常用的變量
$args : #這個變量等於請求行中的參數,同$query_string
$content_length : 請求頭中的Content-length字段。
$content_type : 請求頭中的Content-Type字段。
$document_root : 當前請求在root指令中指定的值。
$host : 請求主機頭字段,否則為服務器名稱。
$http_user_agent : 客戶端agent信息
$http_cookie : 客戶端cookie信息
$limit_rate : 這個變量可以限制連接速率。
$request_method : 客戶端請求的動作,通常為GET或POST。
$remote_addr : 客戶端的IP地址。
$remote_port : 客戶端的端口。
$remote_user : 已經經過Auth Basic Module驗證的用戶名。
$request_filename : 當前請求的文件路徑,由root或alias指令與URI請求生成。
$scheme : HTTP方法(如http,https)。
$server_protocol : 請求使用的協議,通常是HTTP/1.0或HTTP/1.1。
$server_addr : 服務器地址,在完成一次系統調用后可以確定這個值。
$server_name : 服務器名稱。
$server_port : 請求到達服務器的端口號。
$request_uri : 包含請求參數的原始URI,不包含主機名,如:”/foo/bar.php?arg=baz”。
$uri : 不帶請求參數的當前URI,$uri不包含主機名,如”/foo/bar.html”。
$document_uri : 與$uri相同。
7)常用正則:
. : 匹配除換行符以外的任意字符
? : 重復0次或1次
+ : 重復1次或更多次
* : 重復0次或更多次
\d :匹配數字
^ : 匹配字符串的開始
$ : 匹配字符串的介紹
{n} : 重復n次
{n,} : 重復n次或更多次
[c] : 匹配單個字符c
[a-z] : 匹配a-z小寫字母的任意一個
小括號()之間匹配的內容,可以在后面通過$1來引用,$2表示的是前面第二個()里的內容。正則里面容易讓人困惑的是\轉義特殊字符。
配置案例
1. 在URL結尾添加斜杠
在虛擬主機中這么添加一條改寫規則:
rewrite ^(.*[^/])$ $1/ permanent;
2. 刪除URL結尾的斜杠
在虛擬主機中這么添加一條改寫規則:
rewrite ^/(.*)/$ /$1 permanent;
-ldl -lpthread -lcrypt -lpcre -lssl -lcrypto -ldl -lpthread -lz \
-Wl,-E sed -e "s|%%PREFIX%%|/usr/local/nginx|" \ -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \ -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \ -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \ < man/nginx.8 > objs/nginx.8 make[1]: 離開目錄“/usr/local/nginx-1.14.2”
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \ || mkdir -p '/usr/local/nginx/sbin' test ! -f '/usr/local/nginx/sbin/nginx' \ || mv '/usr/local/nginx/sbin/nginx' \ '/usr/local/nginx/sbin/nginx.old' cp objs/nginx '/usr/local/nginx/sbin/nginx' test -d '/usr/local/nginx/conf' \ || mkdir -p '/usr/local/nginx/conf' cp conf/koi-win '/usr/local/nginx/conf' cp conf/koi-utf '/usr/local/nginx/conf' cp conf/win-utf '/usr/local/nginx/conf' test -f '/usr/local/nginx/conf/mime.types' \ || cp conf/mime.types '/usr/local/nginx/conf' cp conf/mime.types '/usr/local/nginx/conf/mime.types.default' test -f '/usr/local/nginx/conf/fastcgi_params' \ || cp conf/fastcgi_params '/usr/local/nginx/conf' cp conf/fastcgi_params \ '/usr/local/nginx/conf/fastcgi_params.default' test -f '/usr/local/nginx/conf/fastcgi.conf' \ || cp conf/fastcgi.conf '/usr/local/nginx/conf' cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default' test -f '/usr/local/nginx/conf/uwsgi_params' \ || cp conf/uwsgi_params '/usr/local/nginx/conf' cp conf/uwsgi_params \ '/usr/local/nginx/conf/uwsgi_params.default' test -f '/usr/local/nginx/conf/scgi_params' \ || cp conf/scgi_params '/usr/local/nginx/conf' cp conf/scgi_params \ '/usr/local/nginx/conf/scgi_params.default' test -f '/usr/local/nginx/conf/nginx.conf' \ || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf' cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/html' \ || cp -R html '/usr/local/nginx' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs'