谷歌在俄羅斯暫停 Play 商店支付功能,並建議關鍵的付費安卓 App 改為免費
nginx的模型
nginx採用epoll模型,這種模型是在linux核心2.6之後採用的,nginx採用epoll模型,非同步非阻塞,而Apache採用select模型
select特點:select選擇控制代碼的時候,是遍歷所有控制代碼,也就是說控制代碼有事件響應時,select需要遍歷所有控制代碼才能獲取到哪些控制代碼事件通知,因此效率非常低
epoll特點:epoll對於控制代碼事件的選擇不是遍歷的,是事件響應的,就是控制代碼上事件來就馬上選擇出來,不需要遍歷整個控制代碼連結串列,因此效率非常高
nginx工作模式
master-worker模式
單程序模式
nginx配置優化
worker_processes
表示啟動幾個工作程序,建議和本機CPU核數保持一致
worker_rlimit_nofile 65535
表示nginx程序開啟的最多檔案描述符數目,最好與ulimit -n的值保持一致
nginx處理事件模型
events {
use epoll;
worker_connections 65535;
multi_accept on;}
use epoll
使用epoll模式的事件驅動模型,該模型為Linux系統下最優方式
multi_accept on
使每個程序儘可能處理多個請求
worker_connections
單個後臺worker_connections程序的最大併發連線數
tcp_nodelay on
提高資料的實時響應性
tcp_nopush on
防止網路阻塞
nginx http伺服器優化
keepalive-timeout
定義長連線的超時時間,建議30s
client_max_body_size
允許客戶端請求的最大單檔案位元組數、
large_client_header_buffers 4 16k
指定客戶端請求中較大的訊息頭的快取最大數量和大小,4為個數;16k為最大快取4個4kb
client_header_buffer_size
客戶端請求頭部的緩衝區大小,建議設為4k
Client_header_timeout
設定客戶端請求讀取超過事件,如果超過這個時間,客戶端還沒有傳送任何資料nginx返回:requite time out
Client_body_timeout
用於設定客戶端請求主題讀取超過時間,預設值為60
send_timeout
指定響應客戶端的超時時間,這個超時僅限於兩個連線活動之前的時間,如果超過這個時間客戶端沒有任何獲得,nginx將會關閉連線
open_file_cache max=102400 inactive=20s
這個將為開啟檔案指定快取,預設是沒有啟用的,max指定快取數量,建議和開啟檔案數一致,inactive是指經過多長時間檔案沒被請求後刪除快取。
open_file_cache_valid 30s
這個是指多長時間檢查一次快取的有效資訊
open_file_cache_min_uses 1
open_file_cache指令中的inactive引數時間內檔案的最少使用次數,如果超過這個數字,檔案描述符一直是在快取中開啟的,如上例,如果有一個檔案在inactive時間內一次沒被使用,它將被移除
server_names_hash_bucket_size 128
伺服器名的hash庫大小
net.ipv4.tcp_max_tw_buckets = 6000
timewait的數量,預設是1024
net.ipv4.ip_local_port_range = 1024 65000
允許系統開啟埠的範圍
net.ipv4.tcp_tw_recycle = 1
開啟timew_wait sockes的快速回收,預設為0表示關閉
net.ipv4.tcp_tw_reuse = 1
開啟重用,允許timewait sockes重新用於新的tcp連線,預設為0表示關閉
net.ipv4.tcp_syncookies = 1
當出現SYN等待佇列溢位時,啟用cookies來處理,課防範少量SYN攻擊,預設為空表示關閉
net.core.netdev_max_backlog = 262144
每個網路介面接收資料包的速率比核心處理這些包的速率快時,允許送到佇列的資料包的最大數目
net.ipv4.tcp_fin_timeout = 1
如果套接字由本端要求關閉,這個引數決定了它保持在FIN-WAIT-2狀態的時間
net.ipv4.tcp_keepalive_time = 30
當keepalive起用的時候,TCP傳送keepalive訊息的頻度。預設是2小時。
net.ipv4.tcp_syn_retries = 1
在核心放棄建立連線之前傳送SYN包的數量。
一個完整的核心優化配置
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
View Code
一個簡單的nginx優化配置檔案
user www www;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000;
error_log /www/log/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 204800;
events
{undefined
use epoll;
worker_connections 204800;
}
http
{undefined
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
keys_zone=TEST:10m
inactive=5m;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 16k;
fastcgi_buffers 16 16k;
fastcgi_busy_buffers_size 16k;
fastcgi_temp_file_write_size 16k;
fastcgi_cache TEST;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server
{undefined
listen 8080;
server_name ad.test.com;
index index.php index.htm;
root /www/html/;
location /status
{undefined
stub_status on;
}
location ~ .*\.(php|php5)?$
{undefined
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
{undefined
expires 30d;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /www/log/access.log access;
}
}
nginx配置優化-httpgzip:
gzip on; //開啟gzip功能
gzip_min_length 1024; //設定請求資源超過該數值才進行壓縮,單位位元組
gzip_buffers 16 8k; //設定壓縮使用的buffer大小,第一個數字為數量,第二個為每個buffer的大小
gzip_comp_level 6; //設定壓縮級別,範圍1-9,9壓縮級別最高,也最耗費CPU資源
gzip_types text/plain application/x-javascript text/css application/xml image/jpeg image/gif image/png; //指定哪些型別的檔案需要壓縮
gzip_disable "MSIE 6\."; //IE6瀏覽器不啟用壓縮