<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Skynet

    ---------- ---------- 我的新 blog : liukaiyi.cublog.cn ---------- ----------

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      112 Posts :: 1 Stories :: 49 Comments :: 0 Trackbacks

    2009年12月20日 #


    作者:NetSeek  http://www.linuxtone.org (IT運維專家網(wǎng)|集群架構(gòu)|性能調(diào)優(yōu))
    歡迎轉(zhuǎn)載,轉(zhuǎn)載時請務必以超鏈接形式標明文章原始出處和作者信息及本聲明.
    首發(fā)時間: 2008-11-25     更新時間:2009-1-14

    目 錄
    一、        Nginx 基礎(chǔ)知識
    二、        Nginx 安裝及調(diào)試
    三、        Nginx Rewrite
    四、        Nginx Redirect
    五、        Nginx 目錄自動加斜線:
    六、        Nginx Location
    七、        Nginx expires
    八、        Nginx 防盜鏈
    九、        Nginx 訪問控制
    十、        Nginx日志處理
    十一、     Nginx Cache
    十二、     Nginx負載均衡
    十三、       Nginx簡單優(yōu)化      
    十四、        如何構(gòu)建高性能的LEMP環(huán)境
    十五、        Nginx服務監(jiān)控
    十六、        常見問題與錯誤處理.
    十七、        相關(guān)資源下載

    【前言】:
    編寫此技術(shù)指南在于推廣普及NGINX在國內(nèi)的使用,更方便的幫助大家了解和掌握NGINX的一些使用技巧。本指南很多技巧來自于網(wǎng)絡和工作中或網(wǎng)絡上朋友們問我的問題.在此對網(wǎng)絡上愿意分享的朋友們表示感謝和致意!歡迎大家和我一起豐富本技術(shù)指南提出更好的建議!請朋友們關(guān)注: http://www.linuxtone.org 技術(shù)分享社區(qū)! 互想學習共同進步!

    一、 Nginx 基礎(chǔ)知識
    1、簡介
       Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。 Nginx 是由 Igor Sysoev 為俄羅斯訪問量第二的 Rambler.ru 站點開發(fā)的,它已經(jīng)在該站點運行超過兩年半了。Igor 將源代碼以類BSD許可證的形式發(fā)布。盡管還是測試版,但是,Nginx 已經(jīng)因為它的穩(wěn)定性、豐富的功能集、示例配置文件和低系統(tǒng)資源的消耗而聞名了。
    更多的請見官方wiki: http://wiki.codemongers.com/

    2、 Nginx的優(yōu)點
    nginx做為HTTP服務器,有以下幾項基本特性:
    1)        處理靜態(tài)文件,索引文件以及自動索引;打開文件描述符緩沖.
    2)        無緩存的反向代理加速,簡單的負載均衡和容錯.
    3)        FastCGI,簡單的負載均衡和容錯.
    4)        模塊化的結(jié)構(gòu)。包括gzipping, byte ranges, chunked responses, 以及 SSI-filter等filter。如果由FastCGI或其它代理服務器處理單頁中存在的多個SSI,則這項處理可以并行運行,而不需要相互等待。
    5)        支持SSL 和 TLS SNI

    Nginx專為性能優(yōu)化而開發(fā),性能是其最重要的考量, 實現(xiàn)上非常注重效率 。它支持內(nèi)核Poll模型,能經(jīng)受高負載的考驗, 有報告表明能支持高達 50,000 個并發(fā)連接數(shù)。
    Nginx具有很高的穩(wěn)定性。其它HTTP服務器,當遇到訪問的峰值,或者有人惡意發(fā)起慢速連接時,也很可能會導致服務器物理內(nèi)存耗盡頻繁交換,失去響應,只能重啟服務器。例如當前apache一旦上到200個以上進程,web響 應速度就明顯非常緩慢了。而Nginx采取了分階段資源分配技術(shù),使得它的CPU與內(nèi)存占用率非常低。nginx官方表示保持10,000個沒有活動的連 接,它只占2.5M內(nèi)存,所以類似DOS這樣的攻擊對nginx來說基本上是毫無用處的。就穩(wěn)定性而言, nginx比lighthttpd更勝一籌。
    Nginx支持熱部署。它的啟動特別容易, 并且?guī)缀蹩梢宰龅?*24不間斷運行,即使運行數(shù)個月也不需要重新啟動。你還能夠在不間斷服務的情況下,對軟件版本進行進行升級。
    Nginx采用master-slave模型, 能夠充分利用SMP的優(yōu)勢,且能夠減少工作進程在磁盤I/O的阻塞延遲。當采用select()/poll()調(diào)用時,還可以限制每個進程的連接數(shù)。
    Nginx 代碼質(zhì)量非常高,代碼很規(guī)范, 手法成熟, 模塊擴展也很容易。特別值得一提的是強大的Upstream與Filter鏈。 Upstream為諸如reverse proxy, 與其他服務器通信模塊的編寫奠定了很好的基礎(chǔ)。而Filter鏈最酷的部分就是各個filter不必等待前一個filter執(zhí)行完畢。它可以把前一個 filter的輸出做為當前filter的輸入,這有點像Unix的管線。這意味著,一個模塊可以開始壓縮從后端服務器發(fā)送過來的請求,且可以在模塊接收 完后端服務器的整個請求之前把壓縮流轉(zhuǎn)向客戶端。
    Nginx采用了一些os提供的最新特性如對sendfile (Linux 2.2+),accept-filter (FreeBSD 4.1+),TCP_DEFER_ACCEPT (Linux 2.4+) 的支持,從而大大提高了性能

    二、 Nginx 安裝及調(diào)試
    1、Pcre 安裝

    1. ./configure
    2.   make && make install
    3.   cd ../
    復制代碼

    2.        nginx 編譯安裝

    1. ./configure --user=www --group=www --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-openssl=/usr/local/openssl
    2. make && make install
    復制代碼

    更詳細的模塊定制與安裝請參照官方wiki.

    3、Nginx 配置文件測試:

    1. # /usr/local/nginx/sbin/nginx -t  //Debug 配置文件的關(guān)鍵命令需要重點撐握.
    2. 2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    3. 2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
    復制代碼

    3、Nginx 啟動:

    1. # /usr/local/nginx/sbin/nginx
    復制代碼

    4、Nginx 配置文件修改重新加載:

    1. # kill -HUP `cat /usr/local/nginx/logs/nginx.pid
    復制代碼

    `
    三、Nginx Rewrite

    1.  Nginx Rewrite 基本標記(flags)
    last - 基本上都用這個Flag。
    ※相當于Apache里的[L]標記,表示完成rewrite,不再匹配后面的規(guī)則
    break - 中止Rewirte,不再繼續(xù)匹配
    redirect - 返回臨時重定向的HTTP狀態(tài)302
    permanent - 返回永久重定向的HTTP狀態(tài)301
          ※原有的url支持正則  重寫的url不支持正則

    2.  正則表達式匹配,其中:
        * ~         為區(qū)分大小寫匹配
        * ~*       為不區(qū)分大小寫匹配
        * !~和!~*   分別為區(qū)分大小寫不匹配及不區(qū)分大小寫不匹配

    3. 文件及目錄匹配,其中:
       * -f和!-f用來判斷是否存在文件
        * -d和!-d用來判斷是否存在目錄
        * -e和!-e用來判斷是否存在文件或目錄
        * -x和!-x用來判斷文件是否可執(zhí)行


    3.  Nginx 的一些可用的全局變量,可用做條件判斷:

    1. $args
    2. $content_length
    3. $content_type
    4. $document_root
    5. $document_uri
    6. $host
    7. $http_user_agent
    8. $http_cookie
    9. $limit_rate
    10. $request_body_file
    11. $request_method
    12. $remote_addr
    13. $remote_port
    14. $remote_user
    15. $request_filename
    16. $request_uri
    17. $query_string
    18. $scheme
    19. $server_protocol
    20. $server_addr
    21. $server_name
    22. $server_port
    23. $uri
    復制代碼

    四、 Nginx Redirect
    將所有l(wèi)inuxtone.org與netseek.linuxtone.org域名全部自跳轉(zhuǎn)到http://www.linuxtone.org

    1. server
    2. {
    3. listen 80;
    4. server_name linuxtone.org netseek.linuxtone.org;
    5. index index.html index.php;
    6. root /data/www/wwwroot;
    7. if ($host !~ "^www.linxtone.org$") {
    8. rewrite ^(.*) http://www.linuxtone.org$1 redirect;
    9. }
    10. ........................
    11. }
    復制代碼

    五、 Nginx 目錄自動加斜線:

    1. if (-d $request_filename){
    2.            rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
    3.      }
    復制代碼

    六  Nginx Location

    1.基本語法:[和上面rewrite正則匹配語法基本一致]
    location [=|~|~*|^~] /uri/ { … }
        * ~  為區(qū)分大小寫匹配
        * ~* 為不區(qū)分大小寫匹配
        * !~和!~*分別為區(qū)分大小寫不匹配及不區(qū)分大小寫不匹配

    示例1:
    location = / {
    # matches the query / only.
    # 只匹配 / 查詢。
    }
    匹配任何查詢,因為所有請求都已 / 開頭。但是正則表達式規(guī)則和長的塊規(guī)則將被優(yōu)先和查詢匹配

    示例2:
    location ^~ /images/ {
    # matches any query beginning with /images/ and halts searching,
    # so regular expressions will not be checked.
    # 匹配任何已 /images/ 開頭的任何查詢并且停止搜索。任何正則表達式將不會被測試。

    示例3:
    location ~* .(gif|jpg|jpeg)$ {
    # matches any request ending in gif, jpg, or jpeg. However, all
    # requests to the /images/ directory will be handled by
    }
    # 匹配任何已 gif、jpg 或 jpeg 結(jié)尾的請求。


    七、 Nginx expires

    1.根據(jù)文件類型expires

    1. # Add expires header for static content
    2. location ~* .(js|css|jpg|jpeg|gif|png|swf)$ {
    3.     if (-f $request_filename) {
    4.        root /data/www/wwwroot/bbs;
    5.        expires      1d;
    6.        break;
    7.     }
    8. }
    復制代碼

    2、根據(jù)判斷某個目錄

    1. # serve static files
    2. location ~ ^/(images|javascript|js|css|flash|media|static)/  {
    3. root    /data/www/wwwroot/down;
    4.         expires 30d;
    5.   }
    復制代碼

    八、  Nginx 防盜鏈

    1.        針對不同的文件類型

    1. #Preventing hot linking of images and other file types
    2. location ~* ^.+.(gif|jpg|png|swf|flv|rar|zip)$ {
    3.         valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost baidu.com;
    4. if ($invalid_referer) {
    5.       rewrite   ^/   ;
    6.      # return   403;
    7.       }
    8. }
    復制代碼

    2.        針對不同的目錄

    1. location /img/ {
    2.     root /data/www/wwwroot/bbs/img/;
    3.     valid_referers none blocked server_names *.linuxtone.org http://localhost baidu.com;
    4.     if ($invalid_referer) {
    5.                    rewrite  ^/  ;
    6.                    #return   403;
    7.     }
    8. }
    復制代碼

    3.        同實現(xiàn)防盜鏈和expires的方法

    1. #Preventing hot linking of images and other file types
    2. location ~* ^.+.(gif|jpg|png|swf|flv|rar|zip)$ {
    3.         valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost ;
    4. if ($invalid_referer) {
    5.       rewrite   ^/   ;
    6.                      }
    7.      access_log off;
    8.      root /data/www/wwwroot/bbs;
    9. expires 1d;
    10.      break;
    11. }
    復制代碼

    九、 Nginx 訪問控制

    1.        Nginx 身份證驗證

    1. #cd /usr/local/nginx/conf
    2. #mkdir htpasswd
    3. /usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/tongji linuxtone
    4. #添加用戶名為linuxtone
    5. New password:   (此處輸入你的密碼)
    6. Re-type new password:   (再次輸入你的密碼)
    7. Adding password for user
    8. http://count.linuxtone.org/tongji/data/index.html(目錄存在/data/www/wwwroot/tongji/data/目錄下)
    9. 將下段配置放到虛擬主機目錄,當訪問http://count.linuxtone/tongji/即提示要密驗證:
    10. location ~ ^/(tongji)/  {
    11.                 root    /data/www/wwwroot/count;
    12.                         auth_basic              "LT-COUNT-TongJi";
    13.                         auth_basic_user_file  /usr/local/nginx/conf/htpasswd/tongji;
    14.                 }
    復制代碼

    2.        Nginx 禁止訪問某類型的文件.
    如,Nginx下禁止訪問*.txt文件,配置方法如下.

    1. location ~* .(txt|doc)$ {
    2.    if (-f $request_filename) {
    3.    root /data/www/wwwroot/linuxtone/test;
    4.    #rewrite …..可以重定向到某個URL
    5.    break;
    6.    }
    7. }
    復制代碼

    方法2:

    1. location ~* .(txt|doc)${
    2.         root /data/www/wwwroot/linuxtone/test;
    3.         deny all;
    4. }
    復制代碼

    實例:
    禁止訪問某個目錄

    1. location ~ ^/(WEB-INF)/ {
    2.             deny all;
    3. }  
    復制代碼

    3.        使用ngx_http_access_module限制ip訪問

    1. location / {
    2.     deny    192.168.1.1;
    3.     allow   192.168.1.0/24;
    4.     allow   10.1.1.0/16;
    5.     deny    all;
    6. }
    復制代碼

    詳細參見wiki: http://wiki.codemongers.com/NginxHttpAccessModule#allow

    4.        Nginx 下載限制并發(fā)和速率

    1. limit_zone   linuxtone  $binary_remote_addr  10m;
    2. server
    3.        {
    4.                listen       80;
    5.                server_name  down.linuxotne.org;
    6.                index index.html index.htm index.php;
    7.                root   /data/www/wwwroot/down;
    8.                #Zone limit
    9.                location / {
    10.                    limit_conn   linuxtone  1;
    11.                    limit_rate  20k;
    12.                }
    13. ..........
    14.        }
    復制代碼

    只允許客房端一個線程,每個線程20k.
    【注】limit_zone   linuxtone  $binary_remote_addr  10m; 這個可以定義在主的


    5.        Nginx 實現(xiàn)Apache一樣目錄列表

    1. location  /  {
    2.     autoindex  on;
    3. }
    復制代碼

    6.        上文件大小限制
    主配置文件里加入如下,具體大小根據(jù)你自己的業(yè)務做調(diào)整。
    client_max_body_size 10m;                                                         

    十、        Nginx 日志處理

    1.Nginx 日志切割
    #contab -e
    59 23 * * * /usr/local/sbin/logcron.sh /dev/null 2>&1
    [root@count ~]# cat /usr/local/sbin/logcron.sh

    1. #!/bin/bash
    2. log_dir="/data/logs"
    3. time=`date +%Y%m%d`  
    4. /bin/mv  ${log_dir}/access_linuxtone.org.log ${log_dir}/access_count.linuxtone.org.$time.log
    5. kill -USR1 `cat  /var/run/nginx.pid`
    復制代碼

    更多的日志分析與處理就關(guān)注(同時歡迎你參加討論):http://bbs.linuxtone.org/forum-8-1.html

    2.利用AWSTATS分析NGINX日志
      設(shè)置好Nginx日志格式,仍后利用awstats進行分析.
    請參考: http://bbs.linuxtone.org/thread-56-1-1.html

    3.        Nginx 如何不記錄部分日志
    日志太多,每天好幾個G,少記錄一些,下面的配置寫到server{}段中就可以了
    location ~ .*.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$
    {
         access_log off;
    }

    十一、Nginx Cache服務配置

    如果需要將文件緩存到本地,則需要增加如下幾個子參數(shù):

    1. proxy_store on;
    2. proxy_store_access user:rw group:rw all:rw;
    3. proxy_temp_path 緩存目錄;
    復制代碼

    其中,
    proxy_store on用來啟用緩存到本地的功能,
    proxy_temp_path用來指定緩存在哪個目錄下,如:proxy_temp_path html;

    在經(jīng)過上一步配置之后,雖然文件被緩存到了本地磁盤上,但每次請求仍會向遠端拉取文件,為了避免去遠端拉取文件,必須修改

    1. proxy_pass:
    2. if ( !-e $request_filename) {
    3.     proxy_pass  http://mysvr;
    4. }
    復制代碼

    即改成有條件地去執(zhí)行proxy_pass,這個條件就是當請求的文件在本地的proxy_temp_path指定的目錄下不存在時,再向后端拉取。

       
    更多更高級的應用可以研究ncache,詳細請參照http://bbs.linuxtone.org 里ncache相關(guān)的貼子.

    十二、Nginx 負載均衡
    1. Nginx 負載均衡基礎(chǔ)知識
    nginx的upstream目前支持4種方式的分配
    1)、輪詢(默認)
    每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
    2)、weight
    指定輪詢幾率,weight和訪問比率成正比,用于后端服務器性能不均的情況。
    2)、ip_hash
    每個請求按訪問ip的hash結(jié)果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
    3)、fair(第三方)
    按后端服務器的響應時間來分配請求,響應時間短的優(yōu)先分配。
    4)、url_hash(第三方)

    2.        Nginx 負載均衡實例1

    1. upstream bbs.linuxtone.org {#定義負載均衡設(shè)備的Ip及設(shè)備狀態(tài)
    2.     server 127.0.0.1:9090 down;
    3.     server 127.0.0.1:8080 weight=2;
    4.     server 127.0.0.1:6060;
    5.     server 127.0.0.1:7070 backup;
    6. }
    復制代碼

    在需要使用負載均衡的server中增加
    proxy_pass http://bbs.linuxtone.org/;

    每個設(shè)備的狀態(tài)設(shè)置為:
    a)        down 表示單前的server暫時不參與負載
    b)        weight 默認為1.weight越大,負載的權(quán)重就越大。
    c)        max_fails :允許請求失敗的次數(shù)默認為1.當超過最大次數(shù)時,返回proxy_next_upstream 模塊定義的錯誤
    d)        fail_timeout:max_fails次失敗后,暫停的時間。
    e)        backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。

    nginx支持同時設(shè)置多組的負載均衡,用來給不用的server來使用。

    client_body_in_file_only 設(shè)置為On 可以講client post過來的數(shù)據(jù)記錄到文件中用來做debug
    client_body_temp_path 設(shè)置記錄文件的目錄 可以設(shè)置最多3層目錄
    location 對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡


    3.        Nginx 負載均衡實例 2
    按訪問url的hash結(jié)果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效,也可以用作提高Squid緩存命中率.

    簡單的負載均等實例:
    #vi nginx.conf  //nginx主配置文件核心配置

    1. ……….
    2. #loadblance my.linuxtone.org
    3.        upstream  my.linuxtone.org  {
    4.        ip_hash;
    5.        server   127.0.0.1:8080;
    6.        server   192.168.169.136:8080;
    7.        server   219.101.75.138:8080;
    8.        server   192.168.169.117;
    9.        server   192.168.169.118;
    10.        server   192.168.169.119;
    11.      }
    12. …………..
    13. include          vhosts/linuxtone_lb.conf;
    14. ………
    15. # vi proxy.conf
    16. proxy_redirect off;
    17. proxy_set_header Host $host;
    18. proxy_set_header X-Real-IP $remote_addr;
    19. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    20. client_max_body_size 50m;
    21. client_body_buffer_size 256k;
    22. proxy_connect_timeout 30;
    23. proxy_send_timeout 30;
    24. proxy_read_timeout 60;
    25. proxy_buffer_size 4k;
    26. proxy_buffers 4 32k;
    27. proxy_busy_buffers_size 64k;
    28. proxy_temp_file_write_size 64k;
    29. proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
    30. proxy_max_temp_file_size 128m;
    31. proxy_store on;
    32. proxy_store_access   user:rw  group:rw  all:r;
    33. #nginx cache               
    34. #client_body_temp_path  /data/nginx_cache/client_body 1 2;
    35. proxy_temp_path /data/nginx_cache/proxy_temp 1 2;
    復制代碼

    #vi  linuxtone_lb.conf

    1. server
    2.     {
    3.         listen  80;
    4.         server_name my.linuxtone.org;
    5.         index index.php;
    6.         root /data/www/wwwroot/mylinuxtone;
    7.         if (-f $request_filename) {
    8.             break;
    9.            }
    10.         if (-f $request_filename/index.php) {
    11.           rewrite (.*) $1/index.php break;
    12.         }
    13.         error_page 403 http://my.linuxtone.org/member.php?m=user&a=login;
    14.         location / {
    15.            if ( !-e $request_filename) {
    16.                proxy_pass http://my.linuxtone.org;
    17.                break;
    18.            }
    19.            include /usr/local/nginx/conf/proxy.conf;
    20.         }
    21. }
    復制代碼



    十三、Nginx簡單優(yōu)化


    1.        減小nginx編譯后的文件大小 (Reduce file size of nginx)
    默認的nginx編譯選項里居然是用debug模式(-g)的(debug模式會插入很多跟蹤和ASSERT之類),編譯以后一個nginx有好幾兆。去掉nginx的debug模式編譯,編譯以后只有幾百K
    在 auto/cc/gcc,最后幾行有:
    # debug

    1. CFLAGS=”$CFLAGS -g”
    復制代碼

    注釋掉或刪掉這幾行,重新編譯即可。

    2.        修改Nginx的header偽裝服務器
    1)        修改nginx.h

    1. #vi nginx-0.7.30/src/core/nginx.h
    2. #define NGINX_VERSION      "1.8"
    3. #define NGINX_VER          "LTWS/" NGINX_VERSION
    4. #define NGINX_VAR          "NGINX"
    5. #define NGX_OLDPID_EXT     ".oldbin"
    復制代碼

    2) 修改nginx_http_header_filter_module
    #vi nginx-0.7.30/src/http/ngx_http_header_filter_module.c
    將如下

    1. static char ngx_http_server_string[] = "Server: nginx" CRLF;
    復制代碼

    修改為

    1. static char ngx_http_server_string[] = "Server: LTWS" CRLF;
    復制代碼

    a)        修改nginx_http_header_filter_module
    #vi nginx-0.7.30/src/http/ngx_http_special_response.c
    將如下:

    1. static u_char ngx_http_error_full_tail[] =
    2. "<hr><center>" NGINX_VER "</center>" CRLF
    3. "</body>" CRLF
    4. "</html>" CRLF
    5. ;
    復制代碼
    1. static u_char ngx_http_error_tail[] =
    2. "<hr><center>nginx</center>" CRLF
    3. "</body>" CRLF
    4. "</html>" CRLF
    5. ;
    復制代碼

    修改為:

    1. static u_char ngx_http_error_full_tail[] =
    2. "<center> "NGINX_VER" </center>" CRLF
    3. "<hr><center>http://www.linuxtone.org</center>" CRLF
    4. "</body>" CRLF
    5. "</html>" CRLF
    6. ;
    7. static u_char ngx_http_error_tail[] =
    8. "<hr><center>LTWS</center>" CRLF
    9. "</body>" CRLF
    10. "</html>" CRLF
    11. ;
    復制代碼

    修改后重新編譯一下環(huán)境,
    404錯誤的時候顯示效果圖(如果沒有指定錯誤頁的話):
    404.png

    利用curl命令查看服務器header
    curl.png



     

    3.為特定的CPU指定CPU類型編譯優(yōu)化.
    默認nginx使用的GCC編譯參數(shù)是-O
    需要更加優(yōu)化可以使用以下兩個參數(shù)
    --with-cc-opt='-O3'
    --with-cpu-opt=opteron
    使得編譯針對特定CPU以及增加GCC的優(yōu)化.
    此方法僅對性能有所改善并不會有很大的性能提升,供朋友們參考.
    CPUD類型確定: # cat /proc/cpuinfo | grep "model name"
    編譯優(yōu)化參數(shù)參考:http://en.gentoo-wiki.com/wiki/Safe_Cflags


    4.Tcmalloc優(yōu)化Nginx 性能

    1. # wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-alpha.tar.gz
    2. # tar zxvf libunwind-0.99-alpha.tar.gz
    3. # cd libunwind-0.99-alpha/
    4. # CFLAGS=-fPIC ./configure
    5. # make CFLAGS=-fPIC
    6. # make CFLAGS=-fPIC install
    7. # wget http://google-perftools.googlecode.com/files/google-perftools-0.98.tar.gz
    8. # tar zxvf google-perftools-0.98.tar.gz
    9. # cd google-perftools-0.98/
    10. # ./configure
    11. # make && make install
    12. # echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
    13. # ldconfig
    14. # lsof -n | grep tcmalloc
    復制代碼

    編譯nginx 加載google_perftools_module:
    ./configure --with-google_perftools_module
    在主配置文件加入nginx.conf 添加:
    google_perftools_profiles /path/to/profile;

    5.內(nèi)核參數(shù)優(yōu)化
    # vi /etc/sysctl.conf   #在末尾增加以下內(nèi)容:

    1. net.ipv4.tcp_fin_timeout = 30
    2. net.ipv4.tcp_keepalive_time = 300
    3. net.ipv4.tcp_syncookies = 1
    4. net.ipv4.tcp_tw_reuse = 1
    5. net.ipv4.tcp_tw_recycle = 1
    6. net.ipv4.ip_local_port_range = 5000 65000
    復制代碼

    #使配置立即生效
    /sbin/sysctl -p

    十四、如何構(gòu)建高性的LEMP
    請參見: http://www.linuxtone.org/lemp/lemp.pdf
    1、提供完整的配置腳本下載:http://www.linuxtone.org/lemp/scripts.tar.gz
    2、提供NGINX常見配置范例含(虛擬主機,防盜鏈,Rewrite,訪問控制,負載均衡
    Discuz相關(guān)程序靜態(tài)化及等等),你只要稍稍修改即可線上應用。 3、將原版的xcache替換成EA,并提供相關(guān)簡單調(diào)優(yōu)腳本及配置文件。
    更多的及更新資料請關(guān)注: http://www.linuxtone.org

    十五、Nginx監(jiān)控
    1、        RRDTOOL+Perl腳本畫圖監(jiān)控
    先安裝好rrdtool ,關(guān)于rrdtool本文不作介紹,具體安裝請參照linuxtone監(jiān)控版塊.
    #cd /usr/local/sbnin
    #wget http://blog.kovyrin.net/files/mrtg/rrd_nginx.pl.txt
    #mv rrd_nginx.pl.txt rrd_nginx.pl
    #chmod a+x rrd_nginx.pl

    #vi rrd_nginx.pl   //配置腳本文件設(shè)置好路徑
    #!/usr/bin/perl
    use RRDs;
    use LWP::UserAgent;

    # define location of rrdtool databases
    my $rrd = '/data/www/wwwroot/nginx/rrd';
    # define location of images
    my $img = '/data/www/wwwroot/nginx/html';
    # define your nginx stats URL
    my $URL = "http://219.232.244.13/nginx_status";
    …………
    【注】根據(jù)自己具體的狀況修改相應的路徑.
    #crontab –e //加入如下
    * * * * * /usr/local/sbin/rrd_nginx.pl
    重啟crond后,通過配置nginx虛擬主機指到/data/www/wwwroot/nginx/html目錄,通過crond自動執(zhí)行perl腳本會生成很多圖片.
    http://xxx/connections-day.png即可看到服務器狀態(tài)圖。

    2、        官方Nginx-rrd 監(jiān)控服務(多虛擬主機)(推薦)
    網(wǎng)址:http://www.nginx.eu/nginx-rrd.html
    此解決方案其實是基于上述監(jiān)控方案的一個改進和增強,同樣先安裝好rrdtool這個畫圖工具和相應的perl模塊再做如下操作:
    # yum install perl-HTML*
    先建立好生成的庫存和圖片存放錄

    1. #mkdir -p /data/www/wwwroot/nginx/{rrd,html}
    2. #cd /usr/local/sbin
    3. #wget http://www.nginx.eu/nginx-rrd/nginx-rrd-0.1.4.tgz
    4. #tar zxvf nginx-rrd-0.1.4.tgz
    5. #cd nginx-rrd-0.1.4
    6. #cd etc/
    7. #cp nginx-rrd.conf /etc
    8. #cd etc/cron.d
    9. #cp nginx-rrd.cron /etc/cron.d
    10. #cd /usr/local/src/nginx-rrd-0.1.4/html
    11. # cp index.php /data/www/wwwroot/nginx/html/
    12. #cd /usr/local/src/nginx-rrd-0.1.4/usr/sbin
    13. #cp * /usr/sbin/
    復制代碼

    #vi /etc/nginx-rrd.conf

    1. #####################################################
    2. #
    3. # dir where rrd databases are stored
    4. RRD_DIR="/data/www/wwwroot/nginx/rrd";
    5. # dir where png images are presented
    6. WWW_DIR="/data/www/wwwroot/nginx/html";
    7. # process nice level
    8. NICE_LEVEL="-19";
    9. # bin dir
    10. BIN_DIR="/usr/sbin";
    11. # servers to test
    12. # server_utl;server_name
    13. SERVERS_URL="http://219.32.205.13/nginx_status;219.32.205.13  http://www.linuxtone.org/nginx_status;www.linuxtone.org""
    復制代碼

    //根據(jù)你的具體情況做調(diào)整.
    SEVERS_URL 格式 http://domain1/nginx_status;domain1 http://domain2/nginx_status;domain2
    這種格式監(jiān)控多虛擬主機連接狀態(tài):
    重點啟crond服務,仍后通過http://219.32.205.13/nginx/html/ 即可訪問。配置過程很簡單!

    3、        CACTI模板監(jiān)控Nginx
    利用Nginx_status狀態(tài)來畫圖實現(xiàn)CACTI監(jiān)控
    nginx編譯時允許http_stub_status_module

    # vi /usr/local/nginx/conf/nginx.conf

    1. location /nginx_status {
    2. stub_status on;
    3. access_log off;
    4. allow 192.168.1.37;
    5. deny all;
    6. }
    復制代碼
    1. # kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
    2. # wget http://forums.cacti.net/download.php?id=12676
    3. # tar xvfz cacti-nginx.tar.gz
    4. # cp cacti-nginx/get_nginx_socket_status.pl /data/cacti/scripts/
    5. # cp cacti-nginx/get_nginx_clients_status.pl /data/cacti/scripts/
    6. # chmod 755 /data/cacti/scripts/get_nginx*
    復制代碼

    檢測插件

    1. # /data/cacti/scripts/get_nginx_clients_status.pl http://192.168.1.37/nginx_status
    復制代碼

    在cacti管理面板導入
    cacti_graph_template_nginx_clients_stat.xml
    cacti_graph_template_nginx_sockets_stat.xml

    十六、常見問題與錯誤處理
    1、400 bad request錯誤的原因和解決辦法
    配置nginx.conf相關(guān)設(shè)置如下.
    client_header_buffer_size 16k;
    large_client_header_buffers 4 64k;
    根據(jù)具體情況調(diào)整,一般適當調(diào)整值就可以。

    2、Nginx 502 Bad Gateway錯誤
    proxy_next_upstream error timeout invalid_header http_500 http_503;
    或者嘗試設(shè)置:
    large_client_header_buffers 4 32k;


    3、Nginx出現(xiàn)的413 Request Entity Too Large錯誤
    這個錯誤一般在上傳文件的時候會出現(xiàn),
    編輯Nginx主配置文件Nginx.conf,找到http{}段,添加
    client_max_body_size 10m; //設(shè)置多大根據(jù)自己的需求作調(diào)整.
    如果運行php的話這個大小client_max_body_size要和php.ini中的如下值的最大值一致或者稍大,這樣就不會因為提交數(shù)據(jù)大小不一致出現(xiàn)的錯誤。
    post_max_size = 10M
    upload_max_filesize = 2M

    4、解決504 Gateway Time-out(nginx)
    遇到這個問題是在升級discuz論壇的時候遇到的
    一般看來, 這種情況可能是由于nginx默認的fastcgi進程響應的緩沖區(qū)太小造成的, 這將導致fastcgi進程被掛起, 如果你的fastcgi服務對這個掛起處理的不好, 那么最后就極有可能導致504 Gateway Time-out
    現(xiàn)在的網(wǎng)站, 尤其某些論壇有大量的回復和很多內(nèi)容的, 一個頁面甚至有幾百K。
    默認的fastcgi進程響應的緩沖區(qū)是8K, 我們可以設(shè)置大點
    在nginx.conf里, 加入: fastcgi_buffers 8 128k
    這表示設(shè)置fastcgi緩沖區(qū)為8×128k
    當然如果您在進行某一項即時的操作, 可能需要nginx的超時參數(shù)調(diào)大點,例如設(shè)置成60秒:send_timeout 60;
    只是調(diào)整了這兩個參數(shù), 結(jié)果就是沒有再顯示那個超時, 可以說效果不錯, 但是也可能是由于其他的原因, 目前關(guān)于nginx的資料不是很多, 很多事情都需要長期的經(jīng)驗累計才有結(jié)果, 期待您的發(fā)現(xiàn)哈!

    5、如何使用Nginx Proxy
    朋友一臺服務器運行tomcat 為8080端口,IP:192.168.1.2:8080,另一臺機器IP:192.168.1.8. 朋友想通過訪問http://192.168.1.8即可訪問tomcat服務.配置如下:
    在192.168.1.8的nginx.conf上配置如下:

    1. server {
    2. listen 80;
    3. server_name java.linuxtone.org
    4. location / {
    5. proxy_pass http://192.168.1.2:8080;
    6. include /usr/local/nginx/conf/proxy.conf;
    7. }
    8. }
    復制代碼

    6、如何關(guān)閉Nginx的LOG
    access_log /dev/null; error_log /dev/null;

    十七、相關(guān)資源下載

    1.nginx配置示例及腳本下載:
    # wget http://www.linuxtone.org/lemp/scripts.tar.gz #此腳本范例定期更新.


    相關(guān)話題 (查看更多知識庫搜索

    其中以下這項挺值得留意的。
    Tcmalloc 不單可用于 Mysql 的優(yōu)化,還能應用于 Nginx
    雖說 Nginx 本身的性能跟系統(tǒng)占用已經(jīng)做到很優(yōu)秀。

    4.Tcmalloc優(yōu)化Nginx 性能

    1. # wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-alpha.tar.gz
    2. # tar zxvf libunwind-0.99-alpha.tar.gz
    3. # cd libunwind-0.99-alpha/
    4. # CFLAGS=-fPIC ./configure
    5. # make CFLAGS=-fPIC
    6. # make CFLAGS=-fPIC install
    7. # wget http://google-perftools.googlecode.com/files/google-perftools-0.98.tar.gz
    8. # tar zxvf google-perftools-0.98.tar.gz
    9. # cd google-perftools-0.98/
    10. # ./configure
    11. # make && make install
    12. # echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
    13. # ldconfig
    14. # lsof -n | grep tcmalloc




    posted @ 2010-01-31 23:08 劉凱毅 閱讀(1058) | 評論 (0)編輯 收藏

         摘要: tabBar.vim 這個插件實現(xiàn)了類似UltraEdit中的標簽頁的功能,而且能通過Alt-<n>來切換, 安裝: 拷貝文件到[你的gvim的安裝目錄]"vimfiles"plugin"中即可! 下載: http://www.vim.org/scripts/script.php?script_id=1338 C:/Program Files/Vim/_v...  閱讀全文
    posted @ 2010-01-18 13:45 劉凱毅 閱讀(1963) | 評論 (0)編輯 收藏


    學習:
    http://blog.chinaunix.net/u1/59571/showart_2077664.html

    個人對上面文章做的 筆記共享出來 和大家分享!


    posted @ 2010-01-05 11:21 劉凱毅 閱讀(2162) | 評論 (0)編輯 收藏

    python easy_install moin 或 下載 moin-1.9.0 編譯
    設(shè)置 path 中有 moin.py
    #我這 參考
    path = $PATH:/Python25/moin-1.9.0/MoinMoin/script

    # wikiconfig.py 下面我點下 參數(shù) ,大家看代碼 就知道了
    vi  /Python25/moin-1.9.0/wiki/config/wikiconfig.py
    #instance_dir = '/where/ever/your/instance/is'
    instance_dir = wikiconfig_dir+'/../'

    # Where your own wiki pages are (make regular backups of this directory):
    data_dir = os.path.join(instance_dir, 'data-1'''# path with trailing /


    navi_bar 
    = [
            
    # If you want to show your page_front_page here:
            #u'%(page_front_page)s',
            u'標題',
            u
    'FindPage',
            u
    'HelpContents',
        ]

    language_default 
    = 'zh'

    在修改linux 時不起作用 :
    修改/root/tools/moin-1.9.0/MoinMoin/script/server/standalone.py 全局先


    運行:
    $>moin  server standalone --config-dir=/root/wiki/mywiki/config/ --hostname=192.168.102.207 --port=18081
    命令是可調(diào)參數(shù):
    #/root/tools/moin-1.9.0/MoinMoin/script/server/standalone.py
    class PluginScript(MoinScript):
        
    def __init__(self, argv, def_values):
            MoinScript.
    __init__(self, argv, def_values)
            self.parser.add_option(
                
    "--docs", dest="docs",
                help
    ="Set the documents directory. Default: use builtin MoinMoin/web/static/htdocs"
            )
            self.parser.add_option(
                
    "--user", dest="user",
                help
    ="Set the user to change to. UNIX only. Default: Don't change"
            )
            self.parser.add_option(
                
    "--group", dest="group",
                help
    ="Set the group to change to. UNIX only. Default: Don't change"
            )
            self.parser.add_option(
                
    "--port", dest="port", type="int",
                help
    ="Set the port to listen on. Default: 8080"
            )
            self.parser.add_option(
                
    "--hostname""--interface", dest="hostname",
                help
    ="Set the ip/hostname to listen on. Use \"\" for all interfaces. Default: localhost"
            )
            self.parser.add_option(
                
    "--start", dest="start", action="store_true",
                help
    ="Start server in background."
            )
            self.parser.add_option(
                
    "--stop", dest="stop", action="store_true",
                help
    ="Stop server in background."
            )
            self.parser.add_option(
                
    "--pidfile", dest="pidfile",
                help
    ="Set file to store pid of moin daemon in. Default: moin.pid"
            )
            self.parser.add_option(
                
    "--debug", dest="debug",
                help
    ="Debug mode of server. off: no debugging (default), web: for browser based debugging, external: for using an external debugger."
            )


    #成功運行 后
    2009-12-20 23:31:31,796 WARNING MoinMoin.log:139 using logging configuration read from built-in fallback in MoinMoin.log module!

    2009-12-20 23:31:32,515 INFO werkzeug:106  * Running on http://192.168.1.100:8080/


    一些簡單的語法介紹『其中 HelpOnDrawings 的功能 有“驚艷”的效果!!呵呵,自己搭建個自己的wiki 出來 看吧』






    posted @ 2009-12-20 23:34 劉凱毅 閱讀(2204) | 評論 (0)編輯 收藏

    主站蜘蛛池模板: 本道天堂成在人线av无码免费| 亚洲国产精品嫩草影院| 亚洲精品乱码久久久久久下载| 亚洲六月丁香六月婷婷蜜芽 | 日本亚洲视频在线| 亚洲精品**中文毛片| 亚洲免费网站观看视频| av电影在线免费看| 亚洲a一级免费视频| 在线v片免费观看视频| 亚洲国产精品成人| 亚洲免费精彩视频在线观看| 亚洲av无码一区二区三区天堂古代 | 18以下岁毛片在免费播放| 午夜免费福利网站| 亚洲综合无码AV一区二区| 亚洲第一页在线视频| 美女黄频免费网站| 91久久精品国产免费一区| 看全色黄大色大片免费久久| 日韩一卡2卡3卡4卡新区亚洲 | 免费福利在线观看| 午夜精品一区二区三区免费视频| 午夜宅男在线永久免费观看网| 亚洲av无码成人精品区在线播放 | 国产亚洲一区二区精品| 亚洲AV综合色区无码二区偷拍| 免费无毒a网站在线观看| 无码国产精品一区二区免费vr| 免费羞羞视频网站| 久久亚洲免费视频| 国产成人精品日本亚洲语音 | 国产乱人免费视频| 亚洲一区二区三区高清| 曰批免费视频播放在线看片二| 亚洲精品在线免费观看| 国产精品亚洲综合一区| 亚洲资源最新版在线观看| a国产成人免费视频| 暖暖免费高清日本一区二区三区| 亚洲AV无码久久精品色欲|