UI視頻教程 2016-10-11 12:05:27 支持高并發(fā)web服務(wù)器搭建 一、搭建 服務(wù)器環(huán)境: 操作系統(tǒng):centos7,16G內(nèi)存,8核cpu 安裝軟件版本: ,php5.6,php-fpm,nginx1.8.1,openresty-1.9.3 安裝位置:/nginxLua openresty所在目錄:/nginxLua/openresty nginx所在目錄:/nginxLua/openresty/nginx nginx.conf所在目錄:/nginxLua/openresty/nginx/conf nginx啟動(dòng)項(xiàng)所在目錄:/nginxLua/openresty/nginx/sbin/nginx linux內(nèi)核參數(shù)配置項(xiàng)(sysctl.conf):/etc/sysctl.conf php-fpm.conf所在:/etc/php-fpm.conf php-fpm配置項(xiàng):/etc/php-fpm.d/www.conf web目錄:/web/html lua代碼目錄:/web/lua 什么是openresty? Openresty是一款nginx+lua的集成包,在目前相對(duì)lua擴(kuò)展不是很健全,openresty是一個(gè)很好的選擇,里面集成lua的基本模板 什么是lua? Lua詳細(xì)的可以百度,作用就是結(jié)合nginx可以實(shí)現(xiàn)非常高并發(fā)的接口,所以lua主要是用來(lái)寫(xiě)接口 什么是nginx? 一種類似apache的web服務(wù)器,強(qiáng)大的負(fù)載均衡和高并發(fā),epoll的高效處理模式是它的優(yōu)勢(shì),但其實(shí)他處理php的速度是跟apache不相上下的,但整體來(lái)說(shuō)效率還是比apache快很多,因?yàn)樗漠惒椒亲枞奶幚頇C(jī)制 編譯安裝openresty下載安裝包(ngx_openresty-1.9.3.1.tar.gz解壓密碼:0516) 把壓縮包拷到服務(wù)器上進(jìn)行解壓(創(chuàng)建一個(gè)文件夾openresty) tar -zxvf ngx_openresty-1.9.3.1.tar.gz -C /openresty 在編譯之前你需要安裝一些基本的依賴包 yum update yum -y install gcc gcc-c++ autoconf automake yum -y install zlib zlib-devel openssl openssl-devel pcre-devel readline-devel yum -y install make nginx相關(guān)命令: 開(kāi)啟:/nginxLua/openrety/nginx/sbin/nginx 關(guān)閉:/nginxLua/openrety/nginx/sbin/nginx -s stop 平滑關(guān)閉 /nginxLua/openrety/nginx/sbin/nginx -s quit 重新加載配置 /nginxLua/openrety/nginx/sbin/nginx -s reload 防火墻相關(guān)配置 firewall-cmd –permanent –query-port=9000/tcp 查看是否開(kāi)啟9000端口 firewall-cmd –permanent –add-port=9000/tcp 添加防火墻對(duì)9000端口開(kāi)放 systemctl start firewalld.service 開(kāi)啟防火墻 systemctl stop firewalld.service 禁止使用防火墻 firewall-cmd –reload 防火墻配置加載 php-fpm相關(guān)命令 /usr/sbin/php-fpm -c /etc/php.ini 啟動(dòng) kill -SIGUSR2 cat /run/php-fpm/php-fpm.pid 重啟 kill -SIGINT cat /run/php-fpm/php-fpm.pid 關(guān)閉 進(jìn)入openresty開(kāi)始編譯安裝 ./configure –prefix=/openresty –with-luajit –with-http_iconv_module –with-http_postgres_module make && make install 測(cè)試是否安裝成功 開(kāi)啟nginx: /openresty/openrety/nginx/sbin/nginx 修改配置文件:vi /openresty/openresty/conf/nginx.conf 在配置文件中的server中加一個(gè)location: location /lua { default_type ‘text/html’; content_by_lua‘ ngx.say(“hellow,word”) ’; } 開(kāi)啟nginx服務(wù):/openresty/openrety/nginx/sbin/nginx curl 127.0.0.1/lua 可以看到結(jié)果:hellow,word 說(shuō)明nginx加lua已經(jīng)編譯安裝好了 安裝php-5.6 添加yum源 CentOs 5.x rpm -Uvh http://mirror./yum/el5/latest.rpm CentOs 6.x rpm -Uvh http://mirror./yum/el6/latest.rpm CentOs 7.X rpm -Uvh https://mirror./yum/el7/epel-release.rpm rpm -Uvh https://mirror./yum/el7/webtatic-release.rpm 安裝php-5.6 yum -y install php56w.x86_64 php56w-cli.x86_64 php56w-common.x86_64 php56w-gd.x86_64 php56w-ldap.x86_64 php56w-mbstring.x86_64 php56w-MySQL.x86_64 php56w-pdo.x86_64 php56w-pear.noarch php56w-process.x86_64 php56w-xml.x86_64 php56w-xmlrpc.x86_64 安裝php56w-fpm (nginx編譯php文件的關(guān)鍵) yum install php56w-fpm -y 配置nginx支持編譯php文件(編輯nginx.conf) location ~ .php(.*){ root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+.php)(.*);//支持thinkphp路由規(guī)則的重要配置 fastcgi_param SCRIPT_FILENAME documentrootfastcgi_script_name; fastcgi_param PATH_INFOfastcgipathinfo;fastcgiparamPATHTRANSLATEDdocument_root$fastcgi_path_info; include fastcgi_params; } 啟動(dòng)nginx和php-fpm,測(cè)試輸出php文件是否有效 502 bad getway :很有可能就是你上面配置出現(xiàn)錯(cuò)誤了 php文件被下載了:說(shuō)明nginx不能編譯php文件,直接坐位普通文件下載了,要檢查配置文件是否做了相關(guān)的配置 二、支持高并發(fā)的配置優(yōu)化 優(yōu)化系統(tǒng)內(nèi)核 timewait 的數(shù)量,默認(rèn)是180000。但是多了會(huì)影響處理速度,少了無(wú)法最大化利用系統(tǒng)性能 net.ipv4.tcp_max_tw_buckets = 20000 允許系統(tǒng)打開(kāi)的端口范圍。不設(shè)置的話在高并發(fā)的情況下有可能把服務(wù)器的端口都占滿,那就徹底爆炸了 net.ipv4.ip_local_port_range = 10000 65000 啟用timewait 快速回收,這個(gè)必須要打開(kāi),很大程度的提高效率 net.ipv4.tcp_tw_recycle = 1 開(kāi)啟SYN Cookies,當(dāng)出現(xiàn)SYN 等待隊(duì)列溢出時(shí),啟用cookies 來(lái)處理。 net.ipv4.tcp_syncookies = 1 web 應(yīng)用中l(wèi)isten 函數(shù)的backlog 默認(rèn)會(huì)給我們內(nèi)核參數(shù)的net.core.somaxconn限制到128,而nginx 定義的NGX_LISTEN_BACKLOG 默認(rèn)為511,所以有必要調(diào)整這個(gè)值 net.core.somaxconn = 262144 每個(gè)網(wǎng)絡(luò)接口接收數(shù)據(jù)包的速率比內(nèi)核處理這些包的速率快時(shí),允許送到隊(duì)列的數(shù)據(jù)包的最大數(shù)目。 net.core.netdev_max_backlog = 262144 系統(tǒng)中最多有多少個(gè)TCP套接字不被關(guān)聯(lián)到任何一個(gè)用戶文件句柄上。如果超過(guò)這個(gè)數(shù)字,孤兒連接將即刻被復(fù)位并打印出警告信息。這個(gè)限制僅僅是為了防止簡(jiǎn)單的DoS攻擊,不能過(guò)分依靠它或者人為地減小這個(gè)值,更應(yīng)該增加這個(gè)值(如果增加了內(nèi)存之后)。 net.ipv4.tcp_max_orphans = 262144 記錄的那些尚未收到客戶端確認(rèn)信息的連接請(qǐng)求的最大值。對(duì)于有128M內(nèi)存的系統(tǒng)而言,缺省值是1024,小內(nèi)存的系統(tǒng)則是128。 net.ipv4.tcp_max_syn_backlog = 262144 時(shí)間戳可以避免序列號(hào)的卷繞。一個(gè)1Gbps的鏈路肯定會(huì)遇到以前用過(guò)的序列號(hào)。時(shí)間戳能夠讓內(nèi)核接受這種“異?!钡臄?shù)據(jù)包。這里需要將其關(guān)掉。 net.ipv4.tcp_timestamps = 0 為了打開(kāi)對(duì)端的連接,內(nèi)核需要發(fā)送一個(gè)SYN 并附帶一個(gè)回應(yīng)前面一個(gè)SYN的ACK。也就是所謂三次握手中的第二次握手。這個(gè)設(shè)置決定了內(nèi)核放棄連接之前發(fā)送SYN+ACK 包的數(shù)量。 net.ipv4.tcp_synack_retries = 1 在內(nèi)核放棄建立連接之前發(fā)送SYN 包的數(shù)量。 net.ipv4.tcp_syn_retries = 1 如果套接字由本端要求關(guān)閉,這個(gè)參數(shù)決定了它保持在FIN-WAIT-2狀態(tài)的時(shí)間。對(duì)端可以出錯(cuò)并永遠(yuǎn)不關(guān)閉連接,甚至意外當(dāng)機(jī)。缺省值是60 秒。2.2 內(nèi)核的通常值是180秒,3你可以按這個(gè)設(shè)置,但要記住的是,即使你的機(jī)器是一個(gè)輕載的WEB 服務(wù)器,也有因?yàn)榇罅康乃捞捉幼侄鴥?nèi)存溢出的風(fēng)險(xiǎn),F(xiàn)IN-WAIT-2 的危險(xiǎn)性比FIN-WAIT-1 要小,因?yàn)樗疃嘀荒艹缘?.5K 內(nèi)存,但是它們的生存期長(zhǎng)些。 net.ipv4.tcp_fin_timeout = 2 當(dāng)keepalive 起用的時(shí)候,TCP 發(fā)送keepalive 消息的頻度。缺省是2 小時(shí)。 net.ipv4.tcp_keepalive_time = 30 詳細(xì)的系統(tǒng)內(nèi)核參數(shù)配置 CTCDN系統(tǒng)優(yōu)化參數(shù) 關(guān)閉ipv6 net.ipv6.conf.all.disable_ipv6=1 net.ipv6.conf.default.disable_ipv6=1 避免放大攻擊 net.ipv4.icmp_echo_ignore_broadcasts=1 開(kāi)啟惡意icmp錯(cuò)誤消息保護(hù) net.ipv4.icmp_ignore_bogus_error_responses=1 關(guān)閉路由轉(zhuǎn)發(fā) net.ipv4.ip_forward=0 net.ipv4.conf.all.send_redirects=0 net.ipv4.conf.default.send_redirects=0 開(kāi)啟反向路徑過(guò)濾 net.ipv4.conf.all.rp_filter=1 net.ipv4.conf.default.rp_filter=1 處理無(wú)源路由的包 net.ipv4.conf.all.accept_source_route=0 net.ipv4.conf.default.accept_source_route=0 關(guān)閉sysrq功能 kernel.sysrq=0 core文件名中添加pid作為擴(kuò)展名 kernel.core_uses_pid=1 開(kāi)啟SYN洪水攻擊保護(hù) net.ipv4.tcp_syncookies=1 修改消息隊(duì)列長(zhǎng)度 kernel.msgmnb=65536 kernel.msgmax=65536 設(shè)置最大內(nèi)存共享段大小bytes kernel.shmmax=68719476736 kernel.shmall=4294967296 timewait的數(shù)量,默認(rèn)180000 net.ipv4.tcp_max_tw_buckets=20000 系統(tǒng)同時(shí)保持TIME_WAIT的最大數(shù)量,如果超過(guò)這個(gè)數(shù)字,TIME_WAIT將立刻被清除并打印警告信息。默認(rèn)為180000 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 每個(gè)網(wǎng)絡(luò)接口接收數(shù)據(jù)包的速率比內(nèi)核處理這些包的速率快時(shí),允許送到隊(duì)列的數(shù)據(jù)包的最大數(shù)目 net.core.netdev_max_backlog=662144 web 應(yīng)用中l(wèi)isten 函數(shù)的backlog 默認(rèn)會(huì)給我們內(nèi)核參數(shù)的net.core.somaxconn限制到128,而nginx 定義的NGX_LISTEN_BACKLOG 默認(rèn)為511,所以有必要調(diào)整這個(gè)值 net.core.somaxconn=662144 <h1 id="限制僅僅是為了防止簡(jiǎn)單的dos-攻擊">限制僅僅是為了防止簡(jiǎn)單的DoS 攻擊 net.ipv4.tcp_max_orphans=662144 未收到客戶端確認(rèn)信息的連接請(qǐng)求的最大值 可以容納更多等待連接的網(wǎng)絡(luò)連接數(shù)。 net.ipv4.tcp_max_syn_backlog=662144 net.ipv4.tcp_timestamps=0 內(nèi)核放棄建立連接之前發(fā)送SYNACK 包的數(shù)量 net.ipv4.tcp_synack_retries=1 內(nèi)核放棄建立連接之前發(fā)送SYN 包的數(shù)量 net.ipv4.tcp_syn_retries=1 啟用timewait 快速回收 net.ipv4.tcp_tw_recycle=1 開(kāi)啟重用。允許將TIME-WAIT sockets 重新用于新的TCP 連接 net.ipv4.tcp_tw_reuse=1 net.ipv4.tcp_mem=94500000 915000000 927000000 net.ipv4.tcp_fin_timeout=3 當(dāng)keepalive 起用的時(shí)候,TCP 發(fā)送keepalive 消息的頻度。缺省是2 小時(shí) net.ipv4.tcp_keepalive_time=30 允許系統(tǒng)打開(kāi)的端口范圍 net.ipv4.ip_local_port_range=10000 65000 修改防火墻表大小,默認(rèn)65536 net.netfilter.nf_conntrack_max=655350 net.netfilter.nf_conntrack_tcp_timeout_established=1200 確保無(wú)人能修改路由表 net.ipv4.conf.all.accept_redirects=0 net.ipv4.conf.default.accept_redirects=0 net.ipv4.conf.all.secure_redirects=0 net.ipv4.conf.default.secure_redirects=0 sysctl -p 使配置生效 系統(tǒng)連接數(shù)的優(yōu)化 查看當(dāng)前系統(tǒng)允許打開(kāi)的文件數(shù)(沒(méi)修改的話就是1024) ulimit -n 編輯修改/etc/security/limits.conf 在末尾添加如下代碼, soft noproc 65535 hard noproc 65535 soft nofile 65535 hard nofile 65535 修改系統(tǒng)運(yùn)行打開(kāi)的最大連接數(shù) ulimit -SHn 65535 nginx.conf的優(yōu)化配置 nginx 進(jìn)程數(shù),建議按照cpu 數(shù)目來(lái)指定,一般為它的倍數(shù) (如,2個(gè)四核的cpu計(jì)為8)。 worker_processes 8; 這個(gè)指令是指當(dāng)一個(gè)nginx 進(jìn)程打開(kāi)的最多文件描述符數(shù)目,理論值應(yīng)該是最多打開(kāi)文 件數(shù)(ulimit -n)與nginx 進(jìn)程數(shù)相除,但是nginx 分配請(qǐng)求并不是那么均勻,所以最好與ulimit -n的值保持一致。 現(xiàn)在在linux2.6內(nèi)核下開(kāi)啟文件打開(kāi)數(shù)為65535,worker_rlimit_nofile就相應(yīng)應(yīng)該填寫(xiě)65535。 worker_rlimit_nofile 65535; useepoll; 使用epoll 的I/O 模型 ( 補(bǔ)充說(shuō)明: 與apache相類,nginx針對(duì)不同的操作系統(tǒng),有不同的事件模型 A)標(biāo)準(zhǔn)事件模型 Select、poll屬于標(biāo)準(zhǔn)事件模型,如果當(dāng)前系統(tǒng)不存在更有效的方法,nginx會(huì)選擇select或poll B)高效事件模型 Kqueue:使用于 FreeBSD 4.1+, OpenBSD 2.9+, NetBSD2.0 和 MacOS X. 使用雙處理器的MacOS X系統(tǒng)使用kqueue可能會(huì)造成內(nèi)核崩潰。 Epoll: 使用于Linux內(nèi)核2.6版本及以后的系統(tǒng)。 /dev/poll:使用于 Solaris 7 11/99+, HP/UX 11.22+(eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。 Eventport:使用于 Solaris 10. 為了防止出現(xiàn)內(nèi)核崩潰的問(wèn)題,有必要安裝安全補(bǔ)丁。 ) 每個(gè)進(jìn)程允許的最多連接數(shù), 理論上每臺(tái)nginx服務(wù)器的最大連接數(shù)為worker_processes*worker_connections。 worker_connections 65535; keepalive 超時(shí)時(shí)間。 keepalive_timeout 60; 客戶端請(qǐng)求頭部的緩沖區(qū)大小,這個(gè)可以根據(jù)你的系統(tǒng)分頁(yè)大小來(lái)設(shè)置,一般一個(gè)請(qǐng)求頭的大小不會(huì)超過(guò)1k,不過(guò)由于一般系統(tǒng)分頁(yè)都要大于1k,所以這里設(shè)置為分頁(yè)大小。 client_header_buffer_size 4k; 7.這個(gè)將為打開(kāi)文件指定緩存,默認(rèn)是沒(méi)有啟用的,max 指定緩存數(shù)量,建議和打開(kāi)文件數(shù)一致,inactive是指經(jīng)過(guò)多長(zhǎng)時(shí)間文件沒(méi)被請(qǐng)求后刪除緩存。 open_file_cachemax=65535 inactive=60s; 這個(gè)是指多長(zhǎng)時(shí)間檢查一次緩存的有效信息。 open_file_cache_valid80s; open_file_cache 指令中的inactive參數(shù)時(shí)間內(nèi)文件的最少使用次數(shù),如果超過(guò)這個(gè)數(shù)字,文件描述符一直是在緩存中打開(kāi)的,如上例,如果有一個(gè)文件在inactive時(shí)間內(nèi)一次沒(méi)被使用,它將被移除。 open_file_cache_min_uses 1; 開(kāi)啟進(jìn)程復(fù)用 multi_accept on; 單個(gè)客戶端在 keep-alive 連接上可以發(fā)送的請(qǐng)求數(shù)量,在測(cè)試環(huán)境中,需要配置個(gè)比較大的值。 keepalive_requests 200000; gzip相關(guān)配置 gzip on; gzip on; gzip_min_length 5k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 4; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary on; 緩存配置 location ~ .*.(gif|jpg|jpeg|png|bmp|swf|js|css)$ { expires 30d; } 引入lua庫(kù) lua_package_path “/nginxLua/openresty/lualib/resty/?.lua;;”; 配置調(diào)用執(zhí)行ua代碼 例 location /redis { default_type ‘text/html’; content_by_lua_file /lua/redis.lua; } 優(yōu)化的nginx配置 user nobody; worker_processes 8; worker_rlimit_nofile 65535; error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; error_log off; pid logs/nginx.pid; events { use epoll; multi_accept on; #開(kāi)啟進(jìn)程復(fù)用 worker_connections 65535; } http { include mime.types; default_type application/octet-stream; #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; access_log off;#正式環(huán)境最好注釋掉 sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 0; keepalive_requests 200000;# 單個(gè)客戶端在 keep-alive 連接上可以發(fā)送的請(qǐng)求數(shù)量,在測(cè)試環(huán)境中,需要配置個(gè)比較大的值。 gzip on; #gzip on; gzip_min_length 5k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 4; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary on; lua_package_path "/nginxLua/openresty/lualib/resty/?.lua;;"; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location /redis { default_type 'text/html'; content_by_lua_file /lua/redis.lua; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # #error_page 500 502 503 504 /50x.html; #location = /50x.html { # root html; #} # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ .php(.*)$ { root html; #fastcgi_pass unix:/dev/shm/fpm-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+.php)(.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf|js|css)$ { expires 30d; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } php-fpm配置優(yōu)化 配置目錄:/etc/php-fpm.d/www.conf php-fpm初始/空閑/最大worker進(jìn)程數(shù) pm.max_children = 300 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 最大處理請(qǐng)求數(shù)是指一個(gè)php-fpm的worker進(jìn)程在處理多少個(gè)請(qǐng)求后就終止掉,master進(jìn)程會(huì)重新respawn一個(gè)新的。 這個(gè)配置的主要目的是避免php解釋器或程序引用的第三方庫(kù)造成的內(nèi)存泄露。 pm.max_requests = 10240 所有配置修改都要記得重啟服務(wù),確保配置加載 三、壓力測(cè)試 測(cè)試項(xiàng)目 worker_rlimit_nofile(最大可用文件描述符數(shù)量) worker_connections 單個(gè)進(jìn)程允許的最大連接數(shù) worker_processes 服務(wù)開(kāi)啟的進(jìn)程數(shù) keepalive_timeout自動(dòng)關(guān)閉連接時(shí)間 multi_accept是否開(kāi)啟進(jìn)程復(fù)用 gzip_comp_level keepalive_requests單個(gè)客戶端在 keep-alive 連接上可以發(fā)送的請(qǐng)求數(shù)量 測(cè)試數(shù)據(jù)(點(diǎn)擊可以下載): 參數(shù)設(shè)置: worker_rlimit_nofile worker_connections worker_processes keepalive_timeout multi_accept gzip_comp_level keepalive_requests 65535 65535 8 0 on 4 65535 |
|