网站性能优化笔记学习
当前应用的主要性能瓶颈应该都在数据库上。这边主要纪录下web服务器相关的性能优化。
包含三个方面:nginx
,php-fpm
,系统内核
Nginx优化
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes 8;//根据服务器cpu核数配置
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 65535;//打开文件数,使用ulimit -n 查看
error_log /var/log/nginx/error.log crit; //配置纪录错误日志级别
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
use epoll; //使用epoll模型
worker_connections 65535;
multi_accept on; //开启尽可能多接收请求
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server_names_hash_bucket_size 128;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;//关闭nginx版本号
client_max_body_size 10m;
client_body_timeout 20s;
client_header_timeout 20s;
client_body_buffer_size 256k;
send_timeout 10s;
keepalive_timeout 60s;
types_hash_max_size 2048;
client_header_buffer_size 4k;
large_client_header_buffers 2 1k;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /dev/shm;
open_file_cache max=1000 inactive=20s; //开启打开文件缓存
open_file_cache_valid 30s;
open_file_cache_min_uses 5;
open_file_cache_errors off;
gzip on; //开启gzip压缩
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
}
server 段的优化
upstream socktest {//轮询多个进程池
server unix:/dev/shm/php-cgi.sock; //sock写到内存里面,提升性能
server unix:/dev/shm/www-php-cgi.sock;
}
server {
access_log /www/logs/store.access.log main buffer=32k; //增加输出缓存
error_log /www/logs/store.error.log crit; //提升错误日志级别
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 1d; //设置静态文件过期时间
}
location ~ \.php {
#fastcgi_pass 127.0.0.1:9000; //默认配置
#fastcgi_pass unix:/dev/shm/php-cgi.sock;//单个配置写入内存
fastcgi_pass socktest; //使用负载沦陷
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
php-fpm进程池优化
在/etc/php-fpm.d/
中多增加几个进程池配置
; Start a new pool named 'www'.
[www] //进程池名称,默认为www,每个名称都不能一样
;listen = 127.0.0.1:9000 //默认以tcp方式监听
;listen = /www/sock/php-cgi.sock //以sock方式监听
listen = /dev/shm/php-cgi.sock //以sock方式监听,并且写入内存,注意要给权限
pm = dynamic //采用动态方式,根据请求动态开启进程
;pm = static //静态方式,任何时候都会开启你设置的进程数量
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
; Note: This value is mandatory.
pm.max_children = 50
; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
pm.max_requests = 1000
; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
request_terminate_timeout = 100s
; Set open file descriptor rlimit.
; Default Value: system defined value
rlimit_files = 65535 //打开文件句柄数
; Set listen(2) backlog.
; Default Value: 511 (-1 on FreeBSD and OpenBSD)
listen.backlog = 65535
系统内核参数优化
修改/etc/sysctl.conf
配置文件,先备份一份
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 = 65535
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 = 524288 699050 1048576 //内存配置要慎重,不能太大
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
配置完系统参数之后重启
sysctl -p