Nginx配置詳解

嚇人的猿發表於2018-03-03

Nginx配置詳解

Nginx的主配置檔案/etc/nginx/nginx.conf

#執行使用者
user www-data; 
#啟動程式,通常設定成和cpu的數量相等
worker_processes 4;
#PID檔案
pid /run/nginx.pid;

#工作模式及連線數上限
events {
    worker_connections 768; #單個後臺worker process程式的最大併發連結數
    # multi_accept on;
}

#設定http伺服器,利用它的反向代理功能提供負載均衡支援
http {
    #sendfile 實際上是 Linux2.0+以後的推出的一個系統呼叫,web伺服器可以通過調整
    #自身的配置來決定是否利用 sendfile這個系統呼叫。等到資料包最大時,一次性的傳輸出去,
    #這樣有助於解決網路堵塞,已經是預設了。
    sendfile on;
    #tcp_nopush 會設定呼叫tcp_cork方法,這個也是預設的,結果就是資料包不會馬上傳送出去,
    #等到資料包最大時,一次性的傳輸出去,這樣有助於解決網路堵塞。
    tcp_nopush on;
    tcp_nodelay on;
    #連線超時時間
    keepalive_timeout 65;
    #為了快速尋找到相應MIME type,Nginx使用雜湊表來儲存MIME type與副檔名。
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    #設定mime型別,型別由mime.type檔案定義
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    #設定日誌檔案存放位置
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    #開啟gzip壓縮
    gzip on;
    gzip_disable "msie6";



    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


主配置檔案的最後兩行命令,代表引用其他的檔案,我們主要來看看/etc/nginx/sites-enabled/default

server {
    #偵聽80埠
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    
     #定義伺服器的預設網站根目錄位置
    root /usr/share/nginx/html;
    #定義預設首頁索引檔案的名稱
    index index.html index.htm;

    #定義可以使用http://localhost/訪問
    server_name localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    #新增fastcgi代理配置
    location /demo.cgi {
        fastcgi_pass 127.0.0.1:8888; #指定要執行的fastcgi ip加埠
        #如果URI以斜線結尾,檔名將追加到URI後面,這個值將儲存在變數$fastcgi_script_name
        fastcgi_index main.cgi; 
        include fastcgi_params; #傳遞fastcgi引數
    }
}

  • location配置語法

語法:location [=|~|~*|^~] /uri/ { ... } 

標識含義
=在精確匹配完成後並不進行額外的搜尋。
~*不區分大小寫的正規表示式。
~區分大小寫的正規表示式。
^~禁止在字串匹配後檢查其他的正規表示式


location  = / {  
  # 只匹配 / 的查詢. 
  [ configuration A ]  
}  
 
location ^~ /images/ {  
  # 匹配任何以 /images/ 開始的查詢並且停止搜尋,不檢查正規表示式。
  [ configuration B ]  
}  

location ~* \.(gif|jpg|jpeg)$ {  
  # 匹配任何以.gif, .jpg, or .jpeg結尾的檔案,"\."代表轉義字元,
  #但是/images/123.gif 的請求將在Configuration B中處理。 
  [ configuration C ]  
}  

location  / {  
  # 匹配任何以 / 開始的查詢,但是正規表示式與一些較長的字串將被首先匹配,
  #例如/images/將被configuration B中處理。
  [ configuration D ]  
} 


配置fastcgi

如果我們需要新增fastcgi配置:

fastcgi_pass 127.0.0.1:8888;指定FastCGI伺服器監聽埠與地址。

fastcgi_index main.cgi;指明預設cgi檔案,通過SCRIPT_FILENAME傳遞。

fastcgi_params檔案,儲存於/usr/local/nginx/conf下,為我們的FastCGI模組設定了基本的環境變數。


配置負載均衡伺服器群

負載均衡列表基本配置:

•location / {}:對aspx字尾的進行負載均衡請求,假如我們要對所有的aspx字尾的檔案進行負載均衡時,可以這樣寫:location ~ .*.aspx$ {}

•proxy_pass:請求轉向自定義的伺服器列表,這裡我們將請求都轉向標識為http://cuitccol.com的負載均衡伺服器列表;

•在負載均衡伺服器列表的配置中,weight是權重,可以根據機器配置定義權重(如果某臺伺服器的硬體配置十分好,可以處理更多的請求,那麼可以 為其設定一個比較高的weight;而有一臺的伺服器的硬體配置比較差,那麼可以將前一臺的weight配置為weight=2,後一臺差的配置為 weight=1)。weigth參數列示權值,權值越高被分配到的機率越大;



相關文章