【WordPress 博客优化系列】nginx配置文件优化

本文针对的优化参数涵盖以下项目:

  1. Nginx运行工作进程数量

  2. Nginx最大打开文件数

  3. Nginx事件处理模型

  4. 开启高效传输模式

  5. 连接超时时间

  6. fastcgi 调优

  7. gzip 调优

  8. expires 缓存设置

  9. 关于系统连接数的优化

    ulimit -n    #执行此命令查看当前值,默认server只允许同时打开1024个文件
    ulimit -n 10240    #可临时调整为10240
  10. 一些默认参数的调整

下面是我配置好的文件,直接喂饭,已增加每个语句的中文释义,有其他参数配置需求的宝宝,请至官方文档:点击直达

```

user nginx; # 使用 nginx 用户运行

worker_processes auto; # 根据可用的 CPU 核心数量自动设置工作进程数

error_log /var/log/nginx/error.log notice; # 错误日志路径为 /var/log/nginx/error.log,日志级别为 notice
pid /var/run/nginx.pid; # PID 文件路径为 /var/run/nginx.pid

worker_rlimit_nofile 65535; # 每个工作进程允许打开的最大文件描述符数量为 65535

events {
worker_connections 65535; # 每个工作进程允许的最大连接数为 65535
}

http {
server_tokens off; # 关闭服务器版本信息的显示
fastcgi_hide_header X-Powered-By; # 隐藏 FastCGI 响应头中的 X-Powered-By
proxy_hide_header X-Powered-By; # 隐藏代理响应头中的 X-Powered-By

http3 on; # 开启 HTTP/3 支持
quic_gso on; # 开启 Generic Segmentation Offload(GSO)以提高 QUIC 性能
http3_max_concurrent_streams 256; # HTTP/3 最大并发流数设置为 256
http3_stream_buffer_size 128k; # HTTP/3 流缓冲区大小设置为 128KB
quic_active_connection_id_limit 4; # QUIC 活跃连接 ID 限制为 4

http2 on; # 开启 HTTP/2 支持
http2_max_concurrent_streams 256; # HTTP/2 最大并发流数设置为 256
http2_recv_buffer_size 512k; # HTTP/2 接收缓冲区大小设置为 512KB
http2_chunk_size 16k; # HTTP/2 分块大小设置为 16KB
keepalive_requests 2000; # 单个连接上允许的最大请求数为 2000

ssl_prefer_server_ciphers on; # 优先使用服务器端的密码套件
ssl_protocols TLSv1.3; # 仅允许使用 TLSv1.3 协议
ssl_session_cache shared:SSL:30m; # 共享 SSL 会话缓存,大小为 30 分钟
ssl_session_timeout 1h; # SSL 会话超时时间设置为 1 小时

ssl_session_tickets off; # 禁用 SSL 会话票据
ssl_buffer_size 32k; # SSL 缓冲区大小设置为 32KB

ssl_ciphers "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256"; # SSL 加密算法设置

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; # 添加严格的传输安全头
add_header X-Frame-Options "SAMEORIGIN"; # 设置 X-Frame-Options 头
add_header X-XSS-Protection "1; mode=block"; # 设置 X-XSS-Protection 头
add_header X-Content-Type-Options nosniff; # 设置 X-Content-Type-Options 头
add_header Referrer-Policy "no-referrer-when-downgrade"; # 设置 Referrer-Policy 头
add_header Permissions-Policy "geolocation=(), microphone=()"; # 设置 Permissions-Policy 头

server_names_hash_max_size 1024; # 服务器名称哈希表的最大大小为 1024
server_names_hash_bucket_size 128; # 服务器名称哈希桶的大小为 128
types_hash_max_size 2048; # 类型哈希表的最大大小为 2048
types_hash_bucket_size 128; # 类型哈希桶的大小为 128
variables_hash_max_size 1024; # 变量哈希表的最大大小为 1024
variables_hash_bucket_size 128; # 变量哈希桶的大小为 128

open_file_cache max=102400 inactive=20s; # 打开文件缓存设置
open_file_cache_valid 30s; # 打开文件缓存有效时间设置为 30 秒
open_file_cache_min_uses 1; # 打开文件缓存最小使用次数设置为 1
client_header_timeout 15; # 客户端请求头超时时间设置为 15 秒
client_body_timeout 15; # 客户端请求体超时时间设置为 15 秒
open_file_cache_errors off; # 关闭打开文件缓存的错误日志记录
reset_timedout_connection on; # 重置超时连接
send_timeout 15; # 发送超时时间设置为 15 秒
client_max_body_size 10m; # 客户端请求体最大大小设置为 10MB

limit_req_zone $binary_remote_addr zone=example_zone:100m rate=25r/s; # 请求速率限制设置
limit_req zone=example_zone burst=80 nodelay; # 请求速率限制参数设置
limit_rate 5m; # 限制响应速率为 5MB/s

fastcgi_connect_timeout 600; # FastCGI 连接超时时间设置为 600 秒
fastcgi_send_timeout 600; # FastCGI 发送超时时间设置为 600 秒
fastcgi_read_timeout 600; # FastCGI 读取超时时间设置为 600 秒
fastcgi_buffer_size 64k; # FastCGI 缓冲区大小设置为 64KB
fastcgi_buffers 4 64k; # FastCGI 缓冲区参数设置
fastcgi_busy_buffers_size 128k; # FastCGI 忙碌缓冲区大小设置为 128KB
fastcgi_temp_file_write_size 128k; # FastCGI 临时文件写入大小设置为 128KB
fastcgi_intercept_errors on; # 拦截 FastCGI 错误
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:128m max_size=10g inactive=180m; # FastCGI 缓存设置

proxy_buffering on; # 开启代理缓冲
proxy_buffers 32 16k; # 代理缓冲区设置
proxy_busy_buffers_size 64k; # 代理忙碌缓冲区大小设置为 64KB

include /etc/nginx/mime.types; # 包含 MIME 类型文件
default_type application/octet-stream; # 默认 MIME 类型设置为 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 /var/log/nginx/access.log main; # 访问日志路径为 /var/log/nginx/access.log,使用主日志格式

sendfile on; # 开启 sendfile 功能

tcp_nopush on; # 开启 TCP_NOPUSH 功能
tcp_nodelay on; # 开启 TCP_NODELAY 功能

keepalive_timeout 60; # keepalive 超时时间设置为 60 秒
client_header_buffer_size 4k; # 客户端请求头缓冲区大小设置为 4KB

expires 2h; # 设置过期时间为 2 小时

gzip on; # 开启 gzip 压缩
gzip_static on; # 开启预压缩
gzip_buffers 4 16k; # gzip 缓冲区设置
gzip_min_length 1k; # gzip 最小压缩文件大小设置为 1KB
gzip_comp_level 9; # gzip 压缩级别设置为 9
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml; # 需要压缩的 MIME 类型
client_body_buffer_size 128k; # 客户端请求体缓冲区大小设置为 128KB
gzip_http_version 1.1; # gzip 使用的 HTTP 版本设置为 1.1
gzip_vary on; # 根据 Accept-Encoding 头进行内容压缩
gzip_proxied any; # 允许任何代理请求进行压缩

output_buffers 4 32k; # 输出缓冲区设置

proxy_connect_timeout 60s; # 代理连接超时时间设置为 60 秒
proxy_send_timeout 60s; # 代理发送超时时间设置为 60 秒
proxy_read_timeout 60s; # 代理读取超时时间设置为 60 秒

include /etc/nginx/conf.d/*.conf; # 包含其他配置文件
}

```

暂无评论

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇