listen 指令只能使用與server字段里
如果本地調用可以監聽本地Unix套接字文件,性能更加,因為不用走內核網絡協議棧
listen unix:/var/run/nginx.sock;
針對端口加地址的監聽;表示之匹配127.0.0.1的8000端口請求
listen 127.0.0.1:8000;
監聽本機所有IP的端口
listen 8000;
或者這么表示
listen *:8000;
監聽IPV6地址
listen [::]:8000 ipv6only=on;
處理HTTP請求頭部流程
內核與客戶端建立好tcp 連接后,根據客戶端的訪問的端口判斷交給系統那個系統處理,這是我們的nginx監聽的端口(448或者80),是客戶端請求的端口,這是內核會根據負載均衡算法選擇一個work進程里的一個epoll_wait方法會返回已建立好的句柄,這是一個讀事件,讀取請求,跟據請求調用accept方法分配連接內存池,接下來就是http模塊處理 調用ngx_http_init_connection方法讀取事件添加epll_ctl中,並添加一個定時器,如果60秒沒有收到請求,就會超時;讀取用戶請求數據從內核,然后在內核的用戶態分配內存,默認分配1k空間可以設置,
接收到用戶請求后會分配請求內存池,默認4K可以做調整,然后用狀態機解析請求的行,解析的時候如果發現url地址1k內存放不下,nginx會自動擴充內存,默認最大擴充到4 8k,表示先把那1k的數據復制8k里,用剩下的7k在去接受用戶剩下的url,如果還不夠就會在分配8k,默認最大分配32k,靠nginx內置變量標識url,然后解析http的header部分,在分配大內存主意這個大內存與URL的大內存共用的,標識頭部確定那個server塊處理請求,當標識完全部header后,就移除定時器,開始11個階段的http請求處理
nginx的正則
元字符
. 可以匹配除換行符以外的任意字符
\w 可以匹配字母或者數字會在下划線或者數字
\s 匹配任意的空白字符
\d 匹配數字
\b 匹配單詞開始或結束
^匹配字符串的開始
$匹配字符串的結束
重復
* 重復零次或多次
+ 重復1次或更多次
?重復零次或一次
{n} 重復n次
{n,}重復n次或者更多次
{n,m}重復n到m次
實例
server name 指令
server { server_name chenxi.com www.cx.com; server_name_in_redirect off; return 302 /redirect; } [root@nginx conf]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.10.90 chenxi.com www.cx.com [root@nginx conf]# nginx -s reload [root@nginx conf]# curl http://www.cx.com -I HTTP/1.1 302 Moved Temporarily Server: nginx/1.15.12 Date: Sat, 08 Jun 2019 22:16:19 GMT Content-Type: text/html Content-Length: 146 Location: http://www.cx.com/redirect 默認返回的是你訪問的域名加后面的redirect Connection: keep-alive 調整配置文件 server { server_name chenxi.com www.cx.com; server_name_in_redirect on; 改成on return 302 /redirect; } 測試 [root@nginx conf]# nginx -s reload [root@nginx conf]# curl http://www.cx.com -I HTTP/1.1 302 Moved Temporarily Server: nginx/1.15.12 Date: Sat, 08 Jun 2019 22:19:27 GMT Content-Type: text/html Content-Length: 146 Location: http://chenxi.com/redirect 返回的是主域名跳轉 Connection: keep-alive
其他用法
.chenxi.com可以匹配chenxi.com和*.chenxi.com
_匹配所有
“”匹配沒有傳遞的Host頭部
server匹配順序
精確匹配
*.在前面的泛域名
*.在后面的泛域名
文件中順序正則匹配的域名
default server
第一個listen指定的default
http 的11個階段
realip模塊可以獲取真實客戶端地址
如何拿到用戶真實IP
拿到用戶IP如何使用
默認不會編譯到nginx中的, --with-http_realip_module 將模塊編譯到nginx中
模塊指令的介紹
set_real_ip_from address
| CIDR
| unix:
;
可用范圍:http
, server
, location
默認值為空
表示從這個IP發來的請求從頭部去取用戶的IP;定義的是前端代理或者cdn地址
real_ip_header field
| X-Real-IP
| X-Forwarded-For
| proxy_protocol
; 定義要取得變量默認X-Real-IP
可用范圍:http,server,location
real_ip_recursive on | off; 表示如過客戶端IP與代理IP相同之間跳過 real_ip_recursive off; 默認 可用范圍:http,server,location
修改配置文件查看效果
server{ server_name chenxi.com www.cx.com; error_log logs/myerror.log debug; set_real_ip_from 192.168.10.90; real_ip_recursive off; real_ip_header X-Forwarded-For; location /{ return 200 "Client real ip : $remote_addr\n"; } } nginx -s reload 測試 [root@nginx conf]# curl -H "X-Forwarded-For: 1.1.1.1,192.168.10.90" chenxi.com Client real ip : 192.168.10.90
修改配置文件開啟real_ip_recursive on 查看效果
server{ server_name chenxi.com www.cx.com; error_log logs/myerror.log debug; set_real_ip_from 192.168.10.90; real_ip_recursive on; real_ip_header X-Forwarded-For; location /{ return 200 "Client real ip : $remote_addr\n"; } } nginx -s reload [root@nginx conf]# curl -H "X-Forwarded-For: 1.1.1.1,192.168.10.90" chenxi.com Client real ip : 1.1.1.1 觸發了動作使用之前的地址
官網介紹http://nginx.org/en/docs/http/ngx_http_realip_module.html#set_real_ip_from
http_rewrite_module 模塊介紹
return 指令介紹
實例
server { server_name haha.com; listen 8080; root html/; error_page 404 /403.html; #return 405; location /{ #return 404 "find nothing!\n"; } } nginx -s reload 加載測試 [root@nginx html]# echo "sdddf" > 403.html [root@nginx vhost]# curl http://haha.com:8080/aa.html sdddf 修改配置文件 server { server_name haha.com; listen 8080; root html/; error_page 404 /403.html; #return 405; location /{ return 404 "find nothing!\n"; } } nginx -s reload 加載測試 [root@nginx vhost]# curl http://haha.com:8080/aa.html find nothing!
修改配置
[root@nginx vhost]# vim test.conf server { server_name haha.com; listen 8080; root html/; error_page 404 /403.html; return 405; location /{ return 404 "find nothing!\n"; } } [root@nginx conf]# nginx -s reload [root@nginx vhost]# curl http://haha.com:8080/aa.html <html> <head><title>405 Not Allowed</title></head> <body> <center><h1>405 Not Allowed</h1></center> <hr><center>nginx/1.15.12</center> </body> </html>