今天一个群里的小伙伴(福建-硕) 分享了nginx 的配置模板,写的挺详细的,这里记录一下:
nginx_conf.txt:
user root; #使用nginx用户启动进程
worker_processes 8; #这里一般为cpu核数
pid logs/nginx.pid; #pid文件存储路径
events {
worker_connections 102400; #最大连接数,根据服务器实际情况配置
use epoll; #复用客户端线程的轮询方法
}
http {
include mime.types; #默认配置,这里表示引用context-type的配置文件。如果需要添加新的类型,手动在此文件中添加即可。
default_type application/octet-stream; #默认context-type类型定义。
charset utf-8; #默认编码
keepalive_timeout 65; #会话超时时间
sendfile on; #开启调用linux的sendfile功能,提高文件传输效率
server_tokens off; #关闭nginx的版本显示
#以下标记蓝 {MOD}配置信息,请根据应用详情配置,过大会影响服务器性能,过小会导致应用异常。如果有不懂之处,请咨询后再配置。如果需要改动以下配置,请在server域重新声明即可,不要更改全局配置。
client_header_buffer_size 2k; #读取客户端请求头部信息的缓冲区大小,
large_client_header_buffers 4 16k; #读取客户端请求头部信息的缓冲区的最大数量和大小
client_max_body_size 8m; #设置客户端请求体最大值
proxy_read_timeout 300; #后端服务器读取超时
proxy_send_timeout 300; #后端服务器发送超时
proxy_buffer_size 16k; #后端response返回缓冲区大小
proxy_buffers 4 16k; #后端response返回缓冲区的最大数量和大小
proxy_busy_buffers_size 32k; #一般为proxy_buffers的两倍,限制最大缓存区大小,如果大于此值,将会依靠proxy_temp_path的路径对response信息进行存储
proxy_temp_file_write_size 32k; #大于32k的response信息,将会被存储到临时文件当中
proxy_temp_path temp/proxy_temp; #proxy的临时文件路径
proxy_next_upstream http_502 http_504 error timeout invalid_header;#定义后端在抛出500、502、504错误时,跳转到另一个可用服务器
proxy_cache_path temp/proxy_cache levels=1:2 keys_zone=cache_one:100m inactive=2d max_size=1g; #定义后端缓存文件的存放路路径以及缓存策略,请根据实际需要进行配置。
log_format main '$remote_addr - $remote_user [$time_local]"$request" '
‘$status $body_bytes_sent $http_referer ’
‘“$http_user_agent” “$http_x_forward_for” ’
‘$upstream_status $upstream_addr “$upstream_cache_status” ’
‘request_time $request_time upstream_response_time $upstream_response_time’; #定义日志存储内容,以此格式进行统一,如果以后有变动,统一进行调整
include conf.d/vhost/*.conf; #引用所有的虚拟主机文件
include conf.d/upstream/*.conf; #引用所有的upstream配置文件
}
upstream.txt:
upstream cmoc_svc_fj {
ip_hash;
server 10.48.153.25:8790;
server 10.48.153.25:8791;
server 10.48.153.25:8792;
server 10.48.153.25:8793;
check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
}
vhosts.txt:
#HTTP
server {
listen 8789;
server_name 10.48.153.25;
if ($request_method !~ ^(GET|POST|HEAD)$ ) {
return 444;
}
if ($time_iso8601 ~ "^(d{4})-(d{2})-(d{2})") {
set $year $1;
set $month $2;
set $day $3;
}
#gzip
include conf.d/gzip/comc_svc.conf;
location / {
root html;
index index.html;
proxy_pass http://cmoc_svc_fj;
proxy_set_header X-Forwarded-For $remote_addr;
access_log /home/nginx/nginx/logs/project-appname-access-$year-$month-$day.log main;
}
error_page 404 502 503 504 500 /50x.html;
location = /50x.html {
root html;
access_log /home/nginx/logs/error-$year-$month-$day.log;
}
}
#HTTPS
#server {
# listen 8443 ssl;
# server_name localhost;
# ssl_certificate conf.d/ssl/project_appname.crt;
# ssl_certificate conf.d/ssl/project_appname.key;
# ssl_session_cache shared:SSL:10m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# location / {
# proxy_pass http://project_appname;
# proxy_set_header X-Forwarded-For $remote_addr;
# access_log /home/nginx/nginx/logs/app_name-access-$year-$month-$year-$day.log main;
# }
#}