tengine安装及配置 负载均衡,tengine负载均衡
tengine安装及配置 负载均衡,tengine负载均衡
一。安装过程
1 “下载
http://tengine.taobao.org/download.html 找到下载包并且下载(Tengine-2.2.0.tar.gz)
2 “解压
tar zxvf Tengine-2.2.0.tar.gz
3“配置检查
进入解压后的目录./configure
- 异常1 :
- [root @bogon tengine]#./configure
- 检查 OS
- + Linux 3.10 。0 - 327 .el7.x86_64 x86_64
- 检查 C编译器...未找到
- ./configure:错误:找不到C编译器cc
- 也就是c编译器gcc找不到
- 安装gcc yum -y安装gcc
- 继续检查
- 异常2 :
- 检查 PCRE库...未找到
- 检查 在/ usr /本地/ ...没有发现PCRE库
- 检查 PCRE库在/ usr / include中/ PCRE / ...未找到
- 检查 PCRE库在/ usr /包装/ ...未找到
- 检查 PCRE库在/ opt /本地/ ...未找到
- ./configure:错误:HTTP重写模块需要PCRE库。
- 您可以通过使用--without-http_rewrite_module来禁用该模块
- 缺少pcre和pcre-devel安装yum -y install pcre pcre-devel
- 这里安装好后可以通过
- Rpm -qa | grep pcre找到所有pcre的包
- Rpm -ql完整包名查看安装的路径
- 异常3 :
- 检查 OpenSSL库...未找到
- 缺少openssl和openssl-devel安装yum -y install openssl openssl-devel
- 继续检测成功可以同构日志看到需要pcre openssl zlib(安装openssl自动安装)的库
- 检查 PCRE库发现...
- 检查 PCRE JIT支持...发现
- 检查 OpenSSL库发现...
- 检查 zlib库发现...
- 创建objs / Makefile
4“安装
make && make install 安装完成安装目录为/ usr / local / nginx
找到 / usr / local / nginx / sbin / nginx -V 查看所有加载的模块
- [root @bogon sbin]#./nginx -V
- Tengine版本:Tengine / 2.2.0 (nginx的/ 1.8.1 )
- 由海湾合作委员会建立 4.8.5 20150623 (红帽 4.8.5 - 11 )(GCC)
- 已启用TLS SNI支持
- 配置参数:
- nginx:加载模块:
- nginx:ngx_core_module(静态)
- nginx:ngx_errlog_module(静态)
- nginx:ngx_conf_module(静态)
- nginx:ngx_dso_module(静态)
- nginx:ngx_events_module(静态)
/ usr / local / nginx / sbin / nginx开始|停止|重新启动和关闭ngix服务
访问http:// ip地址
5“nginx的命令参数
nginx -m显示所有加载的模块
nginx -l显示所有可以使用的指令
nginx -t检查nginx的配置文件是否正确
nginx -s启动nginx
nginx -s reload重启nginx
nginx -s stop停止nginx
6“开机启动nginx的
nigix官网上找到https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/查看linux下的启动脚本
进入官网www.nginx.com点击资源 - 社区维基
点击下界面的Ngix init Scipts
点击Red
hast nginx init脚本即可打开脚本
保存内容到/etc/init.d/nginx(服务的教程参考http://blog.csdn.net/liaomin416100569/article/details/72876349)
拷贝后需要修改其中的两个变量nginx安装目录的配置文件和nginx程序的目录
nginx = “/ usr / sbin / nginx”
prog = $( basename $ nginx )
NGINX_CONF_FILE = “/etc/nginx/nginx.conf”
修改为
nginx = “/ usr / local / nginx / sbin / nginx”
prog = $( basename $ nginx )
NGINX_CONF_FILE = “/ usr /local/ nginx / conf / nginx.conf ”
文件路径
注意nginx文件的内容是否完整拷贝有时拷贝缺少一些字符导致报错
服务nginx启动或者systemctl启动nginx
一般启动会卡住通过systemctl status nginx可以看到抛出错误
[java] 查看纯文本
- 6月的 08 版本03 :08 :55 bogon systemd [ 1 ]:PID文件/var/run/nginx.pid不可读开始(吗?)之后。
- 警告:nginx.service在磁盘上更改。运行 “systemctl守护进程重新加载” 重新加载单位。
修改CONF / nginx.conf文件
将#pid = logs / nginx.pid(可以看到pid默认生成在logs目录下)修改为pid = / var / run / nginx.pid即可去掉注释的#重启即可
添加到系统服务设置开机启动
[root @ bogon init.d]#chkconfig --add nginx[root @ bogon init.d]#chkconfig nginx on
使用http://192.168.58.131/访问看是否出现欢迎使用tengine的英文
注意nginx文件的内容是否完整拷贝有时拷贝缺少一些字符导致报错
服务nginx启动或者systemctl启动nginx
一般启动会卡住通过systemctl status nginx可以看到抛出错误
[java] 查看纯文本
- 6月的 08 版本03 :08 :55 bogon systemd [ 1 ]:PID文件/var/run/nginx.pid不可读开始(吗?)之后。
- 警告:nginx.service在磁盘上更改。运行 “systemctl守护进程重新加载” 重新加载单位。
修改CONF / nginx.conf文件
将#pid = logs / nginx.pid(可以看到pid默认生成在logs目录下)修改为pid = / var / run / nginx.pid即可去掉注释的#重启即可
添加到系统服务设置开机启动
[root @ bogon init.d]#chkconfig --add nginx[root @ bogon init.d]#chkconfig nginx on
使用http://192.168.58.131/访问看是否出现欢迎使用tengine的英文
二。配置文件
默认配置文件位于
/usr/local/nginx/conf/nginx.conf
1 “常用配置选项
- #user nobody;
- #工作的cpu的内核数默认应该是和当前电脑的cpu一致设置为auto自动获取当前机器
- worker_processes auto;
- #ngx_http_log_module模块功能
- #语法为error_log存储的日志文件路径[debug | 信息| 通知| 警告| 错误| 暴击]
- #error_log logs / error.log;
- #error_log logs / error.log notice;
- #error_log logs / error.log info;
- #定义存储nginx主进程ID的文件。nigix可能建立虚拟主机是会创建多个进程
- #pid logs / nginx.pid;
- #最顶层的指令events一般用于指定连接处理参数
- 事件{
- (数量包含所有连接(比如,和后端服务器建立的连接,还有其他的),而不仅仅是和客户端的连接)
- worker_connections 1024 ;
- }
- #加载编译为动态共享对象(DSO)的模块
- #
- #dso {
- #load ngx_http_fastcgi_module.so;
- #load ngx_http_rewrite_module.so;
- #}
- #配置HTTP服务器
- http {
- #包含其他语法正确的配置文件
- 包括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 logs / access.log main;
- #是否启用内核级别拷贝零拷贝速度快
- 发送文件;
- #tcp_nopush在;
- #长连接的超时是假呢
- #keepalive_timeout 0 ;
- keepalive_timeout 65 ;
- #是否启用gzip压缩压缩需要时间但是压缩后可以节省带宽
- #gzip on;
- #配置虚拟主机
- 服务器{
- #虚拟主机监听端口
- 听 80 ;
- #虚拟主机监听的IP(同一电脑可能多个IP)或者域名
- #添加虚拟ip ifconfig网卡名(可以通过ifconfig查看):1 192.168 。58.134 网络掩码 255.255 。255.0 以上
- #删除虚拟ip ifconfig网卡名(可以通过ifconfig查看):1 下
- server_name localhost;
- //当根路径访问时必须http:// localhost /自动进入根目录html下找index.html文件
- 位置 / {
- 根html;
- index index.html index.htm;
- }
- //出现以下错误状态码自动进入/50x.html
- error_page 500 502 503 504 /50x.html;
- //自动找html实际文件路径就是/html/50x.html
- location = /50x.html {
- 根html;
- }
- }
- }
2 “位置的路径匹配
该配置位于ngx_http_core_module模块(http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#location)
nginx的中位置的路径匹配规则
使用正则表达式需要在路径开始添加“〜*”前缀(不区分大小写),或者“〜”前缀(区分大小写)
匹配的路径以“^〜”开始,那么nginx不再检查后面的所有条件和正则表达式。
使用“=”前缀可以定义URI和路径的精确匹配。如果发现匹配,则终止路径查找
#这个表示路径精确就是/就匹配一个比如http:// localhost /
location = / {
[configuration A]
}
location / {
[configuration B]
}
location / documents / {
[configuration C]
}
location ^〜/ images / {
[configuration D]
}
location〜*。(gif | jpg | jpeg)$ {
[configuration E ]
}
请求“/”匹配配置A,请求“/index.html”匹配配置B,请求“/documents/document.html”匹配配置C,请求“/images/1.gif”匹配配置D,请求“/ documents / 1.jpg“匹配配置E. 2 ”位置其他配置
“ngx_http_access_module模块限制客户端访问ip(http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_access_module.html)
配置范例
location / {
deny 192.168.58.1;
允许全部;
}
这里192.168.58.1被禁止访问除了58.1其他都可以访问
58.1访问出现403被禁止其他ip都可以正常访问(linux访问可以使用wget地址或者curl地址)可以通过logs / access.log查看访问服务的客户端信息
注意nginx文件的内容是否完整拷贝有时拷贝缺少一些字符导致报错
服务nginx启动或者systemctl启动nginx
一般启动会卡住通过systemctl status nginx可以看到抛出错误
[java] 查看纯文本
- 6月的 08 版本03 :08 :55 bogon systemd [ 1 ]:PID文件/var/run/nginx.pid不可读开始(吗?)之后。
- 警告:nginx.service在磁盘上更改。运行 “systemctl守护进程重新加载” 重新加载单位。
修改CONF / nginx.conf文件
将#pid = logs / nginx.pid(可以看到pid默认生成在logs目录下)修改为pid = / var / run / nginx.pid即可去掉注释的#重启即可
添加到系统服务设置开机启动
[root @ bogon init.d]#chkconfig --add nginx[root @ bogon init.d]#chkconfig nginx on
使用http://192.168.58.131/访问看是否出现欢迎使用tengine的英文
#user nobody;
worker_processes 1;
#error_log logs/error.log; #日志信息 日志目录:cd /usr/loacl/nginx/logs
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024; #每秒最大允许连接的数量(最大连接数大约十万),如果超过就拒绝连接 tomcat最大2~3千
}
# load modules compiled as Dynamic Shared Object (DSO)
# 是阿里巴巴实现的 动态模块加载
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}
#处理http协议
http {
include 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"';
#记录用户的访问信息 能够分析网页的PV UV
#access_log logs/access.log main;
sendfile on; #流式传送
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #长链接
#gzip on;
#负载均衡 默认是轮询算法
upstream backend {
server 192.168.182.128:8080;
server 192.168.182.1:8080;
#检测后方tomcat是否存活
check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_http_send "HEAD / HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; #官方文档连接 http://tengine.taobao.org/document_cn/http_upstream_check_cn.html
interval
:向后端发送的健康检查包的间隔。fall
(fall_count): 如果连续失败次数达到fall_count,服务器就被认为是down。rise
(rise_count): 如果连续成功次数达到rise_count,服务器就被认为是up。timeout
: 后端健康请求的超时时间。default_down
: 设定初始时服务器的状态,如果是true,就说明默认是down的,如果是false,就是up的。默认值是true,- 也就是一开始服务器认为是不可用,要等健康检查包达到一定成功次数以后才会被认为是健康的。
type
:健康检查包的类型,现在支持以下多种类型tcp
:简单的tcp连接,如果连接成功,就说明后端正常。ssl_hello
:发送一个初始的SSL hello包并接受服务器的SSL hello包。http
:发送HTTP请求,通过后端的回复包的状态来判断后端是否存活。mysql
: 向mysql服务器连接,通过接收服务器的greeting包来判断后端是否存活。ajp
:向后端发送AJP协议的Cping包,通过接收Cpong包来判断后端是否存活。
port
: 指定后端服务器的检查端口。你可以指定不同于真实服务的后端服务器的端口,比如后端提供的是443端口的应用,- 你可以去检查80端口的状态来判断后端健康状况。默认是0,表示跟后端server提供真实服务的端口一样。该选项出现于Tengine-1.4.0。
} server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm;proxy_pass http://backend;
}
#/status路径就拦下来 location /status { check_status; access_log off; allow all; }#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 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; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}}
评论暂时关闭