1、nginx開啟目錄瀏覽 提供下載功能
默認情況下,網(wǎng)站返回index指定的主頁,但如果該網(wǎng)站不存在主頁,則將請求交給autoindex模塊
如果開啟autoindex模塊,則提供一個下載的頁面, 如果沒有開啟autoindex 則會報錯 403
----------------------------------------------------------
#1、配置
[root@web01 centos]# cat /etc/nginx/conf.d/mirror.yangyang.com.conf
server {
listen 80;
server_name mirror.yangyang.com;
charset utf8; #字符集
location / {
root /code;
index index.html;
autoindex on; #開啟目錄索引,提供下載
autoindex_exact_size off; #以人性化方式顯示大小
autoindex_localtime on; #與本地時間保持一致
}
}
#2、在code目錄下創(chuàng)建目錄
[root@web01 ~]# cd /code/
[root@web01 code]# ll
total 0
drwxr-xr-x 2 root root 6 Sep 17 10:16 centos
drwxr-xr-x 2 root root 6 Sep 17 10:16 nginx
drwxr-xr-x 2 root root 6 Sep 17 10:16 server
#3、檢測語法
[root@web01 centos]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#4、重啟
[root@web01 ~]# systemctl restart nginx

圖片.png
2、ginx實現(xiàn)訪問控制,基于來源ip控制、基于用戶密碼控制
實例一:允許特定的ip訪問,其他全部拒絕
#1、修改配置文件
[root@web01 ~]# cat /etc/nginx/conf.d/mirror.yangyang.com.conf
server{
listen 80;
server_name mirror.yangyang.com;
charset utf8; #字符集
autoindex on; #開啟目錄索引,提供下載
autoindex_exact_size off; #以人性化的方式顯示大小
autoindex_localtime on; #與本地時間保持一致
location / {
root /code;
index index.html;
}
location /centos {
allow 10.0.0.1/32
deny all;
}
#2、測試語法
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#3、重啟
[root@web01 ~]# systemctl restart nginx

圖片.png
實例二:拒絕特定的ip訪問(10.0.0.100),其他全部允許
#1、修改配置文件
[root@web01 ~]# cat /etc/nginx/conf.d/mirror.yangyang.com.conf
server{
listen 80;
server_name mirror.yangyang.com;
root /code;
charset utf8; #字符集
autoindex on; #開啟目錄索引,提供下載
autoindex_exact_size off; #以人性化的方式顯示大小
autoindex_localtime on; #與本地時間保持一致
location / {
root /code;
index index.html;
location /centos {
allow 10.0.0.100/32
deny all;
}
}
#2、檢測語法
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#3、重啟
[root@web01 ~]# systemctl restart nginx
注意:deny和allow的順序是有影響的,默認情況下,從第一條規(guī)則進行匹配,如果匹配成功,則不繼續(xù)匹配下面的內(nèi)容,如果匹配不成功,則繼續(xù)往下尋找能匹配成功的內(nèi)容
最后訪問域名

圖片.png
實例三:基于用戶和密碼的方式限制 ( 針對人 ) ( 針對運維人員 )
#1、安裝密碼生成工具
[root@web01 ~]# yum install httpd-tools -y
#2、生成密碼
[root@web01 ~]# htpasswd -b -c /etc/nginx/auth_conf yangyang 123456
#3、修改nginx配置文件
[root@web01 ~]# cat /etc/nginx/conf.d/mirror.yangyang.com.conf
server{
listen 80;
server_name mirror.yangyang.com;
root /code;
charset utf8; #字符集
autoindex on; #開啟目錄索引,提供下載
autoindex_exact_size off; #以人性化的方式顯示大小
autoindex_localtime on; #與本地時間保持一致
location / {
root /code;
index index.html;
}
location /centos {
auth_basic "hello test";
auth_basic_user_file "/etc/nginx/auth_conf";
}
}
#4、檢測語法
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#5、重啟
[root@web01 ~]# systemctl restart nginx
最后訪問域名

圖片.png
3、nginx實現(xiàn)限速( 下載限速 縣直單位時間內(nèi)的http請求 連接限制 )
#1、請求頻率限制 http
[root@web01 ~]# vim /etc/nginx/conf.d/mirror.yangyang.com.conf
limit_req_zone $binary_remote_addr zone=req_od:10m rate=1r/s;
server{
listen 80;
server_name mirror.yangyang.com;
root /code;
charset utf8; #字符集
autoindex on; #開啟目錄索引,提供下載
autoindex_exact_size off; #以人性化的方式顯示大小
autoindex_localtime on; #與本地時間保持一致
limit_req zone=req_od burst=3 nodelay;
location / {
root /code;
index index.html;
}
location /centos {
auth_basic "hello test";
auth_basic_user_file"/etc/nginx/auth_conf";
}
}
#2、檢測語法
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#3、重啟
[root@web01 ~]# systemctl restart nginx

圖片.png
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
- 第一個參數(shù):$binary_remote_addr表示通過這個標識來做限制,限制同一客戶端ip地址。
- 第二個參數(shù):zone=req_one:10m表示生成一個大小為10M,名為req_one的內(nèi)存區(qū)域,用來存儲訪問的頻次信息。
- 第三個參數(shù):rate=1r/s表示允許相同標識的客戶端的訪問頻次,這里限制的是每秒1次,還可以30r/m。
limit_req zone=req_one burst=3 nodelay;
- 第一個參數(shù):zone=req_one 設(shè)置使用哪個配置區(qū)域來做限制,與上面limit_req_zone 里的name對應(yīng)。
- 第二個參數(shù):burst=3,設(shè)置一個大小為3的緩沖區(qū),當有大量請求過來時,超過了訪問頻次限制的請求可以先放到這個緩沖區(qū)內(nèi)。
- 第三個參數(shù):nodelay,超過訪問頻次并且緩沖區(qū)也滿了的時候,則會返回503,如果沒有設(shè)置,則所有請求會等待排隊。
4、連接限制
#1、修改配置
[root@web01 ~]# vim /etc/nginx/conf.d/mirror.yangyang.com.conf
limit_req_zone $binary_remote_addr zone=req_od:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=conn_od:10m;
server{
listen 80;
server_name mirror.yangyang.com;
root /code;
charset utf8; #字符集
autoindex on; #開啟目錄索引,提供下載
autoindex_exact_size off; #以人性化的方式顯示
大小
autoindex_localtime on; #與本地時間保持一致
limit_req zone=req_od burst=5 nodelay;
limit_conn conn_od 1;
location / {
root /code;
index index.html;
}
location /centos {
auth_basic "hello test";
auth_basic_user_file "/etc/nginx/auth_conf";
}
}
#2、檢查語法
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#3、重啟
[root@web01 ~]# systemctl restart nginx
5、速率限制
#1、配置
[root@web01 ~]# vim /etc/nginx/conf.d/mirror.yangyang.com.conf
limit_req_zone $binary_remote_addr zone=req_od:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=conn_od:10m;
server{
listen 80;
server_name mirror.yangyang.com;
root /code;
charset utf8; #字符集
autoindex on; #開啟目錄索引,提
供下載
autoindex_exact_size off; #>以人性化的方式顯示大小
autoindex_localtime on; #>與本地時間保持一致
limit_conn conn_od 1;
limit_rate_after 100m;
limit_rate 100k;
location / {
root /code;
index index.html;
}
location /centos {
auth_basic "hello test";
auth_basic_user_file "/etc/nginx/auth_conf";
}
}
#2、檢測語法
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#3、重啟
[root@web01 ~]# systemctl restart nginx

圖片.png
6、綜合案例、限制web服務(wù)器請求數(shù)處理為1秒一個,觸發(fā)值為5、限制并發(fā)連接數(shù)為1、限制下載速度為100k、如果超過下載次數(shù),則返回提示 "請充值會員"
#1、配置
[root@web01 ~]# vim /etc/nginx/conf.d/mirror.yangyang.com.conf
limit_req_zone $binary_remote_addr zone=req_od:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=conn_od:10m;
server{
listen 80;
server_name mirror.yangyang.com;
root /code;
charset utf8; #字符集
autoindex on; #開啟目錄索引,提供下載
autoindex_exact_size off; #以人性化的方
式顯示大小
autoindex_localtime on; #與本地時間保
持一致
limit_req zone=req_od burst=5 nodelay;
limit_conn conn_od 1;
limit_rate_after 100m;
limit_rate 100k;
error_page 503 @errpage;
location @errpage {
default_type text/html;
return 200 '提示:你以重復(fù)下載,請充值會員';
}
location / {
root /code;
index index.html;
}
location /nginx_status {
stub_status;
}
}
#2、檢測語法
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#3、重啟
[root@web01 ~]# systemctl restart nginx

圖片.png
7、nginx狀態(tài)指標,俗稱7種狀態(tài) 監(jiān)控Nginx
#1、配置
[root@web01 ~]# cd /etc/nginx/conf/
[root@web01 conf.d]# cat mirror.yangyang.com.conf
limit_req_zone $binary_remote_addr zone=req_od:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=conn_od:10m;
server{
listen 80;
server_name mirror.yangyang.com;
root /code;
charset utf8; #字符集
autoindex on; #開啟目錄索引,提供下載
autoindex_exact_size off; #以人性化的方式顯示大小
autoindex_localtime on; #與本地時間保持一致
limit_req zone=req_od burst=5 nodelay;
limit_conn conn_od 1;
limit_rate_after 100m;
limit_rate 100k;
location /nginx_status {
stub_status;
}
#2、檢測,重啟服務(wù)
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx

圖片.png
Active connections 活躍的連接數(shù)
accepts 總的TCP連接數(shù)
handled 成功握手的TCP連接數(shù)
accepts - handled 失敗的TCP連接數(shù)
requests 總的請求數(shù)
Reading 讀取到請求頭的數(shù)量。
Writing 響應(yīng)客戶端到的數(shù)量。
Waiting 客戶端與服務(wù)端的連接數(shù)
8、編寫實例:
#1、配置
[root@web01 conf.d]# cat location.yangyang.com.conf
server {
listen 80;
server_name location.oldxu.com;
location = / {
default_type text/html;
return 200 'location = /';
}
location / {
default_type text/html;
return 200 'location /';
}
location /documents/ {
default_type text/html;
return 200 'location /documents/';
}
location ^~ /images/ {
default_type text/html;
return 200 'location ^~ /images/';
}
location ~* \.(gif|jpg|jpeg)$ {
default_type text/html;
return 200 'location ~* \.(gif|jpg|jpeg)';
}
}
#2、測試,重啟
[root@web01 conf.d]# curl location.yangyang.com
location =/
[root@web01 conf.d]# curl location.yangyang.com/inde.html
location /
----------------------------------------------------------
優(yōu)先級:
匹配符 匹配規(guī)則 優(yōu)先級
= 精確匹配 1
^~ 以某個字符串開頭 2
~ 區(qū)分大小寫的正則匹配 3
~* 不區(qū)分大小寫的正則匹配 4
/ 通用匹配,任何請求都會匹配到 5、
----------------------------------------------------------
[root@web01 conf.d]# cat location2.oldxu.com.conf
server {
listen 80;
server_name location2.oldxu.com;
# 通用匹配,任何請求都會匹配到
location / {
root html;
index index.html;
}
# 精準匹配,必須請求的uri是/nginx_status
location = /nginx_status {
stub_status;
}
# 嚴格區(qū)分大小寫,匹配以.php結(jié)尾的都走這個location
location ~ \.php$ {
default_type text/html;
return 200 'php訪問成功';
}
# 嚴格區(qū)分大小寫,匹配以.jsp結(jié)尾的都走這個location
location ~ \.jsp$ {
default_type text/html;
return 200 'jsp訪問成功';
}
# 不區(qū)分大小寫匹配,只要用戶訪問.jpg,gif,png,js,css 都走這條location
location ~* \.(jpg|gif|png|js|css)$ {
return 403;
}
# 不區(qū)分大小寫匹配
location ~* \.(sql|bak|tgz|tar.gz|.git)$ {
deny all;
}
}
location @name { ... }
@”前綴定義命名位置。這樣的位置不用于常規(guī)請求處理,而是用于請求重定向.
server {
listen 80;
mirror.oldxu.com;
root /code;
location / {
index index.html;
}
#如果出現(xiàn)異常,則重新定向到@error_404這個location上
error_page 404 @error_404;
location @error_404 {
default_type text/html;
return 200 '不要瞎訪問,走丟了';
}
}

圖片.png