nginx负载socket/tcp/http协议
nginx下载和安装
(安装nginx服务为192.168.100.100)
- 下载稳定版nginx
wget “http://nginx.org/download/nginx-1.16.1.tar.gz” - 安装编译环境
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel - 编译安装nginx
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1/
./configure --prefix=/opt/nginx/nginx --sbin-path=/opt/nginx/sbin/nginx --conf-path=/opt/nginx/conf/nginx.conf --with-http_stub_status_module --with-http_gzip_static_module --with-stream
make&&make install - 启动nginx
/opt/nginx/sbin/nginx -s reroad
nginx负载http服务(本次以nginx+tomcat+tomcat进行演示)
(前提是在101和102两台服务器上启动号tomcat服务)
…
http {
upstream tomcatDemo {
server http://192.168.100.101:8080;
server http://192.168.100.102:8080;
}
server {
listen 8080
location / {
proxy_pass tomcatDemo; #请求转向tomcatDemo 定义的服务器列表
}
}
…
测试:访问192.168.100.100:8080即可成功访问101或102中的tomcat页面,分别停止任一tomcat都能正常打开页面,但是两台同时停止就不能正常访问。
nginx负载socket/TCP协议服务
…
stream {
upstream tomcatDemo {
server 192.168.100.101:8090;
server 192.168.100.102:8090;
}
server {
listen 8090
location / {
proxy_connect_timeout 1s;
proxy_timeout 5s;
proxy_pass backend;
}
}
…
测试:调用192.168.100.100:8090即可成功调用101或102中的tcp服务,分别停止任一服务都能正常调用,但是两台同时停止就不能正常调用。
nginx他们参数介绍
##热备(当101出现异常才会调用102)
upstream tomcatDemo {
server 192.168.100.101:8090;
server 192.168.100.102:8090 backup;
}
##轮询(默认的轮询方式是ABABABABABABAB…)
upstream tomcatDemo {
server 192.168.100.101:8090;
server 192.168.100.102:8090;
}
##加权轮询(ABBABBABBABBABBABB…)
upstream tomcatDemo {
server 192.168.100.101:8090 weight=1;
server 192.168.100.102:8090 weight=2;
}
##ip_hash:nginx会让相同的客户端ip请求相同的服务器。
upstream tomcatDemo {
server 192.168.100.101:8090;
server 192.168.100.102:8090;
ip_hash;
}