通用配置
通用配置说明文档
整体参考
# nginx运行时使用www用户,默认是nginx
user www
# CPU核心数-1, 服务器的CPU核心数可用cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c查看,第一个数字就是核心数,该方法比较麻烦
# worker_processes 15;
# 针对cpu核树自动配置。nginx默认是没有开启利用多核cpu的配置的。需要通过增加worker_cpu_affinity配置参数来充分利用多核cpu
worker_processes auto;
# nginx cpu自动亲和配置
worker_cpu_affinity auto;
# nginx错误日志的目录和级别设置 debug, info, notice, warn, error, crit 默认为 crit
error_log logs/error20200220.log warn;
pid logs/nginx.pid;
# 一个nginx worker进程能打开的最多文件描述符,与ulimit -n 保持一致即可(ulimit -n应为65535)
worker_rlimit_nofile 65535;
events {
# nginx采用epoll事件模型,处理效率高
use epoll;
# 单个worker进程可以允许同时建立外部连接的数量,最大(worker_rlimit_nofile/worker_processes) 整体可打开文件数据量除以worker线程数量
worker_connections 10240;
}
http {
# 隐藏nginx版本信息,安全性考虑
server_tokens off;
include mime.types;
# 如果Web程序没设置,Nginx也没对应文件的扩展名,就用Nginx 里默认的 default_type定义的处理方式,application/octet-stream是nginx默认类型
default_type application/octet-stream;
# 统一使用utf-8字符集
charset utf-8;
# 日志格式
log_format main '[time:$request_time s] $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'$upstream_addr $upstream_response_time $request_time $upstream_status '
'"$http_range" "$sent_http_content_range"'
'"$gzip_ratio"'
'"$query_string"'
'"-http_refer:$http_referer"';
access_log /var/log/nginx/access.log main;
# 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的负载
sendfile on;
# 必须在 sendfile 开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量(将响应头和正文的开始部分一起发送,而不一个接一个的发送)。
tcp_nopush on
# 也是防止网络阻塞,不过要包涵在keepalive_timeout参数才有效
tcp_nodelay on
# nginx日志缓存,降低日志IO。
# max;文件描述符
# inactive:日志文件在缓存中多长时间未使用就取消
# min_uses: 在存活时间内日志被写入几次才会记录到缓存
open_log_file_cache max=10240 inactive=60s valid=1m min_uses=2;
# 限制请求体的大小,若超过所设定的大小,返回413错误,涉及到大文件上传时需要增加该值,一般100M已经可以满足项目使用
client_max_body_size 100m;
client_header_buffer_size 64k;
large_client_header_buffers 4 4k;
##压缩配置
gzip on;
gzip_min_length 2k;
gzip_buffers 4 16k;
gzip_comp_level 3;
gzip_vary on;
gzip_types text/plain application/x-javascript application/javascript application/css text/css application/xml application/json text/json;
##缓存配置
proxy_connect_timeout 3600s;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
proxy_buffer_size 512k;
proxy_buffers 64 512k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 512k;
## 当上游服务器的响应过大不能存储到配置的缓冲区域时,Nginx存储临时文件硬盘路径 ,设置为服务器上存在的目录
proxy_temp_path /data/nginx/cache_temp_path;
# 注意【cache_one】,后续的location会用到
proxy_cache_path /data/nginx/cache_path levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=10g use_temp_path=off;
# proxy_cache_key $host$request_uri;
client_body_buffer_size 10240k;
output_buffers 8 64k;
postpone_output 1460;
client_header_timeout 120s;
client_body_timeout 120s;
send_timeout 60s;
# 优化http连接 客户端连接保持会话超时时间,超过这个时间,服务器断开这个链接
keepalive_timeout 1000
;
# keepalive_requests指令用于设置一个keep-alive连接上可以服务的请求的最大数量,当最大请求数量达到时,连接被关闭。默认是100。
keepalive_requests 900;
##集群 失败5次后,将节点设置为不可用,等待600秒后,重置为有效状态
upstream cwbb {
## 如果nginx没有安装sticky模块,则注释
sticky name="hellosticky";
server 192.168.0.1:8080 max_fails=5 fail_timeout=600s weight=10;
server 192.168.0.2:8080 max_fails=5 fail_timeout=600s weight=10;
}
# 慎用proxy_next_upstream 模块
# proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
server {
# nginx监听端口
listen 80;
# nginx域名,如果客户没有提供域名,可以设置为nginx所在服务器的ip
server_name domain.name;
# 如果没有配置https证书,则listen 443 ssl; ssl_certificate; ssl_certificate_key; ssl_session_cache; ssl_session_timeout;都可以用#注释
listen 443 ssl;
ssl_certificate /data/nginx/cert/xxx.crt;
ssl_certificate_key /data/nginx/cert/xxx.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
##缓存静态文件,如果有其他静态文件缓存,可以按照|扩展名|的格式增加。【非静态资源的请求严禁缓存】
location ~* ^.+\.(jpg|jpeg|gif|png|js|)$ {
proxy_pass http://cwbb;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
proxy_cache off;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_buffer_size 128k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_cache_valid 200 304 302 24h;
proxy_cache_key $server_addr$uri$is_args$args;
add_header Cache-Control no-cache;
}
## 根目录访问 ,如果有其他需要代理的路径,则依次增加location即可
location / {
## 如果需要禁止不安全的请求类型,增加如下配置,GET|POST|HEAD是允许的请求类型
if ($request_method !~ ^(GET|POST|HEAD)$) {
return 403 '{"timestamp":"2020-02-20 00:00:00","success":false,"errorCode":"403","errorMessage":"不安全的请求类型:$request_method","errorDetail":"不安全的URL:$request_uri","data":null}';
}
proxy_pass http://cwbb;
limit_rate 400k;
limit_rate_after 5m;
proxy_connect_timeout 1200;
proxy_send_timeout 1200s;
proxy_read_timeout 1200s;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Cache-Control no-cache;
}
}
}