標籤:ngnix 高效能
互連網上講述nginx高並發配置的比較多,主要是nginx+php。本博講述nginx java web應用配置,實現高並發。配置關鍵點,見下文
配置cpu核心數
worker_processes 10;
worker_rlimit_nofile 102400;
配置epoll網路模型 worker_connections線程數,開到上萬
events { use epoll; worker_connections 10240;}
圖片資源cache
http{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 logs/access.log main;#控制緩衝區溢位攻擊client_body_buffer_size 1K;client_header_buffer_size 1k;client_max_body_size 1k;large_client_header_buffers 2 1k;##cache##proxy_connect_timeout 5;proxy_read_timeout 60;proxy_send_timeout 5;proxy_buffer_size 16k;proxy_buffers 4 64k;gzip_proxied any; proxy_busy_buffers_size 128k;proxy_temp_file_write_size 128k;proxy_temp_path /home/temp_dir;proxy_cache_path /home/cache levels=1:2 keys_zone=<span style="color:#ff0000;"><strong>cache_one</strong></span>:200m inactive=1d max_size=1g; #gzip#gzip on;gzip_vary on;gzip_min_length 1k;gzip_buffers 4 8k;gzip_comp_level 4;gzip_http_version 1.0; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;gzip_disable "MSIE [1-6]\.";}
#圖片前端緩衝
location ~ .*\.(jsp|do|action)?${# $server_port 可以不要,只有nginx的連接埠是非80情況下有效proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $remote_addr;proxy_pass http://tc;}location ~ .*\.(gif|jpg|png|htm|html|css|flv|ico|swf) {proxy_pass http://tc;proxy_redirect off;proxy_cache_key $host$uri$is_args$args;proxy_set_header Host $host;proxy_cache <span style="color:#ff0000;"><strong>cache_one</strong></span>;proxy_cache_valid 200 302 1h;proxy_cache_valid 301 1d;proxy_cache_valid any 1m;expires 30d;}location ~ .*\.(js)?${expires 1h;}
nginx負載平衡負載平衡策略較多,這裡主要使用三種 :ip_hash:Ip粘滯,對於web端沒有很成熟的會話複製方案,使用Ip粘滯。使用者登入後,會話保持在一台PC上。least_conn:最小串連數輪詢 :預設。按請求時間順序,分配給後端伺服器
其它策略fair:第三方,未研究。按後端伺服器的回應時間來分配請求,回應時間短的優先分配url_hash:第三方,未研究。linux作業系統
vi /etc/sysctl.conf增加或者修改參數
net.ipv4.tcp_timestamps = 0net.ipv4.tcp_synack_retries = 1net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.nf_conntrack_max = 655360net.netfilter.nf_conntrack_tcp_timeout_established = 1200
<p><span style="background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">kernel.shmall = 4294967296</span></p><p><span style="font-family: Arial, Helvetica, sans-serif; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">net.ipv4.tcp_max_tw_buckets =</span><span style="font-family: Arial, Helvetica, sans-serif; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">6</span><span style="font-family: Arial, Helvetica, sans-serif; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">000</span></p>
vi /etc/security/limits.conf
* soft nofile 1024000 * soft nproc 1024000 * hard nofile 1024000* hard nproc 1024000
nginx高效能java web應用幾個重要參數