支持高并发Web服务器搭建(4)

open_file_cache_valid80s;

open_file_cache 指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。

open_file_cache_min_uses 1;

开启进程复用

multi_accept on;

单个客户端在 keep-alive 连接上可以发送的请求数量,在测试环境中,需要配置个比较大的值。

keepalive_requests 200000;

gzip相关配置

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库

lua_package_path “/nginxLua/openresty/lualib/resty/?.lua;;”;

配置调用执行ua代码


location /redis {
default_type ‘text/html’;
content_by_lua_file /lua/redis.lua;
}

优化的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; #开启进程复用
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;#正式环境最好注释掉 sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 0; keepalive_requests 200000;# 单个客户端在 keep-alive 连接上可以发送的请求数量,在测试环境中,需要配置个比较大的值。 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 ; #} # 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配置优化 配置目录:/etc/php-fpm.d/www.conf php-fpm初始/空闲/最大worker进程数

pm.max_children = 300
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

最大处理请求数是指一个php-fpm的worker进程在处理多少个请求后就终止掉,master进程会重新respawn一个新的。 这个配置的主要目的是避免php解释器或程序引用的第三方库造成的内存泄露。

pm.max_requests = 10240

所有配置修改都要记得重启服务,确保配置加载
三、压力测试

测试项目 worker_rlimit_nofile(最大可用文件描述符数量) worker_connections 单个进程允许的最大连接数 worker_processes 服务开启的进程数 keepalive_timeout自动关闭连接时间 multi_accept是否开启进程复用 gzip_comp_level keepalive_requests单个客户端在 keep-alive 连接上可以发送的请求数量 测试数据(点击可以下载): 参数设置:

worker_rlimit_nofile worker_connections worker_processes keepalive_timeout multi_accept gzip_comp_level keepalive_requests
65535 65535 8 0 on 4 65535

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/6d4e17837bbef8ae38eea5b58957c7e7.html