更多Java技术文章,关注公众号,分享源码阅读笔记
1. Nginx/Tengine
Tengine是淘宝基于Nginx的魔改版,性能都很高,只是版本迭代较慢,Nginx比较快。本质还是Nginx。
1.1 安装
首先去淘宝官网或者Nginx官网下载,解压缩后
#安装必备依赖
yum install gcc openssl-devel pcre-devel zlib-devel -y
#设置安装路径,并初始化安装
./configure --prefix=/usr/local/tengin
#进行安装
make && make install
Tengine是需要编译安装的,所以需要手动添加service脚本
nginxd=/usr/local/nginx/sbin/nginx 修改成nginx安装路径的执行程序的路径。
nginx_config=/usr/local/nginx/conf/nginx.conf 修改成配置文件的路径。
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/tengine/sbin/nginx
nginx_config=/usr/local/tengine/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
保存脚本文件后设置文件的执行权限:
chmod a+x /etc/init.d/nginx.sh
上面的方法完成了用脚本管理nginx服务的功能,比如要设置nginx开机启动等。这时可以使用chkconfig来设置。
先将nginx服务加入chkconfig管理列表:
chkconfig --add /etc/init.d/nginx.sh
可以使用service控制nginx了
service nginx start
service nginx stop
service nginx restart
设置终端模式开机启动:
chkconfig nginx on
1.2 查看调试日志
安装的时候会出现各种问题,这个时候就可以通过log定位问题,文件在安装目录下的logs文件夹中,名称是error.log
# 动态查看最新的输出
tailog -f error.l
2. 配置文件详解
2.1 示例
user nginx;
#工作线程
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
#工作模式及连接数上限
events {
#epoll是多路复用IO(I/O Multiplexing)中的一种方式,
#仅用于linux2.6以上内核,可以大大提高nginx的性能
use epoll;
#单个后台worker process进程的最大并发链接数
worker_connections 1024;
# 并发总数是 worker_processes 和 worker_connections 的乘积
# 即 max_clients = worker_processes * worker_connections
# 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4 为什么
# 为什么上面反向代理要除以4,应该说是一个经验值
# 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000
# worker_connections 值的设置跟物理内存大小有关
# 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
# 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右
# 我们来看看360M内存的VPS可以打开的文件句柄数是多少: # $ cat /proc/sys/fs/file-max
# 输出 34336
# 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
# 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置
# 使得并发总数小于操作系统可以打开的最大文件数目
# 其实质也就是根据主机的物理CPU和内存进行配置
# 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。 # ulimit -SHn 65535
}
#定义轮询集群,可以定义多个,后面使用集群名称即可,例如下面的tomcats
upstream tomcats{
#以下轮询算法加上默认配合权重,共五种,加权轮询方式适用于每个请求所占用的后端时间基本相同,负载情况最好。常用于短连接服务,例如 HTTP 等服务。#ip_hash;
#url_hash;
#least_conn;
#least_time;
#weight是权重,如果不加权重,这没人轮一次,如果加了,按照权重大小分配流量
server 192.168.1.1:8080 weight=1;
#max_fail,只在这里重试2次,失败两次后会分配到其他服务器
#fail_timeout,意思是20秒内失败max_fail,配合使用
server 192.168.1.2:8080 weight=10 max_fail=2 fail_timeout=20;
}
http {
include /etc/nginx/mime.types;
default_type 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;
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
#对于普通应用,必须设为 on,
#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
#以平衡磁盘与网络I/O处理速度,降低系统的uptime.
#例如 当客户端出现图片加载了一般的时候,就需要关闭这个功能
#因为客户端是低性能的,所以服务端也要随之降低一点
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#开启压缩传输,但需要客户端解压缩算力,5G要来了,一般可以不开压缩
#响应速度是最重要的,解压缩会影响解压缩
#gzip on;
#屏蔽nginx版本号
server_tokens off;
##cache配置##
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path /home/temp_dir;
proxy_cache_path /home/cache levels=1:2keys_zone=cache_one:200m inactive=1d max_size=30g;
##cache end##
#包含自己各个网站的配置,可以根据网站配置不同的属性。 include /etc/nginx/conf.d/*.conf;
}
#xxx.conf,是在/etc/nginx/conf.d/下
#一个server表示一个主机
server {
listen 80;
listen [::]:80;
#域名
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
#虚拟映射目录,这里的“/”代表根,这里要注意空格
location / {
#设置根目录
root /usr/share/nginx/html;
#root相对路径下的文件
index index.html index.htm;
}
#定义一个前端项目位置
location /www {
#root只能有一个,所以需要alias来定位静态文件
alias /var/data/www
autoindex on;
}
location /service {
#方向代理上面定义的tomcats集群
proxy_pass http://tomcats;
}
#监控集群的状态
location /status {
check_status; #可以监控RS状态
access_log off;
allow all;
}
location~ .*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {
proxy_pass http://appserver ;
proxy_redirect off;
proxy_set_header Host $host;
proxy_cache cache_one;
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
#这里的过期时间要合理设置,这里设置为1天
expires 1d;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
2.2 session共享
第一步,项目中引入
spring-session
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
第二步,安装redis并配置spring-session
然后就完成了,不用在nginx处配置关于session的东西了。
2.3 限流削峰
2.3.1 IP连接限流算法
2.3.1.1 令牌桶算法
令牌以固定速率产生,并缓存到令牌桶中;令牌桶放满时,多余的令牌被丢弃;请求要消耗等比例的令牌才能被处理。
2.3.1.2 漏桶算法
水(请求)从上方倒入水桶,从水桶下方流出(被处理),来不及流出的水存在水桶中(缓冲),以固定速率流出,水桶满后水溢出(丢弃)。
2.3.2 限制单IP一个时间端内的建立连接请求数
2.3.3 限制IP访问的频率