listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size]
rcvbuf:接收緩沖區大?。?/div>
sndbuf:發送緩沖區大?。?/div>
spdy:SPDY protocol(speedy),在編譯了spdy模塊的情況下,用于支持SPDY協議;
http2:http version 2;
3、server_name NAME […];
指明當前server的主機名;后可跟一個或多個用空白字符分隔的主機;
支持使用*任意長度的任意字符;
支持~起始的正則表達式模式字符串;
優先級:
(1) 首先做精確匹配;例如:www.magedu.com
(2) 左側通配符;例如:*.magedu.com
(3) 右側通配符,例如:www.magedu.*
(4) 正則表達式,例如:~^.*\.magedu\.com$
(5) default_server
4、root path;
設置web資源的路徑映射;用于指明請求的URL所對應的文檔的目錄路徑;
可用上下文:http,server,location,if
5、location [ = | ^~ | ~ | ~* ?] uri { … };
? ?location @name { … }
功能:允許根據用戶請求的URI來匹配定義的各location;匹配到時,此請求將被相應的location塊中的配置所處理,例如做訪問控制等功能;
=:URI的精確匹配;
^~:URI的前半部分匹配,不支持正則表達式;
~:做正則表達式匹配,區分字符大小寫;
~*:做正則表達式匹配,不區分字符大小寫;
匹配優先級:精確匹配=、^~、~或~*、不帶符號的URL;
6、alias path;
只能用于location配置段,定義路徑別名;
location ?/images/ {
root /data/imgs/;
}
http://www.magedu.com/images/a.jpg ? ? ?<— ? ?/data/imgs/images/a.jpg
location ?/images/ ?{
alias /data/imgs/;
}
http://www.magedu.com/images/a.jpg ? ? ?<— ? ?/data/imgs/a.jpg
注意:root表示指明路徑為對應的location “/” URL;alias表示路徑映射,即location指令后定義的URL是相對于alias所指明的路徑而言;
7、index ?file;
默認主頁面;
例如: ? index ? ?index.php ? ?index.html;
8、error_page ? ?code ? […] ? ?[=code] ? URI ? | ? @name;
根據http的響應狀態碼來指明特用的錯誤頁面;
error_page ? 404 ? /404_customed.html
?[=code]:以指定的響應碼進行響應,而不是默認的原來的響應碼;默認表示以新資源的響應碼為其響應碼;
error_page ?404 ?=200 ?/404.html
9、基于IP的訪問控制
allow ? ?IP | ?NETWORK | unix: | all;
deny ? ?IP ?| NETWORK | unix: | all;
可用上下文:http, server, location, limit_except
10、基于用戶的訪問控制(basic,digest)
auth_basic ? “”;
auth_basic_user_file ? “/PATH/TO/PASSWD_FILE”;
賬號密碼建議使用htpasswd來創建
htpasswd命令;
# htpasswd -c -m ?/etc/nginx/.ngxhtpasswd tom
# htpasswd -m ?/etc/nginx/.ngxhtpasswd jerry
11、https服務
前提:生成私鑰,生成證書簽署請求,并獲得證書;
server {
listen 443 ssl;
server_name www.magedu.com;
ssl_certificate ? ? ?cert.pem; ? ? ? ? ?指明證書文件位置
ssl_certificate_key ? ? ?cert.key; ? ?指明私鑰文件位置
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root ? ?/vhosts/web1;
index ? ?index.html index.htm;
}
}
12、stub_status {on|off};
通過指定的uri輸出stub status;僅能用于location上下文;
location ?/status ? ?{
stub_status on;
allow ? ?172.20.120.0/23
deny ? all;
}
結果示例:
Active connections: 5
server ? ? ? ? ? ? ? accepts ? ? ? ? ? ? ? handled ? ? ? ? ? ? requests
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?234 ? ? ? ? ? ? ? ? ? ? ? 234 ? ? ? ? ? ? ? ? ? ?462
Reading: 1 ? ? ? ? ? ? ? Writing: 5 ? ? ? ? ? ? ? ?Waiting: 12
Active connections:當前所有處于打開狀態的連接數;
accepts:已經接受過的連接數;
handled:已經處理過的連接數;
requests:已經處理過的請求數;在“保持連接”模式下,請求數量可能會多于連接數量;
Readking:正處于接收請求狀態的連接數;
Writing:請求已經接收完成,正處于處理請求或發送響應的過程中的連接數;
Waiting:保持連接模式,且處于活動狀態的連接數;
13、rewrite ? ? regex ? ? ?replacement ? ? [flag];
把用戶請求的URI基于regex做檢查,匹配到時將替換為replacement指定的字符串;
在同一個location中存在的多個rewrite規則會自上而下逐個被檢查(循環);可以使用flag控制此循環功能;
regex:正則表達式,用于匹配用戶請求的url;
replacement:重寫為的結果;
[flag]:
last:一旦此rewrite規則重寫完成后,就不再被后面其他的rewrite規則進行處理;而是由user agent重新對重寫后的URL再一次發起請求,并從頭開始執行類似的過程;
break:一旦此rewrite規則重寫完成后,由user agent對新的URL重新發起請求,且不再會被當前location內的任何rewrite規則所檢查;
redirect:以302響應碼(臨時重定向)返回新的URL;
permanent:以301響應碼(永久重定向)返回新的URL;
?例如:
rewrite ? ?^/images/(.*\.jpg)$ ? ? /imgs/$1 ? ?break;
http://www.magedu.com/images/a/1.jpg ? ? ?—> ? ?http://www.magedu.com/imgs/a/1.jpg
14、if
語法 if (condition) ?{…}
應用環境:server,location
condition:
(1)變量名:
變量值為空串,或者以“0”開始,則為fault,其他均為true;
(2)以變量為操作數構成的比較表達式
可使用=,!=類似的比較操作符進行測試;
(3)正則表達式的模式匹配操作;
~:區分大小寫的模式匹配檢查;
~*:不區分大小寫的模式匹配檢查;
!~和!~*:對上面兩種測試取反;
(4)測試路徑為文件的可能性:-f,!-f
(5)測試路徑為目錄的可能性:-d,!-d
(6)測試文件的存在性:-e,!-e
(7)檢查文件是否有執行權限:-x,!-x
例如:
? ? ? if ? ?($http_user_agent ~* MSIE) {
? ? ? ? ? ? ?rewrite ? ^(.*)$ ?/msie/$1 break;
?}
15、防盜鏈
location ? ~* ? \.(jpg|gif|jpeg|png)$ ? {
? ? ? ? ? valid_referer ? none ?blocked ? www.lewis.com;
? ? ? ? ? if ? ? ($invalid_referer) ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? rewrite ? ? ^/ ? ?http://www.lewis.com/403.html;
? ? ? ? ? ? }
}
16、定制訪問日志
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;
注意:此處可用變量為nginx各模塊內建變量;
網絡連接相關配置:
1、keepalive_timeout ?#; ? ? ? ? ?長連接的超時時長,默認為75s;
2、keepalive_requests ?#; ? 在一個長連接上所能夠允許請求的最大資源數;
3、keepalive_disable [msie|safari|none]; ? ?為指定類型的user agent禁用長連接;
4、tcp_nodelay ? on|offf; ? ? 是否對長連接使用TCP_NODELAY選項;
5、client_header_timeout ?#; ? ?讀取http請求報文首部的超時時長;
6、client_body_timeout ? #; ? ? ?讀取http請求報文body部分的超時時長;
7、send_timeout ? #; ? ? ?發送響應報文的超時時長;
LNMP:php啟用fpm模型;
例如:
location / {
root ? ? ? ? ? /vhosts/web1;
index ? ? ? ? ? index.php ? ? index.html ? ? ? ? index.htm;
}
location ~ \.php$ {
root ? ? ? ? ? ? ?/vhosts/web1;
fastcgi_pass ? ? ? ? ? ? ?127.0.0.1:9000;
fastcgi_index ? ? ? ?index.php;
fastcgi_param ? ? ? ? ? SCRIPT_FILENAME ? ? ? ? /vhosts/web1/$fastcgi_script_name;
include ? ? ? ? ? ? ? ?fastcgi_params;
}
nginx反向代理:proxy、fastcgi、upstream
ngx_http_proxy_module:實現反向代理及緩存功能;
(1) proxy_pass URL;
應用上下文:location, if in location, limit_except
proxy_pass后面的路徑不帶uri時,其會將location的uri傳遞給后端的主機;下面的示例會將/uri/傳遞給backend服務器;
location ?/uri/ {
proxy_pass http://hostname;
}
proxy_pass后面的路徑是一個uri時,其會將location的uri替換為后端主機自己的uri;
location ?/uri/ {
proxy_pass http://hostname/new_uri/;
}
如果location定義其uri時使用的正則表達式模式匹配,則proxy_pass后的路徑不能夠使用uri;
location ?~* ?\.(jpg|gif|jpeg)$ ?{
proxy_pass ?http://HOSTNAME;
}
?此處的http://HOSTNAME后面不能有任何uri,哪怕只有/也不可以;
(2) proxy_set_header field value;
用于proxy server向backend server發請求報文時,將某請求首部重新賦值,或在原有值后面添加一個新的值; 也可以添加自定義首部;
?示例:
proxy_set_header ?X-Real-IP ?$remote_addr;
proxy_set_header ?X-Forwarded-For ?$proxy_add_x_forwarded_for;
原有請求報文中如果存在X-Forwared-For首部,則將remote_addr以逗號分隔補原有值后,否則則直接添加此首部;
實踐作業:
假如nginx有兩個server(虛擬主機),且均反代至后端某一個主機,此主機亦有兩個虛擬主機,虛擬主機名與nginx的相同;
要求:用戶請求nginx的哪一個虛擬主機,就將其代理后后端主機的對應的虛擬主機;
緩存相關的選項(緩存要先定義,后調用):
(3) proxy_cache_path ? ?path ? ?[levels=levels] ? [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size]
定義緩存;可用上下文為http;
(4) proxy_cache zone | off;
調用緩存;可用上下文 為http, server和location;
(5) proxy_cache_key string;
定義緩存鍵;
proxy_cache_key $scheme$proxy_host$request_uri;
(6) proxy_cache_valid [code …] time;
對不同響應碼的響應設定其可緩存時長;
示例:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 ? ? ?1m;
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off …;
處于什么狀態下,以舊的緩存響應;
proxy_cache_bypass ?string:設置在何種情況下nginx將不從cache取數據;
$cookie_nocache ?$arg_nocache ?$httpd_authorization
跟連接相關的選項
(7) proxy_connect_timeout time;
定義與后端服務器建立連接的超時時長;默認為60s,不建議超出75s;
(8) proxy_send_timeout time;
把請求發送給后端服務器的超時時長;默認為60s;
(9) proxy_read_timeout time;
等待后端服務器發送響應報文的超時時長;
ngx_http_upstream_module:定義服務器組
用于將多個服務器定義成服務器組,而由proxy_pass, fastcgi_pass等指令進行引用;
(1) upstream name { … }
定義一個后端服務器組,name為組名稱;僅能用于http上下文 ;
(2) server address [parameters];
在upstream中定義一個服務器及其相關參數;僅能用于upstream上下文;
常用參數:
weight=number:定義服務器權重,默認為1;
max_fails=number:最大失敗連接嘗試次數,失敗連接超時時長由fail_timeout參數指定;
fail_timeout=number:等待目標服務器發送響應的時長;
backup:備用服務器,所有主服務器均故障時才啟用此主機;
down:手動標記其不再處理任何用戶請求;
使用方法:
(a) 定義upstream服務器組
upstream websrvs {
server 172.16.100.68 weight=2 max_fails=2 fail_timeout=6s;
server 172.16.100.6 ?weight=1 max_fails=2 fail_timeout=6s;
}
(b) 在反代場景中(proxy_pass, fastcgi_pass, …)進行調用;
location / {
proxy_pass http://websrvs/;
}
(3) ip_hash;
源地址hash,把來自同一個ip地址的請求始終發往同一個backend server,除非此backend server不可用;
(4) least_conn;
最少連接;當各server權重不同時,即為加權最少連接;
(5) health_check [parameters];
健康狀態檢測機制;只能用于location上下文;建議關閉訪問日志;
常用參數:
interval=time檢測的頻率,默認為5秒;
fails=number:判定服務器不可用的失敗檢測次數;默認為1次;
passes=number:判定服務器可用的失敗檢測次數;默認為1次;
uri=uri:做健康狀態檢測測試的目標uri;默認為/;
match=NAME:健康狀態檢測的結果評估調用此處指定的match配置塊;
(6) match name { … }
對backend server做健康狀態檢測時,定義其結果判斷機制;只能用于http上下文;
常用的參數:
status ?code[ ?code …]: 期望的響應狀態碼;
header ?HEADER[operator ?value]:期望存在響應首部,也可對期望的響應首部的值基于比較操作符和值進行比較;
body:期望響應報文的主體部分應該有的內容;
(7) hash key [consistent];
指明基于hash方式進行調度時,其hash key;
hash ?$remote_addr相當于ip_hash;
常用的hash key:
$cookie_name:將一個用戶的請求始終發往同一個backend server,能實現會話綁定的功能;此處的name為cookie某些參數的名稱,此處常用的有cookie_username;
$request_uri: 將對同一個uri的請求始終發往同一個backend server,后端為cache server時特別有用;
session會話保持:
session sticky:基于ip, ngix還可基于請求報文首部中的多種信息,例如cookie, uri;
session cluster:每個server均把創建和維護session同步集群中的其它主機;僅適用于較小規模的環境;
session server:使用一個共享的存儲服務存儲session信息;
ngx_http_headers_module模塊配置
?(1) add_header name value [always];
向響應報文添加自定義首部,并為其賦值;
示例:
add_header realserver $server_addr; ? ? ? ? ? ? ? ? ? ? 添加真實服務器地址的自定義首部
add_header cache_status $upstream_cache_status; ? ? ? ? ? ? ? ? 添加nginx緩存狀態的自定義首部
(2) expires [modified] time;
? ? ?expires epoch | max | off;
允許或禁止向響應報文的Cache-Control或Expires首部添加新值或修改其值;
fastcgi模塊指令:
?(1) fastcgi_pass address;
address為fastcgi server監聽的地址和端口;
示例:fastcgi_pass ? 127.0.0.1:9000;
(2) fastcgi_index name;
定義fastcgi應用的默認主頁;
示例:fastcgi_index ?index.php;
(3) fastcgi_param parameter value [if_not_empty];
設定傳遞給后端fastcgi server參數及其值;
示例:fastcgi_param ?SCRIPT_FILENAME ?/scripts$fastcgi_script_name;
/index.php –> /scripts/index.php
http://www.magedu.com/users.php?username=tom
(4) fastcgi_cache_path path ?[levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size];
定義緩存:緩存空間等;
可應用的上下文 :http
緩存機制:
元數據:內存,即為keys_zone;
數據:磁盤,即為path;
path:文件系統路徑,用于儲存緩存的文件數據;
levels=#[:#[:#]]:緩存目錄層級定義;
levels=2:1
keys_zone=name:size
內存中用于緩存k/v映射關系的空間名稱及大小;
name: cache的標識符;
size:元數據cache大?。?/div>
max_size:緩存空間上限;
注意:只能用于http上下文;
(5) fastcgi_cache zone | off;
調用定義過的緩存;
zone即為通過fastcgi_cache_path定義緩存時其keys_zone參數中的name;
(6) fastcgi_cache_key string;
定義如何使用緩存鍵;
使用示例:fastcgi_cache_key ? $request_uri;
(7) fastcgi_cache_methods GET | HEAD | POST …;
為何請求方法對應的請求進行緩存,默認為GET和HEAD;
(8) fastcgi_cache_min_uses number;
緩存項的最少使用次數;
(9) fastcgi_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_503 | http_403 | http_404 | off …;
是否可使用stale緩存項響應用戶請求;
(10) fastcgi_cache_valid [code …] time;
對不同響應碼的響應設定其可緩存時長;
示例:
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid 404 ? ? ?1m;
注意:調用緩存時,至少應該指定三個參數
fastcgi_cache
fastcgi_cache_key
fastcgi_cache_valid
php-fpm的工作方式:
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
pm = dynamic|static
pm.start_servers:啟動fpm進程時啟動的工作進程數量;
pm.min_spare_servers:最少空閑進程數;
pm.max_spare_servers:最大空閑進程數;
pm.max_children:最大工作進程數;
user = USERNAME
group = GROUPNAME
示例:location / {
root ? ? ? ? ? /vhosts/web1;
index ? ? ? ? ? index.php ? ? index.html ? ? ? ? index.htm;
}
location ~ \.php$ {
root ? ? ? ? ? ? ?/vhosts/web1;
fastcgi_pass ? ? ? ? ? ? ?127.0.0.1:9000;
fastcgi_index ? ? ? ?index.php;
fastcgi_param ? ? ? ? ? SCRIPT_FILENAME ? ? ? ? /vhosts/web1/$fastcgi_script_name;
include ? ? ? ? ? ? ? ?fastcgi_params;
}
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/101956