nginx優化
# 普通用戶啟動 (useradd nginx -s /sbin/nologin -M) user nginx; # 配置nginx worker進程個數 #worker_processes 8; #worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; #worker_cpu_affinity 0001 0010 0100 1000 0001 0010 1000 0001 0010 0100 1000; worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000; #worker_processes 2; #worker_cpu_affinity 0101 1010; # 配置日志存放路徑 access_log logs/access.log warn; error_log logs/error.log main; pid logs/nginx.pid; # nginx事件處理模型優化 events { worker_connections 65535; # 當個進程允許的客戶端最大連接數 use epoll; } # 配置nginx worker進程最大打開文件數 work_rlimit_nofile 65535; http { # 隱藏版本號 server_tokens off; # 設置日志格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; # 開啟高效文件傳輸模式 include mime.types; # 媒體類型 default_type application/octet-stream; # 默認媒體類型 charset utf-8; # 默認字符集 sendfile on; tcp_nopush on; # 只有在sendfile開啟模式下有效 # 設置連接超時時間 keepalive_timeout 65; # 設置客戶端連接保持會話的超時時間,超過則服務器會關閉該連接 tcp_nodelay on; # 打開tcp_nodelay,在包含了keepalive參數才有效果 client_header_timout 15; # 設置客戶端請求有超時時間,該時間內客戶端未發送數據,nginx將返回‘Request time out(408)’錯誤 client_body_timeout 15; # 設置客戶端請求體超時時間,同上 send_timeout 15; # 設置相應客戶端的超時時間,超時nginx將會關閉連接 # 上傳文件大小設置(動態引用) client_max_body_size 10m; # 數據包頭部緩存大小 client_header_buffer_size 1k; #默認請求包頭信息的緩存 large_client_header_buffers 4 4k; #大請求包頭部信息的緩存個數與容量 # 壓縮處理 gzip on; #開啟壓縮 gzip_min_length 1k; #小文件不壓縮 gzip_comp_level 4; #壓縮比率 gzip_buffers 4 16k; #壓縮緩沖區大小,申請4個單位為16K的內存作為亞索結果流緩存 gzip_http_version 1.1 # 默認壓縮版本 #對特定文件壓縮,類型參考mime.types gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_vary on; gzip_disable "MSIE[1-6]\."; # 設置fastcgi fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m; # 為FastCGI緩存指定一個路徑,目錄結構等級,關鍵字區域存儲時間和非活動刪除時間 fastcgi_connect_timeout 300; # 指定連接到后端FastCGI的超時時間 fastcgi_send_timeout 300; # 向FastCGI傳送請求的超時時間,這個值是指已經完成兩次握手后向FastCGI傳送請求的超時時間 fastcgi_read_timeout 300; # 接收FastCGI應答的超時時間,這個值是指已經完成兩次握手后接收FastCGI應答的超時時間 fastcgi_buffer_size 16k; # 緩沖區大小 fastcgi_buffers 16 16k; fastcgi_busy_buffers_size 16k; fastcgi_temp_file_write_size 16k; # 在寫入fastcgi_temp_path時將用多大的數據塊,默認值是fastcgi_buffers的兩倍 fastcgi_cache TEST; # 開啟FastCGI緩存並且為其制定一個名稱 fastcgi_cache_valid 200 302 1h; fastcgi_cache_valid 301 1d; fastcgi_cache_valid any 1m; # 為指定的應答代碼指定緩存時間,上例中將200,302應答緩存一小時,301應答緩存1天,其他為1分鍾 fastcgi_cache_min_uses 1; # 5分鍾內某文件1次也沒有被使用,那么這個文件將被移除 fastcgi_cache_use_stale error timeout invalid_header http_500; # 內存緩存 open_file_cache max=2000 inactive=20s; #設置服務器最大緩存文件數量,關閉20秒內無請求的文件 open_file_cache_valid 60s; #文件句柄的有效時間是60秒,60秒后過期 open_file_cache_min_uses 5; #只有訪問次數超過5次會被緩存 open_file_cache_errors off; }
# 引入子配置文件 include vhost/*.conf # 限制客戶端請求的http方法 #if ($request_method !~ ^(GET|HEAD|POST)$ ) { # return 501; #} # 防止N多爬蟲代理訪問網站 if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot") { return 403; } } location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { expires 30d; #定義客戶端緩存時間為30天 }
nginx的upstream的負載均衡 目前支持的幾種方式
1.輪詢(默認)----每個請求按時間順序逐一分配到不同的后端服務器(down掉的服務器會被自動剔除)
upstream bakend { server 192.168.1.1; server 192.168.1.2; }
2.weight----指定輪詢幾率,weight與訪問成正比,可將性能好的服務器配置較大的weight(down掉的服務器會自動剔除)
upstream bakend { server 192.168.1.1 weight=1; server 192.168.1.2 weight=2; }
3.ip_hash----按請求的ip hash結果分配,這樣每個訪客固定訪問一個后端服務器,可解決session共享問題(down掉的服務器要手工處理)
upstream real_server{ ip_hash; server 192.168.1.1:8080 max_fails=3 fail_timeout=15s; server 192.168.1.2:8080 max_fails=3 fail_timeout=15s; }
4.fire(第3方插件)---按后端服務器的響應時間來分配請求,響應時間短的優先分配
upstream real_server{ server 192.168.1.1:8080; server 192.168.1.2:8080; fair; }
5.url_hash(第三方插件)---按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存服務器時有效
upstream real_server{ server 192.168.1.1:8080; server 192.168.1.2:8080; hash $request_uri; hash_method crc32; }
示例
client_body_buffer_size 128k; //允許客戶端請求緩存大小 client_max_body_size 100m; //允許請求的最大文件容量 large-client_header_buffers 4 8k; // proxy_buffering on; //開啟代理緩沖功能 proxy_buffer_size 8k; //第一部分響應數據的緩存大小 proxy_buffers 8 128k; //響應數據的緩存個數和容量 proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=one:100m inactive=1d max_size=2G; //設置緩存目錄,levels設置緩存個數,keys_zone定義緩存名字和容量,inactive定義緩存存活時間,max_size定義硬盤的緩存容量 proxy_connect_timeout 60s; //與后端服務器建立TCP連接握手超時時間 upstream servers { //ip_hash;ip_hash確保相同客戶端ip使用相同的后端服務器,不適用就默認輪詢 server 192.168.1.3:80 max_fails=3 fail_timeout=30s weight=2; server 192.168.1.4:80 max_fails=3 fail_timeout=30s weight=2; server 192.168.1.5:80 max_fails=3 fail_timeout=30s weight=2; } server { listen 80; server_name web.test.com; access_log logs/host.access.log main; # location ~* \.(mp3|mp4)$ { //匹配URL以MP3或者MP4結尾的請求 # proxy_pass http://localhost:8080 //轉發到本機8080端口 # } # location / { //匹配任意URL # proxy_pass http://localhost:8080 //轉發到本機8080端口 # proxy_set_header X-Forwarded-For $remote_addr //保留用戶真實IP # } location / { proxy_pass http://servers; proxy_cache one; proxy_set_header X-Forwarded-For $remote_addr; } }
nginx的rewrite規則
1.根據瀏覽器標識,訪問資源重定向到指定文件目錄(以下為IE)
if ($http_user_agent ~ MSIE ) {
rewrite ^(.*)$ /msie/$1 break;
}
2.將移動客戶端的請求重定向到其他服務器
if ($http_user_agent ~* '(iphone|ipod)' ) {
rewrite ^.+ http://mobile.site.com$uri;
}
3.用戶使用POST方式請求數據時候,返回405:
if ($request_method = POST ) { return 405; }
4.訪問xxxx時重定向到xxxx目錄
location /xxxx { rewrite ^/xxxx/.*$ /xxxx permanent; }
Nginx Rewrite的基本標記
last 基本上都用這個Flag。相當於Apache里的[L]標記,表示完成rewrite,不再匹配后面的規則 break 中止 Rewirte,不再繼續匹配 redirect 返回臨時重定向的HTTP狀態302 permanent 返回永久重定向的HTTP狀態301。原有的url支持正則,重寫的url不支持正則
正則表達式匹配
* ~ 區分大小寫匹配 * ~* 不區分大小寫匹配 * !~和!~* 區分大小寫不匹配及不區分大小寫不匹配
文件及目錄匹配
* -f 和!-f 用來判斷是否存在文件 * -d 和!-d 用來判斷是否存在目錄 * -e 和!-e 用來判斷是否存在文件或目錄 * -x 和!-x 用來判斷文件是否可執行
Nginx的一些可用的全局變量,可用做條件判斷:
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
if指令
1)一個變量名;false如果這個變量是空字符串或者以0開始的字符串; 2)使用= ,!= 比較的一個變量和字符串 3)是用~, ~*與正則表達式匹配的變量,如果這個正則表達式中包含},;則整個表達式需要用" 或' 包圍 4)使用-f ,!-f 檢查一個文件是否存在 5)使用-d, !-d 檢查一個目錄是否存在 6)使用-e ,!-e 檢查一個文件、目錄、符號鏈接是否存在 7)使用-x , !-x 檢查一個文件是否可執行
if ($http_user_agent ~ MSIE) { rewrite ^(.*)$ /msie/$1 break; } if ($http_cookie ~* "id=([^;]+)(?:;|$)") { set $id $1; } if ($request_method = POST) { return 405; } if ($slow) { limit_rate 10k; } if ($invalid_referer) { return 403; }
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
root /data/www/wwwroot/bbs;
expires 1d;
break;
}
}
ocation ~ ^/(images|JavaScript|js|css|flash|media|static)/ {
root /data/www/wwwroot/down;
expires 30d;
}