nginx優化javascript
# 普通用戶啓動 (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的負載均衡 目前支持的幾種方式css
1.輪詢(默認)----每一個請求按時間順序逐一分配到不一樣的後端服務器(down掉的服務器會被自動剔除)java
upstream bakend { server 192.168.1.1; server 192.168.1.2; }
2.weight----指定輪詢概率,weight與訪問成正比,可將性能好的服務器配置較大的weight(down掉的服務器會自動剔除)node
upstream bakend { server 192.168.1.1 weight=1; server 192.168.1.2 weight=2; }
3.ip_hash----按請求的ip hash結果分配,這樣每一個訪客固定訪問一個後端服務器,可解決session共享問題(down掉的服務器要手工處理)nginx
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方插件)---按後端服務器的響應時間來分配請求,響應時間短的優先分配web
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; }
示例json
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;
}