日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

nginx 模塊

 新進(jìn)小設(shè)計(jì) 2020-06-25

8.nginx開啟目錄瀏覽 提供下載功能

默認(rèn)情況下,網(wǎng)站返回index指定的主頁,但如果該網(wǎng)站不存在主頁,則將請求交給autoindex模塊
##### 如果開啟autoindex模塊,則提供一個下載的頁面, 如果沒有開啟autoindex 則會報(bào)錯 403

[root@web01 centos]# cat /etc/nginx/conf.d/mirror..conf
server {
	listen 80;
	server_name mirror.;
	charset utf8;					#字符集



location / {
	root /code;
	index index.html;
	autoindex on;				#開啟目錄索引,提供下載
	autoindex_exact_size off;	#以人性化方式顯示大小
	autoindex_localtime on;		#與本地時間保持一致
}

}


9.nginx實(shí)現(xiàn)訪問控制,基于來源IP控制、基于用戶名密碼控制

示例一.允許特定的IP訪問,其他全部拒絕
10.0.0.1    可以正常訪問  /centos
10.0.0.100  僅能訪問      /ubuntu   /redhat   

[root@web01 ~]# cat /etc/nginx/conf.d/mirror..conf 
server {
	listen 80;
	server_name mirror.;
	root /code;
	charset utf8;
	autoindex on;				#開啟目錄索引,提供下載
	autoindex_exact_size off;	#以人性化方式顯示大小
	autoindex_localtime on;		#與本地時間保持一致

location / {
	index index.html;
}

location /centos {
	allow 10.0.0.1/32;
	deny all;
}

}
示例二.拒絕特定的IP訪問(10.0.0.100),其他全部允許
[root@web01 ~]# cat /etc/nginx/conf.d/mirror..conf 
server {
	listen 80;
	server_name mirror.;
	root /code;
	charset utf8;
		autoindex on;			#開啟目錄索引,提供下載
		autoindex_exact_size off;	#以人性化方式顯示大小
		autoindex_localtime on;		#與本地時間保持一致

location / {
	index index.html;
}

location /centos {
	deny 10.0.0.100/32;
	allow all;
}

}

注意:deny和allow的順序是有影響的
默認(rèn)情況下,從第一條規(guī)則進(jìn)行匹配
如果匹配成功,則不繼續(xù)匹配下面的內(nèi)容。
如果匹配不成功,則繼續(xù)往下尋找能匹配成功的內(nèi)容。
示例二.基于用戶名和密碼的方式限制 ( 針對個人 ) ( 針對運(yùn)維人員 )
1.安裝密碼生成工具
[root@web01 ~]# yum install httpd-tools -y

2.生成密碼
[root@web01 ~]# htpasswd -b -c /etc/nginx/auth_conf  oldxu 123456

3.修改nginx配置文件
[root@web01 ~]# cat  /etc/nginx/conf.d/mirror..conf 
server {
	listen 80;
	server_name mirror.;
	root /code;
	charset utf8;
		autoindex on;			#開啟目錄索引,提供下載
		autoindex_exact_size off;	#以人性化方式顯示大小
		autoindex_localtime on;		#與本地時間保持一致

location / {
	index index.html;
}

location /centos {
	auth_basic "hello test";
	auth_basic_user_file "/etc/nginx/auth_conf";
}

}

10.nginx實(shí)現(xiàn)限速 ( 下載限速 限制單位時間內(nèi)的Http請求 連接限制 )

1.請求頻率限制 Http
[root@web01 ~]# cat /etc/nginx/conf.d/mirror..conf 
limit_req_zone $binary_remote_addr zone=req_od:10m rate=1r/s;

server {
	listen 80;
	server_name mirror.;
	root /code;
	charset utf8;
	autoindex on;				#開啟目錄索引,提供下載
	autoindex_exact_size off;	#以人性化方式顯示大小
	autoindex_localtime on;		#與本地時間保持一致
	

limit_req zone=req_od burst=3 nodelay;

location / {
	index index.html;
}

location /centos {
	auth_basic "hello test";
	auth_basic_user_file "/etc/nginx/auth_conf";
}

}

limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
#第一個參數(shù):$binary_remote_addr表示通過這個標(biāo)識來做限制,限制同一客戶端ip地址。
#第二個參數(shù):zone=req_one:10m表示生成一個大小為10M,名為req_one的內(nèi)存區(qū)域,用來存儲訪問的頻次信息。
#第三個參數(shù):rate=1r/s表示允許相同標(biāo)識的客戶端的訪問頻次,這里限制的是每秒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ū),當(dāng)有大量請求過來時,超過了訪問頻次限制的請求可以先放到這個緩沖區(qū)內(nèi)。
#第三個參數(shù):nodelay,超過訪問頻次并且緩沖區(qū)也滿了的時候,則會返回503,如果沒有設(shè)置,則所有請求會等待排隊(duì)。

2.連接限制

[root@web01 ~]# cat /etc/nginx/conf.d/mirror..conf 
limit_conn_zone $binary_remote_addr zone=conn_od:10m;

server {
	listen 80;
	server_name mirror.;
	root /code;
	charset utf8;
	autoindex on;			#開啟目錄索引,提供下載
	autoindex_exact_size off;	#以人性化方式顯示大小
	autoindex_localtime on;		#與本地時間保持一致
	limit_conn conn_od 2;

location / {
	index index.html;
}

location /centos {
	auth_basic "hello test";
	auth_basic_user_file "/etc/nginx/auth_conf";
}

}

3.速率限制

[root@web01 ~]# cat /etc/nginx/conf.d/mirror..conf 
limit_conn_zone $binary_remote_addr zone=conn_od:10m;

server {
	listen 80;
	server_name mirror.;
	root /code;
	charset utf8;
		autoindex on;				#開啟目錄索引,提供下載
		autoindex_exact_size off;	#以人性化方式顯示大小
		autoindex_localtime on;		#與本地時間保持一致
	limit_conn conn_od 2;
	limit_rate_after 100m;
	limit_rate 100k;

location / {
	index index.html;
}

location /centos {
	auth_basic "hello test";
	auth_basic_user_file "/etc/nginx/auth_conf";
}

}

6.綜合案例、限制web服務(wù)器請求數(shù)處理為1秒一個,觸發(fā)值為5、限制并發(fā)連接數(shù)為1、限制下載速度為100k

如果超過下載次數(shù),則返回提示 "請充值會員"

[root@web01 conf.d]# cat mirror..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.;
	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 ' Oldxu提示--->請充值會員';
}
location / {
	index index.html;
}

}

11.nginx狀態(tài)指標(biāo),俗稱7種狀態(tài) 監(jiān)控Nginx

location /nginx_status {
	stub_status;
}

Active connections: 2 				
server accepts handled requests
			2     2      17 	
Reading: 0 Writing: 1 Waiting: 1

Active connections		活躍的連接數(shù)
accepts					總的TCP連接數(shù)
handled					成功握手的TCP連接數(shù)
accepts -	handled		失敗的TCP連接數(shù)
requests				總的請求數(shù)
Reading					讀取到請求頭的數(shù)量。
Writing					響應(yīng)客戶端到的數(shù)量。
Waiting					客戶端與服務(wù)端的連接數(shù)

vim /etc/nginx/nginx.conf
	keepalive_timeout 65;		#長連接超時時間
	keepalive_timeout 0;		#模擬短連接效果

12.nginx location匹配、匹配優(yōu)先級

location是用來控制用戶請求的uri路徑的
語法:
location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... } #用戶內(nèi)部重定向
=  精確匹配
~  正則匹配
~* 正則匹配(忽略大小寫)
^~ 以字符串方式匹配
/  通用匹配

編寫實(shí)例:

[root@web01 conf.d]# cat location..conf 
server {
	listen 80;
	server_name location.;

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)';
}

}

測試:
	1.請求 http://location./ 						會被  location =/   		匹配
	2.請求 http://location./index.html				會被  location /			匹配
	3.請求 http://location./documents/test.html	會被  location /documents/	匹配
	4.請求 http://location./images/test.gif		會被  location ^~ /images/	匹配
	5.請求 http://location./documents/1.jpg		會被  location ~* \.(gif|jpg|jpeg)$	匹配
優(yōu)先級:
匹配符	匹配規(guī)則						優(yōu)先級
=		精確匹配					 1
^~		以某個字符串開頭			   2
~		區(qū)分大小寫的正則匹配			  3
~*		不區(qū)分大小寫的正則匹配			 4
/		通用匹配,任何請求都會匹配到     5


[root@web01 conf.d]# cat location2..conf 
server {
	listen 80;
	server_name location2.;

通用匹配,任何請求都會匹配到

location / {
	root html;
	index index.html;
}

精準(zhǔn)匹配,必須請求的uri是/nginx_status

location = /nginx_status {
	stub_status;
}

嚴(yán)格區(qū)分大小寫,匹配以.php結(jié)尾的都走這個location

location ~ \.php$ {
	default_type text/html;
	return 200 'php訪問成功';
}

嚴(yán)格區(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.;
	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 '你可能是瞎訪問,走丟了。但是不要以為瞎訪問就能找到Bug.....';
?	}
}

13.nginx 日志、訪問日志、錯誤日志、日志過濾、日志切割

統(tǒng)計(jì) 分析  那個 uri請求的次數(shù)最多
錯誤日志     用來排除故障
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

log_format  ttt   '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent';

access_log /var/log/nginx/access.log main;

$remote_addr			# 來源的客戶端IP			 		(   user--->web  )
$remote_user			# 登錄的用戶名 Http基本認(rèn)證才會有 -
[$time_local]			# 時間
$request				# 請求uri 請求的方法 請求的協(xié)議
$status					# 狀態(tài)碼
$body_bytes_sent		# 發(fā)送的字節(jié)
$http_referer			# 從那個url過來的
$http_user_agent		# 來源的設(shè)備
$http_x_forwarded_for	# 記錄真實(shí)的客戶端IP  				(   user--->proxy--->web  )



日志過濾
location = /favicon.ico {
        access_log off;
        access_log /dev/null;
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多