Nginx推荐配置
worker_processes 4; # worker进程数,推荐CPU核心数的1-2倍
worker_cpu_affinity 01 10 01 10; #2核cpu,开启4个进程,最多推荐配置8个,8以上性能提升不会再提升了
pid /usr/local/nginx/logs/nginx.pid;
events {
worker_connections 51200; # nginx默认连接为51200
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
# 指定响应客户端的超时时间。这个超过仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。
send_timeout 600;
# 设置客户端连接保持会话的超时时间,超过这个时间,服务器会关闭该连接。
keepalive_timeout 65;
# 关闭nginx版本号
server_tokens off;
# 关闭列表目录,没有默认的index文件,会自动展示目录下的文件列表
autoindex off;
#gzip on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_types image/gif image/png application/javascript text/plain application/x-javascript text/css text/javascript application/xml;
gzip_vary on;
#设置客户端请求头读取超时时间,超过这个时间还没有发送任何数据,Nginx将返回“Request time out(408)”错误
client_header_timeout 120s;
#设置客户端请求主体读取超时时间,超过这个时间还没有发送任何数据,Nginx将返回“Request time out(408)”错误
client_body_timeout 120s;
#上传文件大小限制
client_max_body_size 10m;
client_body_buffer_size 256k;
# proxy_temp_path /xxxx/nginx_temp; # 代理临时目录
# 缓存目录和参数设置,第一个路径表示自定义的缓存文件保存目录
# level 参数边表示缓存目录下的层次目录结构
# key_zone指定缓存区名称和大小
# nactive 清空在指定时间内未被访问的内容
#proxy_cache_path /xxxx/nginx_cache levels=1:2 keys_zone=nginxCache:10m max_size=10g inactive=7d use_temp_path=off; proxy_cache_path
limit_conn_zone $binary_remote_addr zone=20_conn:10m; #用户并发连接数限制,服务器限流配置
# limit_conn_log_level error;
limit_conn_status 503;
# 单个ip限流,限制单个ip的请求数,每秒只处理10个请求,超过拒绝
limit_req_zone $binary_remote_addr zone=10_req:10m rate=10r/s;
# limit_red_log_level error;
limit_req_status 503;
server {
listen 80;
server_name xxx.baidu.com; # 域名地址
limit_conn 20_conn 20; # 限制用户并发数
#limit_rate_after 80M; # 指定在发送多少数据后就进行限速
#limit_rate 600k; # limit_rate指定每秒返回的字节数
limit_req zone=10_req burst=10; # 限制单个ip的请求数
location ^~ /api/ {
proxy_pass http://localhost:8080; # 后端请求地址
proxy_connect_timeout 500s;
proxy_read_timeout 500s;
proxy_send_timeout 500s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root /xxxx/; # 前端根目录
index index.html index.htm;
}
}
}