Nginx配置文件-3

設置黑白名單:

語法:
allow
deny

作用位置:
http, server, location, limit_except

具體實現:
server {
    server_name www.a.com;
    listen 80;
    root /web/a.com;
    index index.html;
    server_tokens off;

    location /test {
        root /www/html;
        deny 172.20.23.23;
        allow 172.20.23.33;
        deny all;
    }

    location /test1 {
        alias /mydata/html;
    }

}

測試訪問:
[root@www21:17:48~]#curl http://www.a.com/test/ 
<h1>test location page for nginx</h1>

更改配置:
allow 172.20.23.33; --->換成deny
測試:
[root@www21:21:16~]#curl http://www.a.com/test/ 
<html>
<head><title>403 Forbidden</title></head>

基於用戶的認證:

關鍵語法:
auth_basic "提醒語句"
官網說明:
Syntax: auth_basic string | off;
Default:    
auth_basic off;
Context:http, server, location, limit_except

auth_basic_user_file
官網說明:
Syntax: auth_basic_user_file file;
Default:    —
Context:http, server, location, limit_except

具體使用:
server {
        location /test1 {
        alias /mydata/html;
        auth_basic "test nginx";
        auth_basic_user_file /etc/nginx/conf.d/.npasswd;
    }
}

設置用戶密碼:
[root@www21:28:47conf.d]#htpasswd -c -m /etc/nginx/conf.d/.npasswd tom
New password: 
Re-type new password: 
Adding password for user tom

測試:
[root@www21:32:56~]#curl -u tom:123456 http://www.a.com/test1/
<h1>test alias for nginx</h1>

查看狀態信息:

stub_status  on|off

具體使用:
server {
    location /status {
        stub_status on;
        allow 172.20.23.33;
        deny all;
     }

}

具體信息解析:
Active connections:當前狀態,活動狀態的連接數 
accepts:統計總值,已經接受的客戶端請求的總數 
handled:統計總值,已經處理完成的客戶端請求的總數 
requests:統計總值,客戶端發來的總的請求數 
Reading:當前狀態,正在讀取客戶端請求報文首部的連接的連接數 
Writing:當前狀態,正在向客戶端發送響應報文過程中的連接數 
Waiting:當前狀態,正在等待客戶端發出請求的空閒連接數

設置訪問日誌格式:

設置格式:
log_format [log_name] '$1 $2 $3';
設置在http段中
引用日誌格式:
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];

具體使用:
自定義日誌格式:
log_format test '$remote_addr:$remote_port $request_method $remote_user $status $http_user_agent $bytes_sent $gzip_ratio';  
使用自定義格式:
server {
    server {
    server_name www.a.com;
    listen 80;
    root /web/a.com;
    index index.html;
    server_tokens off;
    access_log /web/a.com/a.com.log test;
}

http核心模塊的內置變量:
 $uri:當前請求的uri,不帶參數
 $host:http請求報文中host首部,如果請求中沒有host首部,則以處理此請求的虛擬主機名
  代替!
 $request_uri:請求的uri,帶完整參數
 $hostname:nginx服務運行在的主機的主機名
 $remote_addr:客戶端ip
 $remote_port:客戶端端口
 $remote_user:使用用戶認證時客戶端用戶輸入的用戶名
 $request_filename:用戶請求中的RUI經過本地root或alias轉換後映射的本地的文件或路徑
 $request_method:請求方法
 $server_addr:服務器地址
 $server_name:服務器名稱
 $server_port:服務器端口
 $server_protocol:服務器向客戶端發送響應時的協議 如http/1.1
 $scheme:在請求中使用的scheme,如https http,哪個協議
 $http_HEADER:匹配請求報文中指定的HEADER  $http_host匹配請求報文中的host首部
 $sent_http_HEADER:匹配響應報文中指定的HEADER---要小寫
  例子:$http_content_type匹配響應報文中的content-type首部
 $document_root:當前請求映射到的root配置項
日誌格式變量: 
 $status:用來引用狀態碼
 $bytes_sent:發送的字節數
 $http_referer:從哪個頁面跳轉過來的
 $http_user_agent:瀏覽器的類型
 $gzip_ratio:壓縮比例,配合壓縮功能

設置日誌緩存:

open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];  

open_log_file_cache off;  
緩存各日誌文件相關的元數據信息   
max:緩存的最大文件描述符數量  
min_uses:在inactive指定的時長內訪問大於等於此值方可被當作活動項  
inactive:非活動時長  
valid:驗證緩存中各緩存項是否爲活動項的時間間隔 

壓縮功能:

關鍵設置:
gzip on|off
官網說明:
Syntax: gzip on | off;
Default:    
gzip off;
Context:http, server, location, if in location
基本設置項:
gzip_buffers [n]:壓縮時緩衝大小
gzip_comp_level[0--9]:壓縮等級
gzip_distable [對哪種瀏覽器,文件不壓縮]msie6
gzip_min_length [值]:內容大於這個值纔會壓縮
gzip_http_version:壓縮後構建響應報文,使用那個版本 1.0/1.1
gzip_types:只對那些類型的網頁文件做壓縮默認包含有text/html
gzip_buffers number size; 支持實現壓縮功能時緩衝區數量及每個緩存區的大小  
默認:32 4k 或 16 8k
gzip_vary on | off;如果啓用壓縮,是否在響應報文首部插入“Vary: Accept-Encoding

具體使用:
server {
        location /test3/ {
        gzip on;
        gzip_comp_level 6;
        gzip_min_length 100;
        gzip_types text/xml text/css text/plain application/javascript;
    }
}

測試:
172.20.23.33:44018 GET - 200 curl/7.29.0  200 436946 "-" -->壓縮前
172.20.23.33:44020 GET - 200 curl/7.29.0  200 3252 "134.86"-->壓縮後
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章