study/linux/nginx.md

48 lines
1.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 进程数
Nginx 有 master 和 worker 两种进程master 进程用于管理 worker 进程worker 进程用于 Nginx 服务。
```sh
grep -c processor /proc/cpuinfo # 查看cpu核数
```
根据CPU核数设置worker_processes
```
worker_processes 4;
```
# 绑定 Nginx 进程到不同的 CPU 上
```
worker_processes 2; # 2核CPU的配置
worker_cpu_affinity 01 10;
worker_processes 4; # 4核CPU的配置
worker_cpu_affinity 0001 0010 0100 1000;
worker_processes 8; # 8核CPU的配置
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 1000000;
```
# 开启高效文件传输模式
```
http {
include mime.types;
default_type application/octet-stream;
sendfile on; # 开启文件的高效传输模式
tcp_nopush on; # 激活 TCP_CORK socket 选择
tcp_nodelay on; # 数据在传输的过程中不进缓存
}
```
# gzip
```
http {
gzip on; # 开启压缩功能
gzip_min_length 1k; # 允许压缩的对象的最小字节
gzip_buffers 4 32k; # 压缩缓冲区大小表示申请4个单位为32k的内存作为压缩结果的缓存
gzip_http_version 1.1; # 压缩版本用于设置识别HTTP协议版本
gzip_comp_level 9; # 压缩级别1级压缩比最小但处理速度最快9级压缩比最高但处理速度最慢
gzip_types text/plain application/x-javascript text/css application/xml; # 允许压缩的媒体类型
gzip_vary on; # 该选项可以让前端的缓存服务器缓存经过gzip压缩的页面例如用代理服务器缓存经过Nginx压缩的数据
}
```