nginx基礎應用與拓展

nginx 統計語句

      1.根據訪問IP統計UV     awk '{print $1}'  access.log|sort | uniq -c |wc -l

      2.統計訪問URL統計PV    awk '{print $7}' access.log|wc -l     

      3.查詢訪問最頻繁的URL  awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|more    

      4.查詢訪問最頻繁的IP   awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|more

      5.根據時間段統計查看日誌   cat  access.log| sed -n '/14\/Mar\/2015:21/,/14\/Mar\/2015:22/p'|more

                                 awk '{print $1}' access.log | sort | uniq -c | sort -n -k 1 -r | head -n 10



#user  nobody;  

worker_processes  1; 8   一般等於cpu總核數或總核數的兩倍,例如兩個四核cpu,則總核數爲8

  

#error_log  logs/error.log;  

#error_log  logs/error.log  notice;  

error_log  logs/error.log  info; 指定錯誤日誌的存放路徑,錯誤日誌記錄級別可選項爲{debug | info | notice | warn | error | crit}

work_rlimit_nofile 51200;定義文件描述符數量

use epoll;使用的網絡I/O模型,linux系統推薦採用epoll模型,

client_max_body_size 8m;允許客戶端請求的最大單個文件字節數

client_body_buffer_size 512k;  緩衝區代理緩衝用戶端請求的最大字節數,可以理解爲先保存到本地再傳給用戶

proxy_connect_timeout   5;  跟後端服務器連接的超時時間-發起握手等待響應超時時間

proxy_read_timeout      5;  連接成功後-等待後端服務器響應時間-其實已經進入後端的排隊之中等待處理

proxy_send_timeout      60;  後端服務器數據回傳時間-就是在規定時間內後端服務器必須傳完所有的數據

proxy_buffer_size       16k;  代理請求緩存區-這個緩存區間會保存用戶的頭信息以供nginx進行規則處理-一般只要能保存下頭信息即可

proxy_buffers           4 64k;  告訴nginx 保存單個用的幾個buffer最大用多大空間

proxy_busy_buffers_size 128k;   如果系統很忙的時候,可以申請更大的proxy_buffers

proxy_temp_file_write_size 128k;   proxy緩存臨時文件的大小











【log_format】


假設將nginx作爲web服務器,位於負載均衡設備、squid、nginx反向代理之後,就不能獲取到

客戶端的真實IP地址了,原因是經過反向代理之後,由於在客戶端和web服務器之間增加了中間層,因此web服務器就無法

直接拿到客戶端的IP了,通過$remote_addr變量拿到的將是反向代理服務器的IP地址,但是,反向代理服務器在轉發

請求的HTTP頭信息中,可以增加X-Forwarded-For信息,用以記錄原有的客戶端IP地址和原來客戶端請求的服務器地址

    這時候,就要用log_format指令來設置日誌格式,讓日誌記錄X-Forwarded-For信息中的IP地址,即客戶端的真實IP

,例如,創建一個名爲mylogformat的日誌格式,再用$http_x_forwarded_for變量記錄用戶的X-Forwarded-For的IP都中

log_format mylogformat(一般是access_log,所以要寫access) ‘$remote_addr – $remote_user [$time_local] “$request” ‘‘$status $body_bytes_sent “$http_referer” ‘‘”$http_user_agent” 

在日誌格式樣式中,變量$remote_addr和$http_x_forwarded_for用於記錄IP地址,

$remote_addr是記錄遠程客戶端的用戶名稱

$time_local是記錄訪問時間與市區

$request是用來記錄請求URL與HTTP協議

$status記錄請求狀態,例如成功是狀態爲200,頁面找到時狀態爲404

$body_bytes_sent用以記錄發送給客戶端的文件主體內容大小,

$http_referer用以記錄從哪個頁面訪問過來的

$http_user_agent用以記錄客戶端瀏覽器的相關信息


nginx 0.7.4之後的版本,access.log指令中的日誌文件路徑可以包含變量

         access_log /data/logs/4server_name.log combinedl


fastcgi:

       可伸縮架構的cgi開發擴展,在內存中,將cgi解釋器保持較高的性能

        php fastcgi :使php腳本運行更快,php程序載入內存,而不是每次需要時,都從存儲器中讀取。減輕cpu負擔

       

 

【http_referer】


有時候有這樣的需求,凡是從百度或者google點過來的請求,也就是說,當用後搜索到你的站點時,一旦他從搜索引擎的搜索頁面進到我們的網站時,就可以做一個跳轉。

 if ($http_referer ~* "www.baidu.com") {

          rewrite ^/(.*)$ http://www.lishiming.net redirect;

      }

      if ($http_referer ~* "www.google.com") {

          rewrite ^/(.*)$ http://www.lishiming.net redirect;

      }

 這樣配置以後,凡是從百度或者google點過來的請求都會跳轉到 www.lishiming.net


 

 

 

 

 

 

event{

    worker_connections  1024;  允許連接數

}

proxy_set_header  Host  $host :首先說明 proxy_set_header 指令在向反向代理的後端Web服務器發起請求時添加指定的 Header頭信息,

                                後端web服務器有多個基於域名的虛擬主機時,通過頭信息Host,用於指定請求的域名,這樣後端web才能識別反向代理請求哪個虛擬主機處理。



nginx日誌記錄

log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘‘$status $body_bytes_sent “$http_referer” ‘‘”$http_user_agent” 


$remote_addr        與$http_x_forwarded_for 用以記錄客戶端的ip地址;

$remote_user        用來記錄客戶端用戶名稱;

$time_local         用來記錄訪問時間與時區;

$request            用來記錄請求的url與http協議;

$status             用來記錄請求狀態;成功是200,

$body_bytes_s ent   記錄發送給客戶端文件主體內容大小;

$http_referer       用來記錄從那個頁面鏈接訪問過來的;

$http_user_agent    記錄客戶端瀏覽器的相關信息;

############################################  http_referer #######################################################################

#       以referer防止而已刷下發驗證碼接口

location /sendValidCode {

                #只允許login和register頁面上的sendValidCode請求

                if ($http_referer ~ "com/login") {

                        set $a "1";

                }

                if ($http_referer ~ ".com/register") {

                        set $a "${a}1";

                }

if ($http_referer ~ "com/reset-password") {

                        set $a "${a}1";

                }

                if ($http_referer ~ "com/h5/reset-password") {

                        set $a "${a}1";

                }

                if ($http_referer ~ "com/company") {

                        set $a "${a}1";

                }

#               add_header A $a;

                if ($a != "1") {

                        return 599;

                }

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass http://17;

proxy_redirectdefault;

}


############################################################################################



#####################################      log_format 配置   as  下面幾行###################

events {

    worker_connections 1024;

}


http {

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';


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

#######################



pid        logs/nginx.pid;  

  

  

events {  

    worker_connections  1024;  

}  

  

  

http {  

    include       mime.types;  

    default_type  application/octet-stream;  

  

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  

    #                  '$status $body_bytes_sent "$http_referer" '  

    #                  '"$http_user_agent" "$http_x_forwarded_for"';  

  

    #access_log  logs/access.log  main;  

      

    

    #設定請求緩衝  

    server_names_hash_bucket_size 128;  

    client_header_buffer_size 32k;  

    large_client_header_buffers 4 32k;  

    client_max_body_size 300m;  

    sendfile on;  

    tcp_nopush     on;  

    keepalive_timeout 60;  

    tcp_nodelay on;  

    server_tokens off;  

    client_body_buffer_size 512k;  緩衝區代理緩衝用戶端請求的最大字節數,可以理解爲先保存到本地再傳給用戶

    proxy_connect_timeout   5;  跟後端服務器連接的超時時間-發起握手等待響應超時時間

    proxy_send_timeout      60;  後端服務器數據回傳時間-就是在規定時間內後端服務器必須傳完所有的數據

    proxy_read_timeout      5;  連接成功後-等待後端服務器響應時間-其實已經進入後端的排隊之中等待處理

    proxy_buffer_size       16k;  代理請求緩存區-這個緩存區間會保存用戶的頭信息以供nginx進行規則處理-一般只要能保存下頭信息即可

    proxy_buffers           4 64k;  告訴nginx 保存單個用的幾個buffer最大用多大空間

    proxy_busy_buffers_size 128k;   如果系統很忙的時候,可以申請更大的proxy_buffers

    proxy_temp_file_write_size 128k;   proxy緩存臨時文件的大小

    client_header_timeout  3m;  

    client_body_timeout    3m;  

    send_timeout           3m;  

   

   

    gzip on;#開啓gzip,節省帶寬  

    gzip_min_length  1100;  

    gzip_buffers     4 8k;  

    gzip_types       text/plain text/css application/x-javascript image/bmp application/javascript;     

      

    output_buffers   1 32k;  

    postpone_output  1460;  

      

    limit_rate_after 3m;#限速模塊,前3M下載時不限速  

    limit_rate 512k; #限速模塊   

  

$remote_addr, $http_x_forwarded_for 記錄客戶端 IP

$remote_user 記錄客戶端用戶名稱

$request 記錄請求的 URL 和 HTTP Protocol

$status 記錄請求狀態

$body_bytes_sent 發送給客戶端的 Bytes,不包括 Header 的大小;該變數與 Apache mod_log_config 的 "%B" 相容

$bytes_sent 發送給客戶端的 總Bytes數

$connection 連接的序列號

$connection_requests 當前通過一個連接獲得的請求數量

$msec 日誌寫入時間。單位爲秒,精度是毫秒

$pipe 如果請求是通過HTTP流水線(pipelined)發送,pipe值爲"p",否則爲"."

$http_referer 記錄從哪個頁面鏈接訪問過來的

$http_user_agent 記錄客戶端瀏覽器相關信息

$request_length 請求的長度(包括請求行,請求頭和請求正文)

$request_time 請求處理時間,單位爲秒,精度毫秒; 從讀入客戶端的第一個位元組開始,直到把最後一個字元發送給客戶端後進行日誌寫入爲止

$time_iso8601 ISO8601標準格式下的本地時間

$time_local 通用日誌格式下的本地時間

  

    server {  

        listen       80;  

        server_name  localhost;  

  

        #charset koi8-r;  

  

        access_log  logs/host.access.log;  

          

          

        #自動補全"/"  

        if (-d $request_filename){  

         rewrite ^/(.*)([^/])$ http://$host/$1$2/ last;  

        } 

#####

location ~* /wei/.* {

                root /dat/;

                rewrite "/wei/(.+)" /$1 break;

}   http://vwei/resource/app-release.apk

          

        ###################動靜分離配置#######################  

          

        ###################動態訪問轉向tomcat處理#######################  

        location ~ \.(jsp|page|do)?$ {  

           proxy_set_header  Host $host;  

           proxy_set_header  X-Real-IP  $remote_addr;  

           proxy_pass http://mcul;#mcul與負載均衡upstream配置名稱mcul一致   

        }  

          

        ###################設定訪問靜態文件直接讀取不經過tomcat#########  

        location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$  {   

           expires      30d;       

        }        

        location ~ .*\.(js|css)?$ {  

           expires      1h;  

        }     

          

        ####################所有請求均由Tomcat處理############  

        #location / {  

        #    root   html;  

        #    index  index.html index.htm;  

        #    proxy_pass  http://mcul;  #mcul與負載均衡upstream配置名稱mcul一致   

        #}  

          

        ###################動靜分離配置#######################  

  

        ##################404錯誤頁#################  

        error_page  404              /404.html;  

        location = /40x.html {    

            root   html;    

        }  

        ##################404錯誤頁#################  

  

        # redirect server error pages to the static page /50x.html  

        #  

          

        ##################50x錯誤頁#################  

        error_page   500 502 503 504  /50x.html;  

        location = /50x.html {  

            root   html;  

        }  

        ##################50x錯誤頁#################  

  

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80  

        #  

        #location ~ \.php$ {  

        #    proxy_pass   http://127.0.0.1;  

        #}  

  

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  

        #  

        #location ~ \.php$ {  

        #    root           html;  

        #    fastcgi_pass   127.0.0.1:9000;  

        #    fastcgi_index  index.php;  

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  

        #    include        fastcgi_params;  

        #}  

  

        # deny access to .htaccess files, if Apache's document root  

        # concurs with nginx's one  

        #  

        #location ~ /\.ht {  

        #    deny  all;  

        #}  

    }  

  

  

    # another virtual host using mix of IP-, name-, and port-based configuration  

    #  

    #server {  

    #    listen       8000;  

    #    listen       somename:8080;  

    #    server_name  somename  alias  another.alias;  

  

    #    location / {  

    #        root   html;  

    #        index  index.html index.htm;  

    #    }  

    #}  

  

    ############負載均衡配置###########  

    upstream mcul {    

         ip_hash;

         server 192.168.1.62:8080;

         server 192.168.1.63:8080;   

    }    

    ############負載均衡配置###########  

  

    # HTTPS server  

    #  

    #server {  

    #    listen       443;  

    #    server_name  localhost;  

  

    #    ssl                  on;  

    #    ssl_certificate      cert.pem;  

    #    ssl_certificate_key  cert.key;  

  

    #    ssl_session_timeout  5m;  

  

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;  

    #    ssl_ciphers  HIGH:!aNULL:!MD5;  

    #    ssl_prefer_server_ciphers   on;  

  

    #    location / {  

    #        root   html;  

    #        index  index.html index.htm;  

    #    }  

    #}  

  

}  



nginx主要是工作在七層  爲http提供方向代理的


轉發能力上   lvs > haproxy > nginx

消耗資源上   nginx最小


##########這裏是陳乃龍自學##########

如果我想分離出所有php頁面,並且指定存放php頁面的指定目錄



upstream phpserver {

      server 192.168.1.102:8080

      server 192.168.1.103:8080

}


upstream imgserver {

     server 192.168.1.102:8081

server 192.168.1.103:8081

}


location ~* \.php$ {

      fastcgi_pass http://phpserver;   #個人理解 fastcgi是迅速匹配的,尤其某些類型的靜態文件

}

       


location / {

     root /web/html;

index index.html index.php;

}


location ~* "\.(jpg|png|gif)$" {

     proxy_pass http://imgserver;

}


location /images/ {

  rewrite http://172.16.1.10/images/

}


location / {

  root html;

  index index.html;

  rewrite ^/bbs/(.*)$ http://172.16.100.16/forum/$1;  當訪問鏈接爲更目錄爲bbs開頭的,那麼轉發到http../forum/ 下面鏈接原封不動。$!代表 : /(.*)

}                                                     #rewrite ^/bbs/(.*)$ http://172.16.100.16/forum/$1 last;  意思是:本輪匹配結束之後,開啓下一輪檢查  

爲了防止死循環,使用  rewrite.....htpp:.... break;




完整獲取客戶端ip與服務端realserver ip

             proxy_set_header Host $host;

             proxy_set_header X-Forwarded-Host $host;

             proxy_set_header X-Forwarded-Server $host;

proxy_set_header X-Forwarded-For  $remote_addr;

             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 服務端響應客戶端時候,也是http返回時,記錄服務端ip





        location /chen {

            root   html;         定義主頁,訪問項目名時候,主頁就是html文件

            index  index.html index.jsp;   我的主頁呢?那就是index.html和index.jsp,你也可以寫chen。html。但是這三個html結尾的文件只能有一個存在

            proxy_pass http://tomcat_8080/chen;   轉達到這裏,然後進入chen目錄,找index主頁

        }


nginx 讀寫分離

httpd.conf  

<Directory  /www/html>

Dav on


location / {

proxy_pass http://192.168.182.6/;   #這是nginx服務的IP,表示請求方式爲put的話,從下面182.7節點上(看下面request)

if ($request_method = "PUT"){

 proxy_pass http://192.168.182.7/;

}

}



這是其中一個做靜態分離時候 用的location 哈哈哈,root意思是訪問文件的路徑


location / {

root   /home/nginx/webJsjtxx;

index index.jsp index.do index.html index.htm;

}


   location / {

allow 192.168.182.0/24

deny all;

}


nginx安裝對應參數

–prefix= 指向安裝目錄

–sbin-path 指向(執行)程序文件(nginx)

–pid-path= 指向pid文件(nginx.pid)

–conf-path= 指向配置文件(nginx.conf)

–lock-path= 指向lock文件(nginx.lock)

--with-http_realip_module          將用戶ip轉發到後端服務器

--prefix=/mall/web/nginx 

--with-http_stub_status_module     獲取nginx自上次啓動以來的工作狀態

--with-http_ssl_module             啓用ngx_http_ssl_module支持(使支持https請求,需已安裝openssl)

--with-http_gzip_static_module     啓用ngx_http_gzip_static_module支持(在線實時壓縮輸出數據流) nginx支持壓縮數據,爲了節省寬帶,但是無形中消耗了cpu,並且只支持text/html,支持其他需要添加gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png;

--add-module=/usr/local/src/nginx/nginx_upstream_check_module-master 

--add-module=/usr/local/src/nginx/nginx-goodies-nginx-sticky-module-ng

--add-module=/usr/local/src/nginx/echo-nginx-module-0.51

–with-http_gzip_static_module 啓用ngx_http_gzip_static_module支持(在線實時壓縮輸出數據流)



~      #波浪線表示執行一個正則匹配,區分大小寫

~*    #表示執行一個正則匹配,不區分大小寫

^~    #^~表示普通字符匹配,如果該選項匹配,只匹配該選項,不匹配別的選項,一般用來匹配目錄

=      #進行普通字符精確匹配

@     #"@" 定義一個命名的 location,使用在內部定向時,例如 error_page, try_files


多個location配置的情況下匹配順序爲 : 

首先匹配 =,其次匹配^~, 其次是按文件中順序的正則匹配,最後是交給 / 通用匹配。當有匹配成功時候,停止匹配,按當前匹配規則處理請求。


語法規則: location [=|~|~*|^~] /uri/ { … } = 表示精確匹配,這個優先級也是最高的 

^~ 表示uri以某個常規字符串開頭,理解爲匹配 url路徑即可。

nginx不對url做編碼,因此請求爲/static/20%/aa,可以被規則^~ /static/ /aa匹配到(注意是空格)。

 ~ 表示區分大小寫的正則匹配                                    location ~ /exam/   proxy_pass http://192.168.1.53:8080; 去8080下尋找項目爲exam的程序

 ~* 表示不區分大小寫的正則匹配(和上面的唯一區別就是大小寫) 

 !~和!~*分別爲區分大小寫不匹配及不區分大小寫不匹配的正則 

 / 通用匹配,任何請求都會匹配到,默認匹配.

 

 

 配置上傳文件大小設置  client_max_body_size 300m

 

 

 location /status    則訪問status這個項目,則進入這個項目下,按照定義的index進行搜索主頁html或者jsp或其他輸出

 location /status/   只會輸出status   只能是一個文件。

  location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {

           root /var/www/img/;

           expires 30d;  客戶端瀏覽器緩存時間,超過30天則失效

       }


  定義主頁,兩種方法

        location / {

                        root   /home/tomcat/tomcat_8080/webapps/chen;

                        index index.jsp index.do index.html index.htm;

                  }

 

  location / {

           rewrite ^/ http://mylvs.edu.cn/chen/index.html permanent;

                  }

######################  nginx  禁用 trace   #######################

if ($request_method = TRACE ) {

 

return 403;

 

}



 if ($request_method !~ ^(GET|HEAD|POST)$ ) 

        { 

          return 403; 

       }  

 

#############  

        location /activemq_44/ {

                if ($http_referer ~ "home/menu.jsp") {

                    set $a "1";

                }

                if ($a != "1") {

                        return 403;

                }

                proxy_pass     http://17/;

                proxy_redirect  default;

        }


#############################################################關於圖片文件的動靜分離

http:///1-1.png


        location ~* /resource/.* {

           location ~ .*\.(ico|gif|jpg|jpeg|png|bmp|swf)$ {

                expires 180d;

                proxy_redirect off;

                root /data/client/resource/;

                rewrite "/resource/(.+)" /$1 break;

        }

                proxy_redirect off;

                root /data/client/resource/;

                rewrite "/resource/(.+)" /$1 break;

       }


  

  

  【AWStats】

  是在Sourceforge上發展很快的一個基於Perl的WEB日誌分析工具。

  它可以統計您站點的如下信息:


             訪問量(UV),訪問次數,頁面瀏覽量(PV),點擊數,數據流量等

             精確到每月、每日、每小時的數據

             訪問者國家

             訪問者IP

             Robots/Spiders的統計

             訪客持續時間

             對不同Files type的統計信息

             Pages-URL的統計

             訪客操作系統瀏覽器等信息

             其它信息(搜索關鍵字等等)

 

修改nginx.conf的日誌格式,不然awstats無法分析。 


    log_format access '$remote_addr - $remote_user [$time_local] "$request" '


        '$status $body_bytes_sent "$http_referer" '


        '"$http_user_agent" "$http_x_forwarded_for"';


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


注意,日誌格式裏的雙引號不能漏了且每個參數之間是一個空格分隔,因爲只要有細微的差別,awstats就無法讀取日誌。





【#開啓nginx狀態監控】

location /nginx_status { 

stub_status on;

access_log off; 

allow 10.8.17.6;  

deny all;

auth_basic "nginx_status"; #用戶名

auth_basic_user_file conf/htpasswd; #密碼包路徑,需要用apache htpasswd工具生成

}



【location中嵌套】

        location ~* ^/wxcs-market/data/.* {

           location ~ .*\.(ico|gif|jpg|jpeg|png|bmp|swf)$ {

                expires 180d;

                proxy_redirect off;

                rewrite "/data/(.+)" /$1 break;

               }

                proxy_redirect off;

                root /data/newmall/;

                rewrite "/data/(.+)" /$1 break;

       }

#       if ($host != "")

#       {

#         return 400;

#       }



##############################################   圖片訪問,配置,已經試過了#######################


        location ~* /society/.*\.(ico|gif|jpg|jpeg|png|bmp|swf)$  {

            proxy_redirect off;

            root /data/client/company-client/society/;           

   rewrite "/society/(.+)" /$1 break;

        }

##################後端服務器返回502 504、執行超時等錯誤時候,自動發送請求到upstream負載均衡池中的另一臺服務器,實線故障轉移

        location // {

                proxy_next_upstream http_502 http_504 error timeout invalid_header;

                proxy_pass     http://error_server_pool;

proxy_set_header Host ;

proxy_set_header X-Forwarded-For $remote_addr;

                proxy_redirect  default;

        }

【nginx支持的幾種算法分析】


         1、 輪詢 每個請求按時間順序分配到不同的後端服務器了,後端服務器down掉,自動切除;

         

         2、weight  設定服務器權值: 如weight=2    服務器性能不均時候使用 。weight: 默認爲1,weight越大,負載的權重越大;

         

         3、 ip_hash 每個請求按訪問ip的hash結果分配,每個訪客有固定的後端服務器,可以解決session問題;

         

         4、 fair(第三方) 按後端服務器的響應時間來分配,響應時間短的優先分配

         

         5、url_hash (第三方) 按訪問的url的hash結果分配,使每個url定向到同一個後端服務器,後端爲緩存服務器比較有效。

         

【詳細參數介紹:】


         1)down : 當前的IP server暫時不參與負載,不進行反向代理;

         

         2)max_fails: 允許請求失敗的次數默認爲1,當超過最大次數時,返回proxy_next_upstream模塊定義的錯誤;

         

         3)fail_timeout : max_fails次失敗後,暫停的時間;

         

         4)backup:  其它所有非backup機器down或者忙時候,請求backup機器,這臺機器壓力最輕。

 

 

 

alias nginx=/home/nginx/sbin/nginx


【nginx 緩存】

nginx緩存是存在用戶空間的,也就是用戶瀏覽器緩存。

減少後端壓力、內部網絡傳輸,一些靜態資源,不常改變的,沒必要每次都請求後端


nginx對哪些進行緩存?


1. 緩存沒有 Set-Cookie 的GET和HEAD的響應。


2. 通過定義獨特的原始URL,如proxy_cache_key。


3. 通過定義緩存時間,如X-Accel-Expires、Cache-Control、Expires。



######################################

  測試下nginx more_set_headers 模塊  要對com的nginx上的報文做個處理


 2016/11/3 12:42:22

好的

 2016/11/3 12:42:21

這個模塊只能在location裏面生效

 2016/11/3 12:43:19

針對uuid爲 B97FAAAE-A99B-4AEA-9BEB-A2309EF1826E的報文頭 做一些處理 比如說加標記報文頭或者刪除referrer  配合函數返回指定狀態碼

######################################



nginx重寫個配置文件:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/jsp.conf    加載了這個配置文件


*/1 * * * * /usr/sbin/ntpdate  && /sbin/hwclock --systohc



【proxy_redirect】

語法:proxy_redirect [ default|off|redirect replacement ] 

默認值:proxy_redirect default 

使用字段:http, server, location 

如果需要修改從被代理服務器傳來的應答頭中的"Location"和"Refresh"字段,可以用這個指令設置。 

假設被代理服務器返回Location字段爲: http://localhost:8000/two/some/uri/ 

這個指令: 

proxy_redirect http://localhost:8000/two/ http://frontend/one/; 

將Location字段重寫爲http://frontend/one/some/uri/。 

在代替的字段中可以不寫服務器名: 


proxy_redirect http://localhost:8000/two/ /; 


############################################ nginx如何防止DDOS*** http_limit_conn#######################################  

16、nginx如何防止DDOS***?


    主要使用nginx的http_limit_conn和http_limit_req模塊來防禦。

ngx_http_limit_conn_module 可以限制單個IP的連接數,

ngx_http_limit_req_module 可以限制單個IP每秒請求數

http{}

limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;        所有訪問ip 限制每秒10個請求

limit_conn_zone $binary_remote_addr zone=addr:10m;      

    location {}

    limit_req zone=one burst=5 nodelay;    允許超過頻率限制的請求數不多於5個,假設1、2、3、4秒請求爲每秒9個,那麼第5秒內請求15個是允許的  反之,如果第一秒內請求15個,會將5個請求放到第二秒,第二秒內超過10的請求直接503,類似多秒內平均速率限制。

    limit_conn addr 1;    // 限制同一時間內1個連接,超出的連接返回503

###############################################



########  nginx  ########## tcp轉發

server {

        server_name ;

        listen 72;

        proxy_pass:;

}

##########################




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章