基于nginx的高可用web集群

本文详细介绍了基于nginx搭建高可用web集群的过程,包括编译下载、配置解析、环境搭建、错误处理、IO多路复用、HTTPS服务部署、负载均衡原理与实践、DNS解析等内容。讲解了nginx作为反向代理和负载均衡器的角色,以及如何通过keepalived实现高可用。此外,还涉及了服务器配置、健康检查、调度算法和调优技巧,以及自定义负载均衡器的实现。
摘要由CSDN通过智能技术生成

一、写在前面

nginx是十分轻量级的HTTP服务器。是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器。

了解nginx的基本操作
nginx一键安装脚本:https://blog.csdn.net/m0_59371732/article/details/126162978?spm=1001.2014.3001.5501
按步骤操作如下:

编译下载
curl -O http://nginx.org/download/nginx-1.22.0.tar.gz  下载

​ 将已经编译好的二进制程序安装到指定的路径 make install

nginx.conf文件解析
  • 工作进程的数量,与cpu数量相同即可,使用top命令按1查看
#user  nobody;
worker_processes  2;
  • 错误日志的地址
#error_log  logs/error.log;
error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

  • 指定安装路径
  --prefix=PATH         set installation prefix

  • 建立长连接(http模块)
  keepalive_timeout  65;
  • 网站首页位置,/代表当前
  location / {
   
            root   html;
            index  index.html index.htm;
        }

  • 运行改代码 添加需要的模块等
./configure --prefix=/usr/local/jay-zhou99  --user=jay-zhou --group=jay-zhou 
 --with-http_ssl_module   --with-threads  --with-http_v2_module  
 --with-http_stub_status_module  --with-stream --with-http_gunzip_module

安装好后文件显示

[root@VM-12-8-centos nginx-1.22.0]# cd /usr/local/jay-zhou99/
[root@VM-12-8-centos jay-zhou99]# ls
conf  html  logs  sbin
安装好后的操作
conf  存放nginx的配置的文件

html   存放网站的网页的目录

logs   存放日志

sbin   存放nginx的启动程序的

```shell
[root@VM-12-8-centos html]# ls
50x.html  index.html

index.html 首页文件 :进入某个网站看到的第一个页面

链接:通过首页里的链接跳到其他的页面

修改环境变量 可以在任何地方使用nginx 命令直接启动

[root@VM-12-8-centos sbin]# PATH=/usr/local/jay-zhou99/sbin/:$PATH

把这个脚本的变量放到当前终端运行,不会去产生一个子进程

source  /root/.bashrc

停掉nginx进程

[root@VM-12-8-centos ~]# ps aux|grep nginx
或者使用kill  -9  ,先杀死master的那个nginx进程
  • 防火墙的设置

立即关闭防火墙,只是暂时

[root@VM-12-8-centos nginx_d]# service firewalld stop

永久关闭防火墙,下次开机时生效

[root@VM-12-8-centos nginx_d]# systemctl disable firewalld

查看防火墙状态

service filewalld status
日志问题,不同级别的错误

0最高

loglevel
The kernel log levels are:
0 (KERN_EMERG)
The system is unusable.
1 (KERN_ALERT)
Actions that must be taken care of immediately.
2 (KERN_CRIT)
Critical conditions.
3 (KERN_ERR)
Noncritical error conditions.
4 (KERN_WARNING)
Warning conditions that should be taken care of.
5 (KERN_NOTICE)
Normal, but significant events.
6 (KERN_INFO)
Informational messages that require no action.
7 (KERN_DEBUG)  调试模式: 程序执行的整个过程都记录,方便开发者调试程序

Kernel debugging messages, output by the kernel if the developer enabled debugging at compile time.
==

0 EMERG.  emergency 紧急  --》系统不能使用了
1 ALERT.  告警
2 CRIT.   严重 Critical
3 ERR.     程序因为某个错误导致不能运行,只要是error以上的级别都会导致程序启动或者是运行失败
4 WARNING. 警告 ,不会影响程序的正常运行,只是提醒而已 
5 NOTICE.  正常的记录,有点影响力的事件
6 INFO.    普通的信息都记录
7 DEBUG.  调试模式,什么都记录

二、环境搭建学习

web服务器配置

(本章是在云服务器上学习用的)
1、在配置文件里修改配置,新建3个虚拟主机

nginx.conf 新添加的server

一个server对应一个网站 -----》虚拟主机 (只截取了一个server)

server{
   
            listen 80;
            server_name www.liang.com;
            location / {
   
                    root html/liang;
                    index index.html index.htm shouye.html #首页文件优先级,先从做左边
            }       
            
            access_log  logs/liang_access.log  main; #访问日志
            error_log logs/liang_error.log notice; #错误日志 
}

2、添加好后 新建各个虚拟主机对应的网页根目录,并且新建首页

[root@VM-12-8-centos jay-zhou99]# cd html
[root@VM-12-8-centos html]# pwd
/usr/local/jay-zhou99/html
[root@VM-12-8-centos html]# 
[root@VM-12-8-centos html]# tree
.
|-- 50x.html
|-- hejin
|   `-- he.html
|-- index.html
|-- jay.jpg
|-- liang
|   `-- index.html    在index.html 中输入想在网页上看到的内容
|-- lili
|   `-- index.html
`-- tian
    `-- index.html

3、重启nginx服务

nginx -s reload

4、域名解析

​ 拿我们的nginx服务器做客户,修改/etc/hosts 添加对应的域名

写入/etc/hosts 文件

1.14.183.210 www.liang.com
1.14.183.210 www.tian.com
1.14.183.210 www.lili.com

5、测试访问

访问的域名不一样,然会的内容也不一样

[root@VM-12-8-centos html]# curl www.lili.com
liyili cool
[root@VM-12-8-centos html]# curl www.liang.com
liangrui bingo    
[root@VM-12-8-centos html]# 

(在windows里改hosts文件

		C:\Windows\System32\drivers\etc

改不了的话把hosts文件移到桌面上修改,然后再移回去

# 以下为 kafka的
192.168.10.160  www.sc.com
192.168.10.157  www.sc.com
192.168.10.161  www.sc.com

# 以下为学习linux加的
# 是基于域名的虚拟主机  ,此种要好些,公用一个ip,共用一个端口
1.14.183.210   www.liang.com
1.14.183.210   www.tian.com
1.14.183.210   www.lili.com
错误页面跳转

浏览器访问错误页面 访问的www.qq.com/liang 跳出一个404错误页面和公益广告
https://www.qq.com/404page.html

1、自己写一个错误的页面

此为配置文件中的错误 处理,可以编写一个404文件

  error_page  404              /404.html;

[root@VM-12-8-centos html]# ls
50x.html  hejin  index.html  jay.jpg  liang  lili  tian
[root@VM-12-8-centos html]# vim 404.html


[root@VM-12-8-centos html]# cat 404.html 
<html>
	<head>
		<title>404 page not found</title>
</head>
<body>
	<p>page not found
	<p><h3>admin:liangrui  16609165839</h3>  (h为加粗)
	<p><img src=jay.jpg width=200> 
</body>
</html>

2、然后加到server中,使用www.liang.com/***访问看是否会出现那个错误页面


    server {
   
                listen 80;
                server_name www.liang.com;
                location / {
   
                        root html/liang;                        index index.html index.htm shouye.html;  #首页文件优先级,先从做左>}
                error_page  404              /404.html;

                access_log  logs/liang_access.log  main; #访问日志
                error_log logs/liang_error.log notice; #错误日志 
    }

3、打开浏览器没有那个效果,打开日志排错

2022/07/22 19:35:24 [error] 15159#15159: *434 open() "/usr/local/jay-zhou99/html/liang/404.html" failed (2: No such file or directory), client: 222.244.221.23, server: www.liang.com, request: "GET /ww HTTP/1.1", host: "www.liang.com"

html 文件放的不对,要放到这个文件下面去


/usr/local/jay-zhou99/html/liang/404.html"

4、加了一行跳转的页面。5秒后自动跳转

<html>
	<head>
		<title>404 page not found</title>
		<meta http-equiv="refresh" content="5";url="http://www.bilibili.com">
</head>
<body>
	<p>page not found
	<p><h3>admin:liangrui  16609165839</h3>
	<p><img src=jay.jpg width=200> 
</body>
</html>

5、错误页面效果如下
在这里插入图片描述

5秒后跳转至bilibili

在这里插入图片描述

www.liang.com:8080 (冒号后面加端口,表示访问哪个端口)

IO多路复用

解决高并发的问题
1、select 和 epoll

select单个进程可监视的fd数量受到限制,epoll和select都可实现同时监听多个I/O事件的状态。

  • select 基于轮训机制
  • epoll基于操作系统支持的I/O通知机制 epoll支持水平触发和边沿触发两种模式。

lsof -p 加进程号 查看该进程打开了哪些文件 (p---->pid)

[root@VM-12-8-centos conf]# ps aux |grep nginx
root      4793  0.0  0.1  50196  3084 ?        Ss   Jul19   0:00 nginx: master process nginx
jay-zhou 29738  0.0  0.1  52280  2356 ?        S    22:02   0:00 nginx: worker process
jay-zhou 29739  0.0  0.1  52280  2356 ?        S    22:02   0:00 nginx: worker process
root     29791  0.0  0.0 115928  1016 pts/1    S+   22:02   0:00 grep --color=auto nginx
[root@VM-12-8-centos conf]# lsof -p 29738
COMMAND   PID     USER   FD      TYPE             DEVICE SIZE/OFF    NODE NAME
nginx   29738 jay-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值