標籤:模型 web伺服器 events notice 使用者組
user www; #定義Nginx啟動並執行使用者和使用者組
worker_processes 2; #nginx進程數,建議設定為等於CPU總核心數。
#error_log logs/error.log; 全域錯誤記錄檔定義類型
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; 進程檔案
events { 工作模式與串連數上限
worker_connections 2048; 每個進程的最大串連數(最大串連數=串連數*進程數)
use epoll;事件處理機制(epoll) :在系統核心2.6 以上,高效能網路I/O模型,並發數比較大,socket串連比較多的web伺服器使用
epoll是最好的。如果跑在FreeBSD上面,就用kqueue模型。
#參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
}
#設定http伺服器
http {
include mime.types; #副檔名與檔案類型映射表
default_type application/octet-stream; #預設檔案類型
charset utf-8; #預設編碼
sendfile on; #開啟高效檔案傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出 檔案,對於普通應用設為 on,如果用來進行下載等應用磁碟IO重負載應用,可設定為off,以平衡磁碟與網路I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off。
autoindex on; #開啟目錄列表訪問,合適下載伺服器,預設關閉。
tcp_nopush on; #防止網路阻塞
tcp_nodelay 能有效提高資料的即時響應性
keepalive_timeout 120; #長連線逾時時間,單位是秒
#自訂日誌格式
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 /home/access.log main;
gzip on; #開啟gzip壓縮輸出
經過gzip壓縮後頁面大小可以變為原來的30%甚至更小,這樣,使用者瀏覽頁面的時候速度會塊得多。gzip的壓縮頁面需要瀏覽器和伺服器雙方都支援,實際上就是伺服器端壓縮,傳到瀏覽器後瀏覽器解壓並解析。瀏
gzip_min_length 1k; //設定允許壓縮的頁面最小位元組數,頁面位元組數從header頭得content-length中進行擷取。預設值是0,不管頁面多大都壓縮。建議設定成大於1k的位元組數,小於1k可能會越壓越大。
gzip_buffers 16 64k; #壓縮緩衝區-//設定系統擷取幾個單位的緩衝用於儲存gzip的壓縮結果資料流。4 16k代表以16k為單位,安裝未經處理資料大小以16k為單位的4倍申請記憶體。
gzip_http_version 1.1; #壓縮版本(預設1.1,前端如果是squid2.5請使用1.0)
gzip_comp_level 6; #壓縮等級 ,1壓縮比最小,傳輸速度最快,9壓縮比做大,傳輸速度最慢,比較好CPU
gzip_types text/plain application/x-javascript text/css application/xml;壓縮類型
gzip_vary on;/和http頭有關係,加個vary頭,給Proxy 伺服器用的,有的瀏覽器支援壓縮,有的不支援,所以避免浪費不支援的也壓縮,所以根據用戶端的HTTP頭來判斷,是否需要壓縮
server {
# listen 80 default;
# server_name_;
# return 500;
underscores_in_headers on;
listen 80;
server_name node.convert.com;
access_log /home/host.access.log main;
location / {
root /var/www/html;
index index.html index.htm;
}
===========================================================================================
反向 Proxy
user www;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 2048;
}
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 /home/access.log main;
sendfile on;
charset utf-8;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
# tcp_nodely on;
gzip on;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
client_max_body_size 100;/允許用戶端請求的最大單個檔案位元組數,它出現在要求標頭部的Content-Length欄位。(可以更改此參數達到限制使用者上傳檔案大小的目的)
client_body_buffer_size 512k;\\緩衝區代理緩衝用戶端請求的最大位元組數,可以理解為先儲存到本地再傳給使用者。 這個指令可以指定串連請求使用的緩衝區大小,預設值:8k/16k 。如果用戶端請求一個檔案大於128k,則Nginx會嘗試在硬碟上建立臨時檔案。如果硬碟滿了,則會報錯
proxy_connect_timeout 600s; \\跟後端伺服器串連的逾時時間,發起握手等候響應逾時時間
proxy_read_timeout 600s; \\串連成功 _等候後端伺服器的回應時間_其實已經進入後端的排隊中等候處理。預設值:
proxy_send_timeout 600s;\\後端伺服器回傳時間_就是在規定時間內後端伺服器必須傳完所有的資料。設定Proxy 伺服器轉寄請求的逾時時間,同樣指完成兩次握手後的時間,如果超過這個時間Proxy 伺服器沒有資料轉寄到後端伺服器,nginx將關閉串連。
proxy_buffer_size 32k;\\代理請求緩衝區_這個緩衝區間會儲存使用者的頭資訊來提供Nginx進行規則處理。一般只要能儲存下頭資訊即可。預設值:proxy_buffer_size 4k/8k 。設定從後端伺服器讀取的第一部分應答的緩 沖區大小,通常情況下這部分應答中包含一個小的應答頭。
proxy_buffers 4 32k;\\設定用於讀取應答(來自後端伺服器)的緩衝區數目和大小,告訴Nginx儲存單個用的幾個 Buffer,最大用多大空間
proxy_busy_buffers_size 64k;\\如果系統很忙的時候可以申請更大的proxy_buffers,官方推薦*2
proxy_temp_file_write_size 64k; \\設定在寫入proxy_temp_path時緩衝臨檔案資料的大小,在預防一個背景工作處理序在傳遞檔案時阻塞太長
proxy_temp_path /dev/shm/proxy_temp; \\類似於http核心模組中的client_body_temp_path指令,指定一個目錄來緩衝比較大的被代理請求。
反代的三種寫法
寫法一: include extra/upstream01.conf;
upstream01.conf;
server {
# underscores_in_headers on;
# listen 80;
# server_name node .convert.com;
# access_log /home/host2.access.log main;
#
# location / {
# proxy_pass http://www.51cto.com/;
# proxy_set_header Host $host;
# proxy_redirect off;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
# }
寫法二:
upstream node1_convert_pool {
# server 192.168.1.210:80 ;
# }
# upstream node2_convert_pool {
# server 192.168.1.211:80 ;
# }
# server {
# underscores_in_headers on;
# listen 80;
# server_name node.convert.com;
# access_log /home/host1.access.log main;
#
# location / {
# proxy_pass http://node1.convert.com;
# proxy_set_header Host $host;
# proxy_redirect off;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
# }
#
# }
# server {
# underscores_in_headers on;
# listen 80;
# server_name node2.convert.com;
# access_log /home/host2.access.log main;
#
# location / {
# proxy_pass http://node2_convert_pool/;
# proxy_set_header Host $host;
# proxy_redirect off;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
# }
#
#
# }
}
寫法三:
server {
# underscores_in_headers on;
# listen 80;
# server_name node .convert.com;
# access_log /home/host2.access.log main;
#
# location / {
# proxy_pass http://www.51cto.com/;
# proxy_set_header Host $host;
# proxy_redirect off;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
# }
#
本文出自 “crazy_sir” 部落格,請務必保留此出處http://douya.blog.51cto.com/6173221/1541615