1. 程式人生 > >nginx配置解釋及優化

nginx配置解釋及優化

配置優化的幾項:

1,nginx是基於事件的非阻塞模式的,也就是說一個程序可以處理多個請求,所以worker_processes不必要設定大高,一般小於等於cpu核數

2,靜態檔案快取配置,最大快取數量,檔案未使用存活期

open_file_cache max=555350 inactive=20s;

open_file_cache_valid 30s; 每30s去檢查一次檔案有效

open_file_cache_min_uses 2;  有效期內檔案最少使用次數,即20s中需要使用過2過或以上才會認為是需要快取的。

3,work_connections是併發響應的關鍵配置,理論上是越大越好。可設定成最大可用sockets數量,這個值*worker_processes=最大客戶連線數但使用瀏覽器訪問時,最大客戶連線數需要除以2,因為一次請求瀏覽器會開兩個連線,如果是作為反向代理伺服器則要除以4,因為代理到真實伺服器還會再開2個連線

下面是一個完整的配置的詳細說明:

#user  nobody;  #設定使用nginx的使用者
worker_processes  1;   #設定工作的程序數,通常設定為cpu個數 或 2倍
#設定錯誤檔案存放路徑  notice info 為級別
#error_log  logs/error.log ;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;   #設定pid存放路徑(pid是控制系統中的重要檔案)
#設定最大連線數
events {
    worker_connections  1024;
}
#http中設定了http請求相關的配置 http中有多個server的配置
http {
    include       mime.types;   #包含其它配置檔案 通過這個檔案找到請求檔案型別與mime的對映關係
    default_type  application/octet-stream;   #指定為二進位制流 在上面檔案的對映關係中沒有找到對映關係的請求檔案返回預設的二進位制流
    client_max_body_size 10M;
    upstream xxxx{  #設定多個就可以實現負載均衡   將請求分發到下面兩個server中 xxxx為自己定義的一個名稱
    	 server   xx.xx.xx.xx:8080;
         server   xx.xx.xx.xy:8080;
    }
    #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;   #設定訪問日誌路徑  main為上面定義的日誌格式  設定為off可關閉減少IO開銷
    error_log    logs/error.log crit #設定為嚴格級別錯誤日誌才記錄  
    sendfile        on;    #啟用核心複製模式  應該保持開啟達到最快的IO效率
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  30s;   #支援持久連線  不用每次請求都新開連線   所以設定一個連線的超時時間
    #gzip  on;       #開啟檔案壓縮傳輸   生產環境應該開啟 gzip_min_length/gzip_types可設定壓縮的最小長度及壓縮類別
    server {  # server表示一個虛擬主機,http可有多個server 這個server模組是nginx配置反向代理的模組 
        listen       80;   #監聽埠 
        server_name  localhost;   #伺服器名稱  一個請求過來交給哪個server來處理 由這部分來決定
        #charset utf-8;   #設定字元編碼 
        #access_log  logs/host.access.log  main;   #日誌檔案 
        location / {   #一個server下可有多個locatioin 匹配規則由location後的表示式決定
           proxy_pass         http://xxxx;  #實現反向代理的功能   xxxx是上面upstream的名稱 表示轉發到upstream中的地址上去
           proxy_set_header   Host             $host;
           proxy_set_header   X-Real-IP        $remote_addr;
           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
        #error_page  404              /404.html;
        #redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;  #50x錯誤到定向到50x.html;然後下面配置會去找安裝目錄下的html資料夾 
        location = /50x.html {
            root   html;    #表示在html路徑下找50x.html
        }
        # 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;
        #}
    }
    # 這個server模組是nginx配置虛擬主機的模組  下面配置表示監聽某個ip:port以及預設定向地址
    # 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;
    #    }
    #}

    # HTTPS server
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

配置上的第二部分的server是給出預設的nginx虛擬主機配置。首先需要在伺服器配置IP地址,然後繫結IP地址與虛擬主機,可以通過配置多個server實現多個網站執行在同一nginx上。(虛擬主機使用的是特殊的軟硬體技術,它把一臺執行在因特網上的伺服器主機分成一臺臺虛擬的主機,每臺虛擬主機都可以是一個獨立的網站,可以具有獨立的域名,具有完整的Intemet伺服器功能(WWWFTPEmail等),同一臺主機上的虛擬主機之間是完全獨立的。從網站訪問者來看,每一臺虛擬主機和一臺獨立的主機完全一樣利用虛擬主機,不用為每個要執行的網站提供一臺單獨的Nginx伺服器或單獨執行一組Nginx程序。虛擬主機提供了在同一臺伺服器、同一組

Nginx程序上執行多個網站的功能。--引用其它文章的解釋)

一個http請求到達後,根據域名尋找到哪一個虛擬主機來處理,然後再根據域名後的地址匹配location表示式進而選擇某一個location的處理規則(locatioin括號內的就是處理規則的描述)

location表示式匹配規則:

location表示式有兩種匹配模式:普通字串匹配,正則匹配

表示式形式為:location [=|~|~*|^~|@] /uri/{...}  中括號為可選

=表示精確匹配,請求必須和後面的uri一模一樣

~表示使用正則表示式

~*使用正則且不區分大小寫

^~ 不使用正則的匹配

@表示定義一個變數   用得少

匹配的步驟是:

1,先查詢 =開頭的精確匹配

2,再查詢沒有[ ]裡任何符號的普通匹配,且為最大字首規則,即以匹配得更精確的為準

3,第二步如果匹配上了也會繼續尋找正則匹配,除非第二步使用了 ^* 就會忽略正則匹配了

4,如果正則匹配上了就以第一次正則匹配成功的結果為準(即只要成功匹配到一次就不再向下匹配了),否則以第2步的普通匹配結果為準