作者:农村小姑娘0 | 来源:互联网 | 2023-06-25 10:35
Day56Nginx负载均衡-下1.1负载均衡 (http层OSI七层)协议 https (TCP层OSI四层)端口http{ upstreamname{
Day 56 Nginx负载均衡-下
1.1 负载均衡
(http层 OSI七层) 协议 https
(TCP层 OSI四层) 端口
http{
upstream name {
ip_hash;
server 172.16.1.7 weight=1;
server 172.16.1.8 max_fail=2 fail_timeout=10s max_cOnns=1000;
}
}
proxy_pass http://name;
1.2 session 会话(服务端)
代码默认写的session存储在WEB服务器本地,解决session会话
会话保持 -> ip_hash
会话共享 -> redis、MySQL
存客户端 -> 浏览器
COOKIE(客户端)
1.3 负载均衡
F5 硬件
LVS 软件
Haproxy 支持四层和七层
Nginx 仅支持7层
1.4 如何配置Nginx4层负载均衡
通过访问负载均衡的5555端口,实际是后端的web01的22端口在提供服务。
通过访问负载均衡的6666端口,实际是后端的mysql的3306端口在提供服务。
The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream configuration parameter.
worker_processes auto;
error_log /var/log/nginx/error.log info;
events {
worker_connections 1024;
}
stream {
upstream backend {
hash $remote_addr consistent;
server backend1.example.com:12345 weight=5;
server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
}
server {
listen 12345;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass backend;
}
}
[root@lb01 ~]# mkdir -p /etc/nginx/conf.c
[root@lb01 ~]# vim /etc/nginx/nginx.conf
----在events层下面,http层上面
include /etc/nginx/conf.c/*.conf;
[root@lb01 ~]# cd /etc/nginx/conf.c/
[root@lb01 conf.c]# vim stream.conf
[root@lb01 conf.c]# cat stream.conf
stream {
#1.定义虚拟资源池
upstream ssh {
server 172.16.1.7:22;
}
upstream mysql {
server 172.16.1.51:3306;
}
#2.调用虚拟资源池
server {
listen 5555;
proxy_pass ssh;
}
server {
listen 6666;
proxy_pass mysql;
}
}
[root@lb01 conf.c]# systemctl restart nginx
flag-------------------------------
系统 服务 地址
CentOS7.5 proxy 10.0.0.5
CentOS7.5 Nginx 10.0.0.7
CentOS7.5 TOmcat 10.0.0.7
1.4.1 在10.0.0.7准备静态资源
[root@web01 conf.d]# cat images.conf
server{
listen 80;
server_name ds.oldboy.com;
root /soft/code;
index index.html;
location ~* .*\.(png|jpg|gif)$ {
root /soft/code/images;
}
}
1.4.2 准备目录, 以及静态相关图片
[root@web01 ~ ]# mkdir -p /soft/code/images/
[root@web01 ~ ]# wget -O /soft/code/images/nginx.png http://nginx.org/nginx.png
[root@web01 ~ ]# systemctl restart nginx
1.4.3 在10.0.0.7准备java动态资源,记得移除本地的8080端口
[root@web01 ~]# yum install -y tomcat
[root@web01 ~]# systemctl start tomcat
[root@web01 ~]# mkdir /usr/share/tomcat/webapps/ROOT
[root@web01 ~]# vim /usr/share/tomcat/webapps/ROOT/java_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
Random rand = new Random();
out.println("Random number:");
out.println(rand.nextInt(99)+100);
%>
1.4.4 配置proxy服务,10.0.0.5,在192.168.69.112配置负载均衡代理调度, 实现访问jsp和png
[root@lb01 conf.d]# cat ds_proxy.conf
upstream static {
server 10.0.0.7:80;
}
upstream java {
server 10.0.0.7:8080;
}
server {
listen 80;
server_name ds.oldboy.com;
location / {
root /soft/code;
index index.html;
}
location ~ .*\.(png|jpg|gif)$ {
proxy_pass http://static;
include proxy_params;
}
location ~ .*\.jsp$ {
proxy_pass http://java;
include proxy_params;
}
}
[root@lb01 conf.d]# systemctl restart nginx
1.4.5 配置Hosts文件
1.4.6 在负载均衡上面添加一个html整合动态资源和静态资源
[root@lb01 ~]# mkdir /soft/code -p
[root@lb01 ~]# cat /soft/code/index.html
测试动静分离
[root@lb01 ~]# systemctl restart nginx