Nginx 专为性能优化而开发,性能是其最重要的要求,十分注重效率,有报告 Nginx 能支持高达 50000 个并发连接数。
Nginx 知识网结构图
Nginx 的知识网结构图如下:
02 反向代理
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-fcc84d2bd6c446c6d796b1bfffbf11ba.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-f231a6a00e175b882fa1218bdfa0939b.png)
负载均衡
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-149c9ada89beda5a268f108270e34cab.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-9a191784c729fc44294b253fe9b2fa76.png)
04 动静分离
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-fa4579e83d9b56aee283d31b1a827d87.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-8b50810e3deeab098214956bd32b1a06.png)
05 Nginx安装
Nginx 如何在 Linux 安装
Nginx 常用命令
./nginx -s quit
Nginx 的配置文件
①全局块
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-8eba11be259dcafb307320aeca33bf83.png)
②events 块
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-3acd3f094680ce1850484e7915b3ed18.png)
③HTTP 块
}
-
=:用于不含正则表达式的 url 前,要求字符串与 url 严格匹配,匹配成功就停止向下搜索并处理请求。 -
~:用于表示 url 包含正则表达式,并且区分大小写。 -
~*:用于表示 url 包含正则表达式,并且不区分大小写。 -
^~:用于不含正则表达式的 url 前,要求 Nginx 服务器找到表示 url 和字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再匹配。 -
如果有 url 包含正则表达式,不需要有 ~ 开头标识。
06 反向代理实战
①配置反向代理
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-16d57e785b739df7725744763a9fcb9e.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-dc7bcd7577d76c8150eef8506539582f.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-c80f3bdbec5a57dd2c08e481150d63ad.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-1461ee7f57d1338b65f4664397d26a7d.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-c749c59c5563bad15615f59b7482ac7f.png)
-
访问 http://192.168.25.132:9001/edu/ 直接跳转到 192.168.25.132:8080 -
访问 http://192.168.25.132:9001/vod/ 直接跳转到 192.168.25.132:8081
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-855ae8722d58dff723a938fa07b70ece.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-0f505c66d9bf1c4e1aaa5a9d6522a01f.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-b7b55a2a551b8d5b6fa67494c48ebaf1.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-e33e4a4db06866f582ddc7c97ff21f16.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-2015a871285d595ee672e867b3cf6090.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-186efa79bbcf543d2e9562494595c2b9.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-363d29fbd85d634f7b8083711f40d717.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-8dd2e0ec3bcce5ed22142728f82bd161.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-8dd2e0ec3bcce5ed22142728f82bd161.png)
192.168.25.132 www.123.com。
-
访问 http://192.168.25.132:9001/edu/ 直接跳转到 192.168.25.132:8080 -
访问 http://192.168.25.132:9001/vod/ 直接跳转到 192.168.25.132:8081
负载均衡实战
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-b2d2d25d4e33549d6451463eae9624ec.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-f2c04bc1307b9c19f022b66816362cd4.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-29465c130b3039cd200541c7948dcd60.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-76c149c9918c40d2da7e5529a4b41f95.png)
-
轮询(默认)。 -
weight,代表权,权越高优先级越高。 -
fair,按后端服务器的响应时间来分配请求,相应时间短的优先分配。 -
ip_hash,每个请求按照访问 ip 的 hash 结果分配,这样每一个访客固定的访问一个后端服务器,可以解决 Session 的问题。
动静分离实战
什么是动静分离?把动态请求和静态请求分开,不是讲动态页面和静态页面物理分离,可以理解为 Nginx 处理静态页面,Tomcat 处理动态页面。
-
纯粹将静态文件独立成单独域名放在独立的服务器上,也是目前主流方案。 -
将动态跟静态文件混合在一起发布,通过 Nginx 分开。
动静分离图析:
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-e0518d167d6a4b62061b1b35d4df1fa4.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-65505795206e29519da85e6dc77afa96.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-b122f4b291823baedecd525fe8627448.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-66237481f746bc9127df5fa8af0eb969.png)
Nginx 高可用
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-ff9d35f7ec4cdc2ecb66445c62af8079.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-9039feb99217bad7cbbddfd5d1d97b79.png)
-
两台 Nginx 服务器 -
安装 Keepalived -
虚拟 ip
安装 Keepalived:
[root@192 usr]# rpm -q -a keepalived
keepalived-1.3.5-16.el7.x86_64
[root@192 keepalived]# vi keepalived.conf
-
smtp_server 192.168.25.147(主)smtp_server 192.168.25.147(备) -
state MASTER(主) state BACKUP(备)
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.25.147
smtp_connect_timeout 30
router_id LVS_DEVEL # 访问的主机地址
}
vrrp_script chk_nginx {
script “/usr/local/src/nginx_check.sh” # 检测文件的地址
interval 2 # 检测脚本执行的间隔
weight 2 # 权重
}
vrrp_instance VI_1 {
state BACKUP # 主机MASTER、备机BACKUP
interface ens33 # 网卡
virtual_router_id 51 # 同一组需一致
priority 90 # 访问优先级,主机值较大,备机较小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.50 # 虚拟ip
}
}
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-857f8924863ebf4a062b45fd98d68a0f.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-ae6ec60ae55ba7e6d3ac6c4e0b44bca3.png)
原理解析
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-9e46cfb64846f327f734746c2b194b78.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-39c9a6b93f34352c4a036c9098dcde28.png)
![](https://www.linuxstudy.tech/wp-content/uploads/2022/02/wxsync-2022-02-1e45d650258c7fc586891006fb0f2196.png)
– EOF –
本篇文章来源于微信公众号: Linux爱好者