tinydu 2020-02-12

1-准备tomcat服务(8088端口)
2-开放防火墙8088端口
##添加8088端口[_0_4_centos bin]# firewall-cmd --add-port=8088/tcp --permanent success##重新加载配置 [_0_4_centos bin]# firewall-cmd --reload success##查看开放端口(去儿呢) [_0_4_centos bin]# firewall-cmd --list-all public target: default icmp-block-inversion: no interfaces: sources: services: ssh dhcpv6-client ports: 8081/tcp 8088/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
3-测试tomcat服务器


1-在/usr/local/nginx/conf/nginx.conf中加入如下配置: proxy_pass http://XX.XXX.176.26:8088;
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8081;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://XX.XXX.176.26:8088;
index index.html index.htm;
}
}2-重新加载nginx
[_0_4_centos conf]# cd /usr/local/nginx/sbin/ [_0_4_centos sbin]# ./nginx -s reload

1-两个tomcat服务器
2-开放防火墙8088 8089
3-测试tomcat服务器;

1-在/usr/local/nginx/conf/nginx.conf中加入如下配置(红色部分);并重新加载配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 8081;
server_name localhost;
location / {
root html;
proxy_pass http://XX.XXX.176.26:8088;
index index.html index.htm;
}
##加入新的http server块
server {
listen 8082;
location ~ /api/ {
proxy_pass http://XX.XXX.176.26:8088;
}
location ~ /admin/ {
proxy_pass http://XX.XXX.176.26:8089;
}
}
}2-开放防火墙8082端口

1、= :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
2、~:用于表示 uri 包含正则表达式,并且区分大小写。
3、~*:用于表示 uri 包含正则表达式,并且不区分大小写。
4、^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。
注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识
1-准备两个tomcat 8088和8089;分别都可以访问/api/a.html;

1-配置ngnix.conf文件
worker_processes 1;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
##定义负载均衡真实服务器IP:端口号 weight表示权重
upstream myserver{
server 49.233.176.26:8088 weight=1;
server 49.233.176.26:8089 weight=1;
}
server {
listen 8083;
location / {
proxy_pass http://myserver;
proxy_connect_timeout 10;
}
}
}
自定义负载均衡真实服务器信息 IP+端口号以及权重

第一种 轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
第二种 weight
weight 代表权重默认为 1,权重越高被分配的客户端越多
upstream server_pool{
server 192.168.5.21 weight=10;
server 192.168.5.22 weight=10;
}第三种 ip_hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器(可以解决session问题)
upstream server_pool{
ip_hash;
server 192.168.5.21:80;
server 192.168.5.22:80;
}
第四种 fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream server_pool{
server 192.168.5.21:80;
server 192.168.5.22:80;
fair;
}