nginx作为web容器之性能优化
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx作为web容器之性能优化相关的知识,希望对你有一定的参考价值。
最近今日痴迷于nginx的高并发,看了很多博文来实现单纯的web容器的高并发以及作为代理的高并发性调优。为了测试学习,自己搭建了四台centos虚拟机。
IP:
1,192.168.2.210
2,192.168.2.220
3,192.168.2.230
4,192.168.2.240
首先,我单纯的用nginx作为web容器处理静态页面。看了大量别人的调优经验有Linux内核参数设置的,有nginx参数设置的,有php-fpm设置的,有修改nginx源码重新编译的。
第一,nginx参数设置
user www www; #worker进程的用户 worker_processes 8; #一般和CPU核数一致 worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000; #error_log /www/log/nginx_error.log crit; #为了得到更好的IO我是关闭日志的 pid /usr/local/nginx/nginx.pid; worker_rlimit_nofile 204800; events { use epoll; worker_connections 204800; } http { 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 4k; fastcgi_buffers 8 4k; fastcgi_busy_buffers_size 8k; fastcgi_temp_file_write_size 8k; 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 { listen 8080; server_name localhost; index index.php index.htm; root /www/html/; location /status { stub_status on; } location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ { 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; } }
在访问静态资源的时候,在worker_proccess和worker_connections设置正确的前提下,对性能提升最大的就是
open_file_cache max=204800 inactive=20s; open_file_cache_min_uses 1; open_file_cache_valid 30s;
这几条,缓存文件资源。我用ab测试性能爆炸式增长,原先发送1000个请求并发1000要10秒,加上后立马变0.4了。吞吐率从几十K变成了1M,我是虚拟机单核1G的配置哦。。。
那么访问PHP文件呢?
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 4k; fastcgi_buffers 8 4k; fastcgi_busy_buffers_size 8k; fastcgi_temp_file_write_size 8k; 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;
可以显著提高效率
还有Linux内核参数调节,主要是网络相关的
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
感觉这部分作用最小,几乎没有任何性能贡献,可能是我不理解这样设置的意义与自己的环境不匹配的原因吧。
总结:
提高nginx效率最有效的方法是开启各种缓存!!!!!!!!
以上是关于nginx作为web容器之性能优化的主要内容,如果未能解决你的问题,请参考以下文章