nginx读这一篇就够了
Posted wxiaoyu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx读这一篇就够了相关的知识,希望对你有一定的参考价值。
nginx 是一个高性能的 HTTP 和反向代理服务器,特点是占用内存少,并发能力强,事实上 Nginx 的并发能力确实在同类型的网页服务器中表现较好。Nginx 专为性能优化而开发,性能是其最重要的要求,十分注重效率,有报告 Nginx 能支持高达 50000 个并发连接数。
01
Nginx 知识网结构图
02
反向代理
![nginx读这一篇就够了](https://image.cha138.com/20210331/ed4ffbfe18a24b438cd4d65e165f8ec2.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/dc40cc7cfee442e298300b62a40b155e.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/9cda1f4e08fa4164818120d97594ebac.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/f7b56be508bc48bbb27c3fc6e1da8bff.jpg)
04
动静分离
![nginx读这一篇就够了](https://image.cha138.com/20210331/606db027a36c49039e5f2bd4b1220b41.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/348d2822a6f04164bf866e101abab771.jpg)
Nginx 如何在 Linux 安装
https: //blog.csdn.net/yujing1314/article/details/97267369
Nginx 常用命令
./nginx -v
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
Nginx 的配置文件
①全局块
![nginx读这一篇就够了](https://image.cha138.com/20210331/f1a1d68062b44b5cbdb0ef3cbf6a263e.jpg)
②events 块
![nginx读这一篇就够了](https://image.cha138.com/20210331/3f671d3abfea4d829bdf5595c59b55dc.jpg)
③HTTP 块
location [ = | ~ | ~* | ^~] url{
}
=:用于不含正则表达式的 url 前,要求字符串与 url 严格匹配,匹配成功就停止向下搜索并处理请求。
~:用于表示 url 包含正则表达式,并且区分大小写。
~*:用于表示 url 包含正则表达式,并且不区分大小写。
^~:用于不含正则表达式的 url 前,要求 Nginx 服务器找到表示 url 和字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再匹配。
如果有 url 包含正则表达式,不需要有 ~ 开头标识。
07
反向代理实战
①配置反向代理
![nginx读这一篇就够了](https://image.cha138.com/20210331/b41c3009c13f44f78d4716e12c4d57d1.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/3cb328a0b1cc4142aa2a88a3f4958cfa.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/0eb7764c8dbe448f9c6a6970d68d0a1f.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/a7f0e079be2d492ab2bdc236e6886917.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/1508d707038f42b9ac6a55b661d2e7b0.jpg)
访问 http://192.168.25.132:9001/edu/ 直接跳转到 192.168.25.132:8080
访问 http://192.168.25.132:9001/vod/ 直接跳转到 192.168.25.132:8081
![nginx读这一篇就够了](https://image.cha138.com/20210331/72ab1acc651844388d4728b8332e18bc.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/1a83e3ebe233450c9330705a69fd58f4.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/41b001403f2d42a88360523ce3685f1b.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/7ae86d7cba034943ad1eed8f95e66963.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/563e9399c40647adb1de7a5d46cff4ab.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/bc514e3068894d68bb7bce2d73f14874.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/40e00c17352d4bb8b2a7f0375c877e20.jpg)
./nginx -s reload
![nginx读这一篇就够了](https://image.cha138.com/20210331/b3779c6e572343779099374e44e73fa4.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/b3779c6e572343779099374e44e73fa4.jpg)
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
![nginx读这一篇就够了](https://image.cha138.com/20210331/208d55e1fe4e495b92e0b54141a62e79.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/7663cfabcf524555bdd719ef88643343.jpg)
./nginx -s reload
![nginx读这一篇就够了](https://image.cha138.com/20210331/7eccd42d97cd485a92de2ce1daf26f08.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/3ecc5303c97c4000a8041758e75b0a5b.jpg)
轮询(默认)。
weight,代表权,权越高优先级越高。
fair,按后端服务器的响应时间来分配请求,相应时间短的优先分配。
-
ip_hash,每个请求按照访问 ip 的 hash 结果分配,这样每一个访客固定的访问一个后端服务器,可以解决 Session 的问题。
09
动静分离实战
什么是动静分离?把动态请求和静态请求分开,不是讲动态页面和静态页面物理分离,可以理解为 Nginx 处理静态页面,Tomcat 处理动态页面。
纯粹将静态文件独立成单独域名放在独立的服务器上,也是目前主流方案。
将动态跟静态文件混合在一起发布,通过 Nginx 分开。
动静分离图析:
![nginx读这一篇就够了](https://image.cha138.com/20210331/ccab422fd4624444bc0d3c581f4d4484.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/116c36ad3fa3498182ce7c90b27dfee0.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/6f3f1a94dbda4d0aa42715fcd0a5637c.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/37350e0df35d4303889d06996a18a86a.jpg)
Nginx 高可用
![nginx读这一篇就够了](https://image.cha138.com/20210331/5d845a6ca9414dcc82afdec634e25f98.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/31f52b1532c04dda9a2d9082a0f2082b.jpg)
两台 Nginx 服务器
安装 Keepalived
-
虚拟 ip
安装 Keepalived:
[root@192 usr]# yum install keepalived -y
[root@192 usr]# rpm -q -a keepalived
keepalived-1 .3.5-16.el7.x86_64
[ root@192 keepalived] # cd /etc/keepalived
[ root@192 keepalived] # vi keepalived.conf
smtp_server 192.168.25.147(主)smtp_server 192.168.25.147(备)
-
state MASTER(主) state BACKUP(备)
global_defs {
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
}
}
[ root@192 sbin] # systemctl start keepalived.service
![nginx读这一篇就够了](https://image.cha138.com/20210331/5bbde939b9e4472ca71064c045ab2c95.jpg)
![nginx读这一篇就够了](https://image.cha138.com/20210331/a20055266f224e2187ccc7359461529b.jpg)
原理解析
![nginx读这一篇就够了](https://image.cha138.com/20210331/9be3493644d44631a7f91feab9bb4f47.jpg)
以上是关于nginx读这一篇就够了的主要内容,如果未能解决你的问题,请参考以下文章
你想快速掌握数据库中间件 MyCAT 的核心概念吗,读这一篇就够了!