LNMP(Linux+Nginx+MySQL+PHP)是流行的Web服务架构,以下是提高网站访问速度的综合优化方案:
# 启用gzip压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1024;
gzip_comp_level 6;
# 静态文件缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
add_header Cache-Control "public, no-transform";
}
# 启用keepalive
keepalive_timeout 65;
keepalive_requests 100;
[mysqld]
innodb_buffer_pool_size = 总内存的50-70%
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2 (非关键数据可设为2)
innodb_flush_method = O_DIRECT
query_cache_size = 64M
table_open_cache = 4000
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
max_execution_time = 30
memory_limit = 128M
upload_max_filesize = 10M
post_max_size = 12M
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 500
通过以上综合优化,可以显著提高LNMP环境下网站的访问速度和整体性能。建议逐步实施并监控效果,根据实际负载情况调整参数。