Nginx怎么调优?

無名 发表于: 2021-07-27   最后更新时间: 2021-09-18 14:25:02   1,023 游览

Nginx怎么调优?

发表于 2021-07-27

1、worker_processes的进程数,数量要与CPU数量一致,通过lscpu查看

worker_processes 1;

2、worker process打开文件数的优化

worker_rlimit_nofile 65535;

2.1、优化了nginx的worker进程最多打开数量的参数之后,还需要优化系统内核参数(允许打开最多文件的参数)

临时配置:

ulimit -Hn 100000 
ulimit -Sn 100000

永久配置:

vim /etc/security/limits.conf

* soft nofile 100000
* hard nofile 100000

3、单个进程最大连接数的优化

events {
worker_connections 2048;
multi_accept on;
use epoll;
}

4、隐藏版本信息的优化

server_tokens off;

5、高效文件传输模式的

sendfile on;

tcp_nopush on;

tcp_nodelay on;

6、访问日志关闭的优化

access_log off;

7、超时时间的优化

keepalive_timeout 10; //设置客户端保持活动状态的超时时间

client_header_timeout 10; //客户端请求头读取超时时间

client_body_timeout 10; //客户端请求体读取超时时间

reset_timedout_connection on; //在客户端停止响应之后,允许服务器关闭连接,释放socket关联的内存

send_timeout 10; //指定客户端的超时时间,如果在10s内客户端没有任何响应,nginx会自动断开连接

8、gzip的优化

gzip on;//开启压缩

gzip_min_length 1000;//小文件不压缩

gzip_comp_level 6;//压缩比例

gzip_types text/plain text/css application/json application/x-javascript text/xml 
application/xml application/xml+rss text/javascript;//对指定文件类型进行压缩

9、缓存静态页面的优化 (文件句柄是打开文件的唯一标示)

open_file_cache max=100000 inactive=20s; // 设置服务器最大缓存10万个文件句柄,关闭20s内无请求的句柄
open_file_cache_valid 30s;  // 文件句柄的有效期为30s
open_file_cache_min_uses 2;  // 最少打开2次才会被缓存
open_file_cache_errors on;

关闭访问日志真的好吗?

你的答案

查看nginx相关的其他问题或提一个您自己的问题