需要的服務(wù)器
角色 | 外網(wǎng)ip | 內(nèi)網(wǎng)ip | 主機名 |
---|---|---|---|
web | eth0:10.0.0.7 | eth1:172.16.1.7 | web01 |
autoindex 目錄索引
[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf
server {
listen 80;
server_name mirror.oldxu.com;
charset utf8;
location / {
root /code;
index index.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format html;
}
}
[root@web01 ~]# mkdir /code/{centos,ubuntu}
[root@web01 ~]# cat /code/index.html
<h1> Mirrors Oldxu.com </h1>
<ul><li><a target="_blank">centos系統(tǒng)</a></li> </ul>
<ul><li><a target="_blank">ubuntu系統(tǒng)</a></li> </ul>
訪問控制:
基于IP實現(xiàn)訪問控制
10.0.0.1 僅允許訪問 /centos
10.0.0.100 拒絕訪問 /ubuntu , 其他的IP都允許
root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf
server {
listen 80;
server_name mirror.oldxu.com;
charset utf8;
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format html;
location / {
index index.html;
}
location /centos {
allow 10.0.0.1/32;
deny all;
}
location /ubuntu {
deny 10.0.0.100/32;
allow all;
}
}
基于用戶名和密碼的訪問控制:
[root@web01 ~]# yum install httpd-tools -y
[root@web01 ~]# htpasswd -bc /etc/nginx/auth_conf oldxu 123
[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf
server {
listen 80;
server_name mirror.oldxu.com;
charset utf8;
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format html;
location / {
index index.html;
}
location /centos {
allow 10.0.0.1/32;
deny all;
}
location /ubuntu {
auth_basic "Oldxu Site";
auth_basic_user_file /etc/nginx/auth_conf;
}
}
限速
請求限制 limit_req
連接限制 limit_conn
下載限速:
[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
server_name mirror.oldxu.com;
charset utf8;
root /code;
limit_req zone=req_one burst=5 nodelay;
limit_conn addr 1;
limit_conn_status 504;
limit_rate 100k;
limit_rate_after 200m;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format html;
location / {
index index.html;
}
location /centos {
allow 10.0.0.1/32;
allow 10.0.0.7/32;
deny all;
}
}
案列
問題
:限制web服務(wù)器請求數(shù)處理為1秒一個锣枝,緩存值為5贾虽、限制用戶僅可同時下載一個文件定罢。當下載超過100M則限制下載速度為500k攒盈。如果同時下載超過2個視頻,則返回提示 "請聯(lián)系oldxu進行會員充值"陵珍。
[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
server_name mirror.oldxu.com;
charset utf8;
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format html;
location / {
index index.html;
}
location /download {
limit_req zone=req_one burst=5 nodelay;
limit_conn addr 2;
limit_conn_status 504;
limit_rate 300k;
limit_rate_after 100m;
}
#接收拋出504的異常,交給內(nèi)部 @error_504 處理
error_page 504 @error_504;
location @error_504 {
#default_type text/html;
#return 200 "請充值會員";
#跳轉(zhuǎn)到其他頁面
#return 302 https://www.waitsun.com/xpay-html;
}
}
nginx七中狀態(tài) stub_stauts
狀態(tài) | 含義 |
---|---|
Active connections | 當前活躍連接數(shù)寝杖,包括Waiting等待連接數(shù)。 |
accepts | 已接收的總TCP連接數(shù)量互纯。 |
handled | 已處理的TCP連接數(shù)量瑟幕。 |
requests | 當前總http請求數(shù)量。 |
Reading | 當前讀取的請求頭數(shù)量 |
Writing | 當前響應(yīng)的請求頭數(shù)量 |
Waiting | 當前等待請求的空閑客戶端連接數(shù) |
狀態(tài)監(jiān)控模塊
location = /nginx_status {
stub_status;
}
location匹配規(guī)則
location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
[root@web01 ~]# cat /etc/nginx/conf.d/location.oldxu.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)';
}
}
nginx 日志
訪問日志 記錄用戶請求的信息
錯誤日志 記錄所有錯誤信息,便于后期排查
server {
access_log /var/log/nginx/vhost_name.log main;
error_log /var/log/nginx/vhost_name_error.log;
}
狀態(tài) | 含義 |
---|---|
$remote_addr | # 記錄客戶端IP地址 user --> web |
$remote_user | # 記錄客戶端用戶名 |
$time_local | # 記錄通用的本地時間 |
$time_iso8601 | # 記錄ISO8601標準格式下的本地時間 |
$request | # 記錄請求的方法以及請求的http協(xié)議 |
$status | # 記錄請求狀態(tài)碼(用于定位錯誤信息) |
$body_bytes_sent | # 發(fā)送給客戶端的資源字節(jié)數(shù),不包括響應(yīng)頭的大小 |
$bytes_sent | # 發(fā)送給客戶端的總字節(jié)數(shù) |
$msec | # 日志寫入時間只盹。單位為秒辣往,精度是毫秒。 |
$http_referer | # 記錄從哪個頁面鏈接訪問過來的 |
$http_user_agent | # 記錄客戶端瀏覽器相關(guān)信息 |
$http_x_forwarded_for | #記錄客戶端IP地址 |
$request_length | # 請求的長度(包括請求行殖卑, 請求頭和請求正文)站削。 |
$request_time | # 請求花費的時間,單位為秒孵稽,精度毫秒 |
注:如果Nginx位于負載均衡器许起,nginx反向代理之后, web服務(wù)器無法直接獲取到客 戶端真實的IP地址菩鲜。
# $remote_addr獲取的是反向代理的IP地址街氢。 反向代理服務(wù)器在轉(zhuǎn)發(fā)請求的http頭信息中,
# 增加X-Forwarded-For信息睦袖,用來記錄客戶端IP地址和客戶端請求的服務(wù)器