## k8s node节点的nginx配置
用于多apiserver时候, node节点的nginx访问3台apiserver时候的配置. nignx是pod跑的,网络是-net模式
error_log stderr notice;
worker_processes auto;
events {
multi_accept on;
use epoll;
worker_connections 1024;
}
stream {
upstream kube_apiserver {
least_conn;
server 192.168.8.161:6443;
server 192.168.8.162:6443;
server 192.168.8.163:6443;
}
server {
listen 127.0.0.1:6443;
proxy_pass kube_apiserver;
proxy_timeout 10m;
proxy_connect_timeout 1s;
}
}
nginx编译安装:
1,安装依赖
yum install pcre pcre-devel openssl openssl-devel –y
2,
useradd -s /sbin/nologin -M nginx
3,编译安装
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.2 --with-http_stub_status_module --with-http_ssl_module
make && make install
echo $?
ln -s /application/nginx-1.6.2/ /application/nginx
3,检查
/application/nginx/sbin/nginx -t 检查语法
/application/nginx/sbin/nginx #启动
4,优化
echo PATH=/application/nginx/sbin/:$PATH >> /etc/profile
source /etc/profile
netstat -ntulp |grep nginx
lsof -i:80
curl 192.168.14.151
nginx -s stop
nginx -s reload
nginx配置
- web服务器
- 反向代理
- 缓存,类似squid
nginx配置
worker_processes 2;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
server {
listen 80;
server_name www.shizi.ml;
root /data/html/www;
index index.html index.php index.htm;
access_log logs/www_access.log;
}
server {
listen 80;
server_name bbs.shizi.ml;
root /data/html/bbs;
index index.html index.php index.htm;
access_log logs/bbs_access.log;
}
nginx日志切割-logrotate
cat > /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
daily
missingok
rotate 7
dateext
compress
delaycompress
notifempty
sharedscripts
postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
fi
endscript
}
nginx日志json格式
log_format json '{"@timestamp": "$time_iso8601",'
'"@version": "1",'
'"client": "$remote_addr",'
'"url": "$uri", '
'"status": $status, '
'"domain": "$host", '
'"host": "$server_addr",'
'"size":"$body_bytes_sent", '
'"response_time": $request_time, '
'"referer": "$http_referer", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"ua": "$http_user_agent" } ';
#access_log /var/log/nginx/lb_nginx-log-node11-52/node11-lb_nginx-access.log json;
nginx header参数
$arg_PARAMETER: 这个变量包含GET请求中,如果有变量PARAMETER时的值。
$args: 这个变量等于请求行中(GET请求)的参数,例如foo=123&bar=blahblah;
$binary_remote_addr: 二进制的客户地址。
$body_bytes_sent: 响应时送出的body字节数数量。即使连接中断,这个数据也是精确的。
$content_length: 请求头中的Content-length字段。
$content_type: 请求头中的Content-Type字段。
$cookie_COOKIE: cookie COOKIE变量的值
$document_root: 当前请求在root指令中指定的值。
$document_uri: 与$uri相同。
$host: 请求主机头字段,否则为服务器名称。
$hostname: #Set to the machine’s hostname as returned by gethostname
$http_HEADER:
$is_args: 如果有$args参数,这个变量等于”?”,否则等于””,空值。
$http_user_agent: 客户端agent信息
$http_cookie: 客户端cookie信息
$limit_rate: 这个变量可以限制连接速率。
$query_string: 与$args相同。
$request_body_file: 客户端请求主体信息的临时文件名。
$request_method: 客户端请求的动作,通常为GET或POST。
$remote_addr: 客户端的IP地址。
$remote_port: 客户端的端口。
$remote_user: 已经经过Auth Basic Module验证的用户名。
$request_completion: 如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty)。
$request_method: GET或POST
$request_filename: 当前请求的文件路径,由root或alias指令与URI请求生成。
$request_uri: 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”,不能修改。
$scheme: HTTP方法(如http,https)。
$server_protocol: 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr: 服务器地址,在完成一次系统调用后可以确定这个值。
$server_name: 服务器名称。
$server_port: 请求到达服务器的端口号。
$uri: 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。该值有可能和$request_uri不一致。$request_uri是浏览器发过来的值,该值是rewrite后的值。例如做了internal redirects后。
nginx日志切割-脚本
#!/bin/bash
# This script run at 00:00
LOG_PATH="/data/logs/nginx/"
DATE=`date -d "yesterday" +%Y-%m-%d`
mv ${LOG_PATH}/access.log ${LOG_PATH}/${DATE}.log
systemctl reload nginx
find $LOG_PATH -mtime +30 -exec rm -rf {} \;
nginx突破十万并发-配置
1,nginx配置-注释版:
worker_processes 8;
#nginx进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu计为8)。
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
#为每个进程分配cpu,上例中将8个进程分配到8个cpu,当然可以写多个,或者将一个进程分配到多个cpu。
worker_rlimit_nofile 65535;
#一个nginx进程打开的最多文件数目,理论值应该是最多打开文件数(ulimit -n)与nginx 进程数相除,但是nginx 分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。假如设置10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。
use epoll;
#使用epoll的I/O模型
#补充说明:
# 与apache相类,nginx针对不同的操作系统,有不同的事件模型
# - 标准事件模型
# Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
# - 高效事件模型
# Kqueue:使用于 FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X. 使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
# Epoll: 使用于Linux内核2.6版本及以后的系统。
# /dev/poll:使用于 Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
# Eventport:使用于 Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装安全补丁。
worker_connections 65535;
#每个进程允许的最多连接数, 理论上每台nginx 服务器的最大连接数为worker_processes*worker_connections。
keepalive_timeout 60;
#keepalive 超时时间。
client_header_buffer_size 4k;
#客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE 取得。
#但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。
open_file_cache max=65535 inactive=60s;
#这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。
open_file_cache_valid 80s;
#这个是指多长时间检查一次缓存的有效信息。
open_file_cache_min_uses 1;
#open_file_cache 指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如果有一个文件在inactive时间内一次没被使用,它将被移除。
nginx配置-无注释版
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_no