Day37nginx常用模塊

需要的服務(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ù)器
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市荣刑,隨后出現(xiàn)的幾起案子馅笙,更是在濱河造成了極大的恐慌,老刑警劉巖厉亏,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件董习,死亡現(xiàn)場離奇詭異,居然都是意外死亡爱只,警方通過查閱死者的電腦和手機皿淋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來恬试,“玉大人窝趣,你說我怎么就攤上這事⊙挡瘢” “怎么了哑舒?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長幻馁。 經(jīng)常有香客問我洗鸵,道長,這世上最難降的妖魔是什么仗嗦? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任膘滨,我火速辦了婚禮,結(jié)果婚禮上稀拐,老公的妹妹穿的比我還像新娘火邓。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布贡翘。 她就那樣靜靜地躺著蹈矮,像睡著了一般。 火紅的嫁衣襯著肌膚如雪鸣驱。 梳的紋絲不亂的頭發(fā)上泛鸟,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天,我揣著相機與錄音踊东,去河邊找鬼北滥。 笑死,一個胖子當著我的面吹牛闸翅,可吹牛的內(nèi)容都是我干的再芋。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼坚冀,長吁一口氣:“原來是場噩夢啊……” “哼济赎!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起记某,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤司训,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后液南,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體壳猜,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年滑凉,在試婚紗的時候發(fā)現(xiàn)自己被綠了统扳。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡畅姊,死狀恐怖咒钟,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情若未,我是刑警寧澤盯腌,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站陨瘩,受9級特大地震影響腕够,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜舌劳,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一帚湘、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧甚淡,春花似錦大诸、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽焙贷。三九已至,卻和暖如春贿堰,著一層夾襖步出監(jiān)牢的瞬間辙芍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工羹与, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留故硅,地道東北人。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓纵搁,卻偏偏與公主長得像吃衅,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子腾誉,可洞房花燭夜當晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容