1、Nginx目錄索引
syntax:autoindex on | off;
Default: autoinde off;
Context: http,server,location
autoindex_exact_size off;
默認(rèn)值on,顯示出文件的確切大小,單位是bytes
修改為off,顯示出文件的大概大小,單位是MB或者GB。
autoindex_localtime on;
默認(rèn)值off,顯示的文件時間為GMT時間。
修改為on,顯示的文件時間為服務(wù)器的時間。
charset utf-8,gbk;
默認(rèn)中文目錄亂碼,添加上解決亂碼。
Examples:
[root@web02 conf.d]# vim /etc/nginx/conf.d/nfzl.conf
server {
? ? ? ? listen 80;
? ? ? ? server_name nfzl.com;
? ? ? ? location / {
? ? ? ? ? ? ? ? root /code;
? ? ? ? ? ? ? ? index index.html;
? ? ? ? }
? ? ? ? location /download {
? ? ? ? ? ? ? ? root /code;
? ? ? ? ? ? ? ? autoindex on;
? ? ? ? ? ? ? ? charset utf-8,gbk;
? ? ? ? ? ? ? ? autoindex_exact_size off;
? ? ? ? ? ? ? ? autoindex_localtime on;
? ? ? ? }
}
2、Nginx狀態(tài)監(jiān)控
1)ngx_http_stub_status_moduld用于展示Nginx連接狀態(tài)信息,需要--with-http_stub_status_module模塊支持
Syntax: stub_status;
Default: -
Context:server,location
2) 配置Nginx status
location /nginx_status {
stub_status;
access_log off;
}
3) 使用瀏覽器訪問http://IP/nginx_status訪問后得到的結(jié)果
Active connections: 2
server accepts handled requests
2 ????????????2???????? 2
Reading: 0 Writing: 1 Waiting: 1
Active connections: 2? #當(dāng)前活動的連接數(shù)
accepts? 2? ? ? ???? #當(dāng)前的總連接數(shù)TCP
handled???????????? #成功的連接數(shù)TCP
requests? ? ? ? ? ? #總的http請求數(shù)
注意:如果使用systemctl restart nginx 會清空連接數(shù)
3、Nginx訪問控制
基于IP的訪問控制http_access_modul
基于用戶登錄認(rèn)證http_auth_basic_modul
1)Nginx基于IP的訪問控制
//允許配置語法
Syntax:allow address | CIDR | unix: | all;
Default: -
Context: http,server,location,limit——except
//拒絕配置語法
Syntax:deny address | CIDR | unix: | all;
Default: -
Context: http,server,location,limit——except
Examples:
示例1、拒絕192.168.1.218訪問download,其他全部允許
[root@web02 conf.d]# cat nfzl.conf
server {
? ? ? ? listen 80;
? ? ? ? server_name 192.168.1.174;
? ? ? ? location / {
? ? ? ? ? ? ? ? root /code;
? ? ? ? ? ? ? ? index index.html;
? ? ? ? }
? ? ? ? location /nginx_status {
? ? ? ? ? ? ? ? stub_status;
? ? ? ? ? ? ? ? access_log off;
? ? ? ? ? ? ? ? allow 192.168.1.218;
? ? ? ? ? ? ? ? deny all;
? ? ? ? }
? ? ? ? location /download {
? ? ? ? ? ? ? ? root /code;
? ? ? ? ? ? ? ? autoindex on;
? ? ? ? ? ? ? ? charset utf-8,gbk;
? ? ? ? ? ? ? ? autoindex_exact_size off;
? ? ? ? ? ? ? ? autoindex_localtime on;
? ? ? ? ? ? ? ? deny 192.168.1.218;
? ? ? ? ? ? ? ? allow all;
? ? ? ? }
}
示例2、只允許192.168.1.218訪問nginx_status,其他全部拒絕
[root@web02 conf.d]# cat nfzl.conf
server {
? ? ? ? listen 80;
? ? ? ? server_name 192.168.1.174;
? ? ? ? location / {
? ? ? ? ? ? ? ? root /code;
? ? ? ? ? ? ? ? index index.html;
? ? ? ? }
? ? ? ? location /nginx_status {
? ? ? ? ? ? ? ? stub_status;
? ? ? ? ? ? ? ? access_log off;
? ? ? ? ? ? ? ? allow 192.168.1.218;
? ? ? ? ? ? ? ? deny all;
? ? ? ? }
? ? ? ? location /download {
? ? ? ? ? ? ? ? root /code;
? ? ? ? ? ? ? ? autoindex on;
? ? ? ? ? ? ? ? charset utf-8,gbk;
? ? ? ? ? ? ? ? autoindex_exact_size off;
? ? ? ? ? ? ? ? autoindex_localtime on;
? ? ? ? ? ? ? ? deny 192.168.1.218;
? ? ? ? ? ? ? ? allow all;
? ? ? ? }
}
2) 基于用戶登錄認(rèn)證http_auth_basic_modul
//配置語法
Syntax:auth_basic string| off
Default: auth_basic off;
Context: http,server,location,limit——except
//用戶密碼記錄配置文件
Syntax:auth_basic_user_file file;
Default: -
Context: http,server,location,limit——except
//需要安裝依賴組件
[root@web02 ~]#yum install httpd-tools
[root@web02 ~]# htpasswd -b -c /etc/nginx/auth_conf superman talent? #用戶名superman,密碼talent
//可在httpd,server,location下添加如下信息
auth_basic "Auth access Download Input your Passwd!";
auth_basic_user_file /etc/nginx/auth_conf;
Examples:
[root@web02 conf.d]# cat nfzl.conf
server {
? ? ? ? listen 80;
? ? ? ? server_name 192.168.1.174;
? ? ? ? location / {
? ? ? ? ? ? ? ? root /code;
? ? ? ? ? ? ? ? index index.html;
? ? ? ? }
? ? ? ? location /nginx_status {
? ? ? ? ? ? ? ? stub_status;
? ? ? ? ? ? ? ? access_log off;
? ? ? ? ? ? ? ? allow 192.168.1.218;
? ? ? ? ? ? ? ? deny all;
? ? ? ? }
? ? ? ? location /download {
? ? ? ? ? ? ? ? root /code;
? ? ? ? ? ? ? ? autoindex on;
? ? ? ? ? ? ? ? charset utf-8,gbk;
? ? ? ? ? ? ? ? autoindex_exact_size off;
? ? ? ? ? ? ? ? autoindex_localtime on;
? ? ? ? ? ? ? ? auth_basic "Auth access Download Input your Passwd!";
? ? ? ? ? ? ? ? auth_basic_user_file /etc/nginx/auth_conf;
? ? ? ? }
}

4、Nginx訪問限制
經(jīng)常會遇到這種情況,服務(wù)器流量異常,負(fù)載過大等等。對于大流量惡意的攻擊訪問,會帶來帶寬的浪費,服務(wù)器壓力,影響業(yè)務(wù),往往考慮對同一個IP的連接數(shù),并發(fā)數(shù)進(jìn)行限制
ngx_http_limit_conn_module 模塊可以根據(jù)定義的KEY來限制每個鍵值的連接數(shù),如同一個IP來源的連接數(shù)
limit_conn_module 連接頻率限制
limit_req_module? 請求頻率限制
http協(xié)議的連接與請求
HTTP是建立在TCP,在完成http請求需要先建立TCP三次握手(稱為TCP連接),在連接的基礎(chǔ)上HTTP請求.
HTTP請求建立在一次TCP連接基礎(chǔ)上,一次TCP請求至少產(chǎn)生一次HTTP連接。
1)nginx連接限制配置實戰(zhàn)
//nginx連接限制語法
Syntax:limit_conn_zone key zone=name:size;
Default: -
Context: http
Syntax:limit_conn zone number;
Default: -
Context: http,server,location
//nginx連接限制實戰(zhàn)
http {
//http段配置連接限制,同一時刻只允許一個客戶端IP連接
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
...
server {
...
location / {
//同一時刻只允許一個客戶端IP連接
limit_conn conn_zone 1;
}
}
}
變量:
$binary_remote_addr? #變量的長度是固定的4個字節(jié)
$remote_addr #變量的長度7-15字節(jié)
建議使用$binary_remote_addr,因一個IP地址=32bit=4字節(jié)
10M=10*1024K=10*1024*1024B/4? 可存儲IP數(shù)量
//示例:http段配置連接限制,同一時刻只允許一個客戶端IP連接
[root@web02 conf.d]# cat nfzl.conf
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
server {
listen 80;
server_name 192.168.1.174;
location / {
root /code;
index index.html;
limit_conn conn_zone 1;
}
location /nginx_status {
stub_status;
access_log off;
allow 192.168.1.218;
deny all;
}
location /download {
root /code;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "Auth access Download Input your Passwd!";
auth_basic_user_file /etc/nginx/auth_conf;
}
}
//使用ab工具進(jìn)行壓力測試
[root@web02 ~]#yum install httpd-tools
[root@web02 ~]#ab -n 50 -c 20 http://127.0.0.1/index.html
2)nginx請求限制配置實戰(zhàn)
//nginx請求限制語法
Syntax:limit_req_zone key zone=name:size rate=rate;
Default: -
Context: http
Syntax:limit_conn zone number [burst=number] [nodelay];
Default: -
Context: http,server,location
//nginx請求限制實戰(zhàn)
http {
//http段配置請求限制,rate限制速率,限制一秒鐘最多一個IP請求
limit_req_zone $binary_remote_addr zone=conn_zone:10m rate=1r/s;
...
server {
...
location / {
//1r/s只接收一個請求,其余請求拒絕處理并返回錯誤碼給客戶端
limit_req zone=conn_zone;
//請求超過1r/s,剩下的將被延遲處理,請求數(shù)超過burst定義的數(shù)量,多余的請求返回503錯誤
#limit_req zone=conn_zone burst=3 nodelay;
}
}
}
//示例:http段配置請求限制,限制一秒鐘最多一個IP請求
[root@web02 conf.d]# cat nfzl.conf
#limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
limit_req_zone $binary_remote_addr zone=conn_zone:10m rate=1r/s;
server {
listen 80;
server_name nfzl.com;
location / {
root /code;
index index.html;
# limit_conn conn_zone 1;
limit_req zone=conn_zone burst=8 nodelay;
}
location /nginx_status {
stub_status;
access_log off;
allow 192.168.1.218;
deny all;
}
location /download {
root /code;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "Auth access Download Input your Passwd!";
auth_basic_user_file /etc/nginx/auth_conf;
}
}
//使用ab工具進(jìn)行壓力測試
[root@web02 ~]#yum install httpd-tools
[root@web02 ~]#ab -n 50 -c 20 http://127.0.0.1/index.html
//思考:Nginx連接限制沒有請求限制有效嗎?
我們前面說過,多個請求可以建立一次的TCP連接之上,那么我們對請求的精度限制,當(dāng)然比對一個連接的限制會更加有效
因為同一時刻只允許一個連接請求進(jìn)入。但是同一時刻多個請求可以通過一個連接進(jìn)入。所以請求限制才是比較優(yōu)的解決方案。
5、Nginx日志配置
//配置語法
Syntax:log_format name [escape=default|json] string ...;
Default: log_format combined "...";
Context:http
//默認(rèn)Nginx定義日志語法
? ? log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '
? ? ? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
? ? ? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"';
//Nginx日志格式允許包含的變量
? 參數(shù) ? ? 說明
$remote_addr 記錄客戶端地址
$remote_user 記錄客戶端用戶
$time_local 記錄訪問時間和時區(qū)
$time_iso8601 記錄ISO8601標(biāo)準(zhǔn)格式下的本地時間
$request 記錄請求的方法以及請求的HTTP協(xié)議
$status ? ? ? ? ? ? ? ? 記錄HTTP請求狀態(tài)
$body_bytes_sent ? ? 發(fā)送給客戶端文件內(nèi)容大小
$bytes_sent ? ? ? ? 發(fā)送給客戶端的總字節(jié)數(shù)
$msec 日志寫入世間。單位為秒,精度是毫秒
$http_referer ? ? ? ? 記錄從那個頁面鏈接訪問過來的
$http_user_agent ? ? 記錄用戶終端瀏覽器等信息
$http_x_forwarded_for? ? 記錄客戶端IP地址
$request_length ? ? 請求的長度(包括請求行,請求頭和請求正文)
$request_time ? ? ? ? 整個請求的總時間
//access_log語法
syntax:access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default:access_log logs/access.log combined;
Context: http,server,location,if in location,limit_except
Example:
server {
...
access_log /var/log/nginx/www_log.log main;
...
}
//示例 修改日志格式$request_time(請求時間),$time_iso8601(修改日志顯示時間)
? ? log_format? main? '$remote_addr - $remote_user [$time_iso8601] $request_time "$request" '
? ? ? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
? ? ? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"';
? ? access_log? /var/log/nginx/access.log? main;

6、Nginx虛擬站點
所謂虛擬主機(jī),及在一臺服務(wù)器上配置多個網(wǎng)站
如:公司主頁、博客、論壇看是三個網(wǎng)站,實則可以運行在一臺服務(wù)器上
需求:公司需要有三個站點,分別是www,bbs,blog
1)創(chuàng)建web站點目錄
[root@web02 ~]# mkdir -p /code/{www,bbs,blog}
[root@web02 ~]# echo www > /code/www/index.html
[root@web02 ~]# echo bbs > /code/bbs/index.html
[root@web02 ~]# echo blog > /code/blog/index.html
示例一:基于IP的虛擬主機(jī)
[root@web02 ~]#vim /etc/nginx/conf.d/www.conf
server {
listen 192.168.1.174:80;
server_name www.nfzl.com;
location / {
root /code/www;
index index.html;
}
}
[root@web02 ~]#vim /etc/nginx/conf.d/bbs.conf
server {
listen 192.168.1.175:80;
server_name bbs.nfzl.com;
location / {
root /code/bbs;
index index.html;
}
}
[root@web02 ~]#vim /etc/nginx/conf.d/blog.conf
server {
listen 192.168.1.176:80;
server_name blog.nfzl.com;
location / {
root /code/blog;
index index.html;
}
}
示例二:基于端口的虛擬主機(jī)
[root@web02 ~]#vim /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.nfzl.com;
location / {
root /code/www;
index index.html;
}
}
[root@web02 ~]#vim /etc/nginx/conf.d/bbs.conf
server {
listen 81;
server_name bbs.nfzl.com;
location / {
root /code/bbs;
index index.html;
}
}
[root@web02 ~]#vim /etc/nginx/conf.d/blog.conf
server {
listen 82;
server_name blog.nfzl.com;
location / {
root /code/blog;
index index.html;
}
}
示例三:基于域名的虛擬主機(jī)
[root@web02 ~]#vim /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.nfzl.com;
location / {
root /code/www;
index index.html;
}
}
[root@web02 ~]#vim /etc/nginx/conf.d/bbs.conf
server {
listen 80;
server_name bbs.nfzl.com;
location / {
root /code/bbs;
index index.html;
}
}
[root@web02 ~]#vim /etc/nginx/conf.d/blog.conf
server {
listen 80;
server_name blog.nfzl.com;
location / {
root /code/blog;
index index.html;
}
}
2)語法測試并重啟nginx
[root@web02 conf.d]# nginx -t
[root@web02 conf.d]# systemctl restart nginx
3)寫hosts解析
192.168.1.174 www.nfzl.com bbs.nfzl.com blog.nfzl.com
4)訪問測試
http://www.nfzl.com

http://bbs.nfzl.com

http://blog.nfzl.com

5) 增加需求:實現(xiàn)每個站點日志獨立
[root@web02 conf.d]#mkdir -p /code/log
[root@web02 conf.d]# vim www.conf
access_log? /code/log/www.nfzl.com_access.log? main;
server {
? ? ? ? listen 80;
? ? ? ? server_name bbs.nfzl.com;
? ? ? ? access_log? /code/log/bbs.nfzl.com_access.log? main;
? ? ? ? location / {
? ? ? ? ? ? ? ? root /code/bbs;
? ? ? ? ? ? ? ? index index.html;
? ? ? ? }
}
~? ?
[root@web02 conf.d]#vim bbs.conf
access_log? /code/log/bbs.nfzl.com_access.log? main;
[root@web02 conf.d]#vim blog.conf
access_log? /code/log/blog.nfzl.com_access.log? main;



7、Nginx Location
使用Nginx Location可以控制訪問網(wǎng)站的路徑,但是一個server可以有多個location配置,多個location的優(yōu)先級該如何區(qū)分
1)Location語法示例
location [=|~|~*|^~|!~*|!~|/] /uri/ { … }
2)Location語法優(yōu)先級排序
匹配符? ? ? ? 匹配規(guī)則? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 優(yōu)先級
=? ???????????? 精確匹配? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1
^~? ? ? ? ? ? ? 以某個字符串開頭? ? ? ? ? ? ? ? ? ? ? 2
~ ???????? ???? 區(qū)分大小寫的正則匹配? ? ? ? ? ? ? ? ?3
~* ???????????? 不區(qū)分大小寫的正則匹配? ? ? ? ? ? ?4
!~? ? ? ? ? ?區(qū)分大小寫不匹配的正則? ????????? ? 5
!~*? ? ? ? ?不區(qū)分大小寫不匹配的正則? ? ? ? ? ?6
/ ???????????? 通用匹配,任何請求都會匹配到? ? ?7
3)配置網(wǎng)站驗證location優(yōu)先級
[root@web02 conf.d]# cat test.conf
server {
listen 80;
server_name www.nfzl.com;
access_log? /code/log/www.nfzl.com_access.log? main;
location / {
default_type text/html;
return 200 "location /";
}
? ? ? ? location =/ {
? ? ? ? ? ? ? ? default_type text/html;
? ? ? ? ? ? ? ? return 200 "location =/";
? ? ? ? }
? ? ? ? location ~ / {
? ? ? ? ? ? ? ? default_type text/html;
? ? ? ? ? ? ? ? return 200 "location ~/";
? ? ? ? }
? ? ? # location ^~ / {
? ? ? #? ? ? ? default_type text/html;
? ? ? #? ? ? ? return 200 "location ^~";
? ? ? # }
}
4)location應(yīng)用場景
#通用匹配,任何請求都會匹配到
location / {
}
#嚴(yán)格區(qū)分大小寫,匹配以.php結(jié)尾的都走這個location
location ~ \.php$ {
fastcge_pass http://127.0.0.1:9000;
}
#嚴(yán)格區(qū)分大小寫,匹配以.jsp結(jié)尾的都走這個location
location ~ \.jsp$ {
fastcge_pass http://127.0.0.1:8080;
}
#不區(qū)分大小寫,只要用戶訪問.jpg,gif,png,js,css都走這條location
location ~* .*\.(jpg|gif|png|js|css)$ {
rewrite (.*)http://cdn.nfzl.com$request_uri;
}
#不區(qū)分大小寫匹配
location ~* "\.(sql|bak|tar.gz|tgz|.git)$" {
default_type text/htm;
return 403 "啟動訪問控制成功";
}