nginx 優化策列 配置文件詳解

Nginx的啓動、中止與重啓

http://www.cnblogs.com/codingcloud/p/5095066.htmlphp

參考地址 html

https://www.zybuluo.com/phper/note/89391linux

如何使用命令查看nginx服務器的訪問併發鏈接數量

http://www.04007.cn/article/276.htmlnginx

nginx高併發處理設置

http://blog.csdn.net/lmx88/article/details/9012939算法

 

nginx訪問量統計

1.根據訪問IP統計UV後端

awk '{print $1}'  access.log|sort | uniq -c |wc -l緩存

2.統計訪問URL統計PVtomcat

awk '{print $7}' access.log|wc -l服務器

3.查詢訪問最頻繁的URLsession

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

查看與80端口有關的鏈接

 

netstat -na | grep ESTAB | grep 80 | wc -l

可查看全部創建鏈接的詳細記錄

netstat -nat||grep ESTABLISHED|wc

 

 

配置文件

nginx  經常使用命令  

啓動  start nginx.exe

 

 中止  nginx.exe   -s    stop

 

從新加載  軟件正在運行     不想重啓  從新加載地址生效  ,,   nginx.exe   -s   reload

 

 

 

 

#user  nobody;
worker_processes  1;    //進程數

#error_log  logs/error.log;
#error_log  logs/error.log  noti

worker_processes  線程數 在生產環境 下 應該與   服務器的cpu的核數同樣   linux  下 按top 1 可查看內核數量 

 

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_

 

表明http協議 若是是http協議請求    由 http{}  中的配置來進行處理   

server {
        listen       80;
        server_name  manage.my.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
        proxy_pass http://127.0.0.1:8081;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        }
        
    }

就會有不少的http請求進入 nginx   有http{} 中的不一樣  的  server  進行處理

    若是訪問路徑是  manage.my.com  就由本server 進行處理  

    nginx 下會有不少tomcat  

   -- 經過server name 進行匹配

  --  監聽端口    listen     監聽80   多個listen     不會 衝突  由於都屬於nginx 內部

 

 

    -- proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    代理頭 地址 由於是反向代理  因此是代理頭信息

        --   location /  

       /表明  全部   就是全部的  manage.my.com 請求 由括號中來處理

 

啓動nginx 不能啓動   問題 兩個

第一個目錄有中文  

第二 nginx 80 端口被佔用    能夠直接用  nginx.exe 查看 是否佔用  ,若是佔用會有報錯

 

 

 

 

集羣配置

 

 

  1. 在http節點下,添加upstream節點。

 

1

2

3

4

upstream favtomcat {

       server 10.0.6.108:7080;

       server 10.0.0.85:8980;

}

 

   2.  將server節點下的location節點中的proxy_pass配置爲:http:// + upstream名稱,即「

http://favtomcat」.

 

1

2

3

4

5

location / {

            root   html;

            index  index.html index.htm;

            proxy_pass http://favtomcat;

}

 

    3.  如今負載均衡初步完成了。upstream按照輪詢(默認)方式進行負載,每一個請求按時間順序逐一分配到不一樣的後端服務器,若是後端服務器down掉,能自動剔除。雖然這種方式簡便、成本低廉。但缺點是:可靠性低和負載分配不均衡。適用於圖片服務器集羣和純靜態頁面服務器集羣。

 

    除此以外,upstream還有其它的分配策略,分別以下:

 

    weight(權重)

 

    指定輪詢概率,weight和訪問比率成正比,用於後端服務器性能不均的狀況。以下所示,10.0.0.88的訪問比率要比10.0.0.77的訪問比率高一倍。

 

1

2

3

4

upstream favtomcat{

      server 10.0.0.77 weight=5;

      server 10.0.0.88 weight=10;

}

 

    ip_hash(訪問ip)

 

    每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器,能夠解決session的問題。

 

1

2

3

4

5

upstream favresin{

      ip_hash;

      server 10.0.0.10:8080;

      server 10.0.0.11:8080;

}

 

    fair(第三方)

 

    按後端服務器的響應時間來分配請求,響應時間短的優先分配。與weight分配策略相似。

 

1

2

3

4

5

upstream favresin{     

      server 10.0.0.10:8080;

      server 10.0.0.11:8080;

      fair;

}

 

    url_hash(第三方)

 

    按訪問url的hash結果來分配請求,使每一個url定向到同一個後端服務器,後端服務器爲緩存時比較有效。

 

    注意:在upstream中加入hash語句,server語句中不能寫入weight等其餘的參數,hash_method是使用的hash算法。

 

1

2

3

4

5

6

upstream resinserver{

      server 10.0.0.10:7777;

      server 10.0.0.11:8888;

      hash $request_uri;

      hash_method crc32;

}

 

    upstream還能夠爲每一個設備設置狀態值,這些狀態值的含義分別以下:

    

    down 表示單前的server暫時不參與負載.

 

    weight 默認爲1.weight越大,負載的權重就越大。

 

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

 

    fail_timeout : max_fails次失敗後,暫停的時間。

 

    backup: 其它全部的非backup機器down或者忙的時候,請求backup機器。因此這臺機器壓力會最輕。

 

1

2

3

4

5

6

7

upstream bakend{ #定義負載均衡設備的Ip及設備狀態

      ip_hash;

      server 10.0.0.11:9090 down;

      server 10.0.0.11:8080 weight=2;

      server 10.0.0.11:6060;

      server 10.0.0.11:7070 backup;

}

相關文章
相關標籤/搜索