實驗版本: 1.10.2
相關命令:
# nginx -t //檢查配置文件格式
#nginx -s reload //重新載入配置文件
實驗:
-
主配文件大概組成
主配置文件的設定 /etc/nginx/nginx.conf
events{..} //事件驅動相關
http{..} //網站服務相關
-
全局配置段解讀與實驗:
user nginx; // 運行nginx 的用戶
worker_processes auto; //worker響應進程的數量;全局配置優化點;綁定CPU不切換
error_log /var/log/nginx/error.log; //錯誤日志存儲路徑
pid /var/run/nginx.pid; //PID 的路徑
include /usr/share/nginx/modules/*.conf; //子配置文件的路徑
實驗1.cpu 與進程綁定優化 #ab -n 100000 -c 10 http://172.18.48.61/index.html
未綁定:
綁定后:
8核示例:
官方說明:自動綁定
The special value auto (1.9.10) allows binding worker processes automatically to available CPUs:
worker_processes auto;
worker_cpu_affinity auto;實驗二:nice值設定
worker_priority -10;
實驗三: 限定每個worker可打開的文件數// 稍稍比并發數多就可以
Changes the limit on the largest size of a core file (RLIMIT_CORE) for worker processes. Used to increase the limit without restarting the main process.
Syntax: worker_rlimit_nofile number;
Default: —
Context: main
-
調試 定位問題:
- daemon on|off //正常使用時 centos6 on; centos7 off( 7 有system 來守護)
-
-
error_log file [level] // 需要指明;錯誤日志是全局的
-
事件驅動 events{}
- worker_connections # //單worker 連接的數量
-
use method :// 指明并發連接的請求處理方法:
- 示例 use epoll;
-
accept_mutex on|off //on 意為著來請求時各worker輪流處理;off 意味著隨機分發
-
http 服務的相關設置:
-
server{…};//具體一個虛擬主機的設置
-
基本設置:
-
listen IP:PORT //設定監聽的地址和端口
listen PORT|address[:port]|unix:/PATH/TO/SOCKET_FILE
listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size]
-
常用可選:
- default_server // 默認虛擬主機
- backlog=# //后續隊列長度
- rcvbuf=# //接收緩存大小
- sndbuf=# // 發送緩存大小
-
-
server_name name…; //指明虛擬主機的名字
- 精確指明: // www.zhlznana.com
- 模0糊0
- *匹配指明: // *.zhlznana.com
-
正則表達式指明 // ~^www.zhlzna+.com$
注:匹配規則機制:
精確匹配 > *左側匹配 > *右側匹配 > * 正則表達式匹配
-
tcp_nodelay on|off
- 是否開啟 nodelay 選項;// 小資源是否攢一起發送
- 只適用用keep alive 時
-
sendfile on|off
- 是否開啟 send file機制。//內核直接轉發
-
-
定義路徑相關配置//指定文件夾配置
root path;
-
示例:
- alias 定義時 //location 后面的字段末尾不要有/
-
結果:
root定義時
一個server中多個locate時://location 不包含域名部分
多個location示例:
location / {
root /data/www;
index index.html;
}
location ~* .(jpg|png|jpeg|gif)$ {
root /data/images/;
}
錯誤頁面重定向: 也可以重定向到某個location /xxxx
- 基本模板:error_page code … [=[response]] uri;
-
示例:
-
-
- 重定向到
-
-
try_files: 按次序檢查文件
-
-
客戶端的響應操作的配置
-
keepalive_timeout timeout [header_timeout];
設定保持連接的超時時長,0表示禁止長連接;默認為75s;75太長減小
-
示例: //實驗為5秒后自動斷開
-
-
keepalive_requests number; //與上面的時間 任何一個滿足即斷開
-
示例:
-
- 請求兩次即關閉
-
-
-
-
keepalive_disable none | browser …; //這個選項很少適用了
-
send_timeout time; //發送響應報文超時后斷開時間
向客戶端發送響應報文的超時時長,此處,是指兩次寫操作之間的間隔時長;
-
client_body_buffer_size size; // 如果客戶端提交的報文大與設定值
用于接收客戶端請求報文的body部分的緩沖區大?。荒J為16k;超出此大小時,其將被暫存到磁盤上的由client_body_temp_path指令所定義的位置;
-
client_body_temp_path path [level1 [level2 [level3]]]; //與上一條配合適用
-
存儲機制為緩存機制;
示例: client_body_temp_path /var/tmp/client_body 1 2 2;
意義:緩存路徑為 /var/tmp/client_body 16進制基于md5校驗碼生成
1:一位16進制表示1級子目錄
2:兩位16進制表示二級子目錄
2:兩位16進制表示三級子目錄
-
-
limit_rate rate; //限制用戶速率;例如百度云盤
限制響應給客戶端的傳輸速率,單位是bytes/second,0表示無限制;
-
limit_except method … { … } //對某IP限定某請求方法
limit_except GET {
allow 192.168.1.0/32;
deny all;
}
注: 這里限定是除了GET 和 HEAD 之外的method
-
-
服務器端文件的讀取優化配置:
-
aio on | off | threads[=pool]; //默認開啟的
是否啟用aio功能
-
directio size | off;
在Linux主機啟用O_DIRECT標記,此處意味文件大于等于給定的大小時使用,例如directio 4m;
-
open_file_cache off; //這個表示關閉
open_file_cache max=N [inactive=time]; //這個表示開啟
nginx 緩存的信息:
1.文件的描述符 (主要是文件的元數據)
2.打開的目錄結構 (索引路徑)
3.沒有找到的或者沒有權限訪問的文件相關信息
max=N //設定可緩存的上限,達到上限后會適用LRU算法實現緩存管理
inactive=time //此處指定的時間內訪問次數少于open_file_cache_min_uses 所限制的次數即為非活動項
-
open_file_cache_valid time;// 檢查頻率
緩存項有效性的檢查頻率;默認為60s;
-
open_file_cache_min_uses number; //界定訪問低于多少次
在open_file_cache指令的inactive參數指定的時長內,至少應該被命中多少次方可被歸類為活動項
-
open_file_cache_errors on | off; //
是否緩存查找時發生錯誤的文件一類的信息;
-
-
模塊的具體控制:
ngx_http_access_module模塊:基于IP訪問控制功能
allow address | CIDR | unix: | all;
deny address | CIDR | unix: | all;
示例:
/bbs 這個location 允許172.18.0.0/16 段的訪問,但200這個地址不能訪問;
其他的進制訪問
實驗:
本機電腦ip:為148.68
148.99 這個虛擬機可以訪問:
取消限制:
訪問可OK了
-
ngx_http_auth_basic_module模塊 //基于用戶訪問控制,使用basic機制進行用戶認證
auth_basic string | off; //顯示提示內容
auth_basic_user_file file; //密碼保存的文件路徑
注:需要安裝httpd-tools
# htpasswd :命令
配置文件:
-
創建用戶密碼文件:
# htpasswd -c -m /app/.nginxpasswd zhlz //-c 第一次創建時使用;文件名與配置文件中的指向一致
訪問測試:
-
-
ngx_http_stub_status_module模塊// 用于查看當前網站情況;
配置示例:
location /basic_status {
stub_status;
}
測試連接:
各參數說明:
Active connections: 活動狀態的連接數;
accepts:已經接受的客戶端請求的總數;
handled:已經處理完成的客戶端請求的總數;
requests:客戶端發來的總的請求數;
Reading:處于讀取客戶端請求報文首部的連接的連接數;
Writing:處于向客戶端發送響應報文過程中的連接數;
Waiting:處于等待客戶端發出請求的空閑連接數;
-
log_format name string …; //定義日志格式:
acces_log /app/zhlz/access_log nana;
訪問日志文件路徑,格式及相關的緩沖的配置;
buffer=size //緩沖日志條目數量
flush=time
nginx 相關變量說明:? 點我點我
相關配置文件//注:日志格式一定設定在http 下
相關文件夾設定nginx的權限:
setfacl -R -m u:nginx:rwx zhlz
測試:
-
open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off; //設置日志緩存文件打開的數量
注:這里緩存的時日志文件的指向
max:緩存的最大文件描述符數量;
min_uses:在inactive指定的時長內訪問大于等于此值方可被當作活動項;
inactive:非活動時長;
valid:驗正緩存中各緩存項是否為活動項的時間間隔;
-
-
-
-
ngx_http_gzip_module // nginx gzip壓縮模塊;節約帶寬
常用指令: 官方指南
gzip on|off |
http,server,location,if in location |
gzip_buffers number size |
http, server, location |
gzip_proxied |
http,server,location |
gzip_min_length length |
http,server,location |
注:nginx 做代理服務器時的設置// any 表示只要符合本代理設置的壓縮type 則后端發來的任何信息均壓縮
實驗步驟:
1.設定配置文件
gzip on;
2.測試連接:
未開啟壓縮時:
開啟壓縮時:
設置最小壓縮長度:
gzip_min_length 64;:
編輯資源文件:
#echo asdfsafsafsadfsdafsdfsdafsadfdsa > /app/zhlz/message.html
連接結果://沒有被壓縮
其他配置:
- ngx_http_ssl_module // 加密模塊
ssl on|off |
ssl 的開關 |
http,server |
|
ssl_certificate file |
證書的路徑 |
http, server |
|
ssl_certificate_key file |
私鑰地址 |
http, server |
|
ssl_session_cache off | none | [builtin[:size]] [shared:name:size]; |
會話是否緩存 |
http server |
builtin:worker私有會話 |
實驗:
生成證書過程略…
修改配置文件://注:listen 端口的修改
鏈接實驗:
-
ngx_http_rewrite_module //舊資源變更過需要重寫請求URL
官方指南 ? 點它
rewrite regex replacement [flag] |
重寫請求URI |
server,location,if |
|
rewrite_log on|off |
是否開啟日志 |
http,server,location,if |
//寫入錯誤日志 |
flag:
last:替換完成后重新檢查location (默認)
break:跳出可能出現的死循環
redirect:返回客戶端新的URL (302)
permanent
實驗示例:
1.編寫配置文件://最好定義在server下,因為break以后要從跳出點向下搜尋location
2.測試鏈接:
3.測試用curl 命令實驗//全部被重定向未2.html 里的值
redirect的測試://設置404錯誤的重定向
配置文件:
鏈接測試:
定位到:
查看報文信息://前三個的定向
- ngx_http_referer_module //nginx 跳轉過來的url識別;防盜鏈
valid_referers none |blocked|server_names|string… |
server,location |
可以接受的來源 |
|
Specifies the “Referer” request header field values that will cause the embedded $invalid_referer variable to be set to an empty string. Otherwise, the variable will be set to “1“. Search for a match is case-insensitive.
$invalid_referer: 如果來源符合要求,則這個內嵌變量則為0 或者空串,否則則被設定為1
none: 直接鍵入的URL 非跳轉過來的
blocked: 經過網關或代理處理過的,無法識別的
server_names :指定域名,可以通配符*
~ 可以正則表達式:
實驗示例:
示例:valid_referers none blocked server_names *.zhlzna.com ~.baidu. ~.google.;
配置文件:
valid_referers none blocked server_names *.zhlzna.com;
if ($invalid_referer){
return 302 http://www.zhlzna.com; //這里是重定向跳轉的頁面
}
測試鏈接://利用curl 可以指定跳轉來源的選項來測驗:
#curl -e http://www.baidu.com http://www.zhlzna.com
-
ngx_http_proxy_module //代理相關
官方說明: 點我
proxy_pass URL; |
指向被代理服務器 |
location if in location limit_except |
基本寫法 |
proxy_set_header field value |
修改代理發出的請求頭部信息 依賴于擴展頭部的自定義 如果不設定,則默認使用: proxy_set_header Host $proxy_host;
|
|
|
實驗1:proxy_pass 的用法;
注意代理與被代理服務器的網段號:
這個語句不能在server和http中使用
配置文件://只要是CURL 來訪問的就跳轉
鏈接實驗:
注意:
配置示例
鏈接測驗
實驗2:
代理指向域名的示例:
鏈接測試
實驗3: nginx +nginx 組合 客戶端真實IP的傳遞
參考網絡文章: 點我
代理服務器配置文件: //傳遞各種參數
50 location ~* .jpg$ {
52
53 proxy_pass http://192.168.0.3;
54 proxy_set_header X-Real-Ip $remote_addr;
55 proxy_set_header X-Forwarded-For $remote_addr;
56 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
57 proxy_set_header Host $host;
58 proxy_redirect off;
59
61
62 }
服務器端配置文件:
23 set_real_ip_from 192.168.0.0/16;
24 real_ip_header X-Real-Ip;
日志格式文件
‘$remote_addr //這個參數就會自動變為代理服務器傳遞過來的真實IP
鏈接示例的日志文件://這里的IP 已經改為真正客戶端的IP
代理服務器的緩存設置//基本常用的
proxy_cache zone |off |
開關緩存 zone為緩存設定的名字 |
http, server, location |
|
proxy_cache_key string |
設定緩存的鍵值 |
http server location |
|
proxy_cache_path path [levels=levels] [use_temp_path=on|off]keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; |
設定緩存的存儲路徑; 緩存內存的映射地址:大小 |
http |
|
proxy_cache_valid [code …] time |
設定各種狀態響應碼的緩存時間 |
http server location |
|
緩存實驗:
代理端設置:
59 proxy_cache zhlzcache;
60 proxy_cache_key $request_uri;
61 proxy_cache_valid 200 301 302 10m;
62 proxy_cache_valid any 1m;
代理端的全局配置:
proxy_cache_path /app/nginxcache levels=2:2:1 keys_zone=zhlzcache:10m; //需要在http 段設置
壓力測試:
1.沒有緩存時:
2.有緩存時:
被代理端有問題時代理服務器如何處理
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 |http_503 | http_504 | http_403 | http_404 | http_429 | off …; |
定義哪些錯誤時啟用緩存響應;off都不用 |
http,server, location |
|
定義哪些錯誤時啟用緩存響應;默認為 off 都不用
實驗:
配置:
測試:先訪問
將服務器文件改名
再次刷新服務器://出來啦
服務器端響應報文的首部信息處理:
proxy_hide_header field |
http, server, location |
額外指定隱藏某些首部 |
|
注:By default, nginx does not pass the header fields “Date”, “Server”, “X-Pad”, and “X-Accel-…” from the response of a proxied server to a client.
設定代理與服務器之間的鏈接時間:
proxy_connect_timeout time; |
http, server, location |
|
|
注:默認為60S ,但不能超過75秒
基于域名的轉發實驗:
在服務器端添加虛擬主機:
1 server {
2 listen 80;
3 server_name www.yaoxiaona.com;
4 sendfile on;
5 root /app/yaoxiaona;
6 }
在代理服務器端設置響應的虛擬主機名: //摘要
server {
6 listen 80;
7 server_name www.yaoxiaona.com;
proxy_pass http://www.yaoxiaona.com;
鏈接測試:
首部專門處理模塊
首部的X-Via 參考確認
實驗: 編輯首部// 添加代理IP:
修改配置:
add_header X-Proxy $server_addr;
測試鏈接:
未填加代理IP時:
填加代理IP時:
實驗:創建NP 組合://這里以本機組合示例
CENTOS7 為例: yum install php-fpm
編輯配置文件:
#vim /etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000 //修改監聽IP端口;監聽所有9000端口
listen.allowed_clients = 127.0.0.1 //允許主機鏈接設置
39 user = nginx //與什么類型的服務器綁定就指定什么名字 ;apache
40 ; RPM: Keep a group allowed to write in log dir.
41 group = nginx
;pm.max_requests = 500 //每個子進程處理多少個請求
121 pm.status_path = /pm_status //記錄該PHP狀態
133 ping.path = /pm_ping //探測接口
138 ping.response = pong //回應探測
下面的需要注意后期可能用到
啟動服務
# systemctl start php-fpm.service
創建目錄:
#mkdir /app/php-fpm/shop -pv
創建測試文件
#vim index.php
配置代理端:的相關配置文件
50 location ~ .php$ {
51 root html;
52 fastcgi_pass 192.168.0.4:9000;
53 fastcgi_index index.php;
54 fastcgi_param SCRIPT_FILENAME /app/php-fpm/shop/$fastcgi_script_name;
55 include fastcgi_params;
56 }
重啟nginx 服務并測試鏈接:
測試在服
1 <?php
2 #phpinfo();
3 $conn = mysql_connect(‘127.0.0.1′,’myuser’,’mypass’);
4 if ($conn)
5 echo “OK”;
6 else
7 echo “lala”;
8 ?>
測試鏈接;
配置查看狀態和ping 的配置文件:
location ~* ^/(pm_status|pm_ping)$ { //注意跟PHP服務配置文件的名字一致
30 include fastcgi_params;
31 fastcgi_pass 192.168.0.4:9000;
32 fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
33
34 }
測驗鏈接:
fastcgi_cache //配置類似于靜態資源緩存
fastcgi 的緩存 //類似與之前的緩存 ; 都需要壓力測試
示例:
壓測截圖:
提升性能,保持連接
備注:
請求首部變量:
Client-Ip |
客戶端首部 |
Host |
接收請求的主機名+端口 |
From |
客戶EMAIL |
Referer |
當前URI的URL |
UA-OS |
客戶端的OS信息 |
User-Agent |
客戶的請求程序 |
響應首部變量
Age |
響應持續時間 |
Retry-After |
資源不可用,下次重試 |
Server |
服務器名稱 版本 |
Title |
對于HTML文檔,即標題 |
Warning |
警告報文 |
原創文章,作者:zhoulz,如若轉載,請注明出處:http://www.www58058.com/75327
寫的很詳細。
船長666