Nginx服务的搭建与配置(二)----Nginx的反向代理
Nginx的反向代理
web网站使用反向代理,除了可以防止外网对内网服务器的恶性攻击、缓存以减少服务器的压力和访问安全控制之外,
还可以进行负载均衡,将用户请求分配给多个服务器。
1.删除残留文件(没有,就跳过)
cd /etc/nginx/conf.d
rm -rf default.conf.rpmsave
2.修改配置文件,配置反向代理。
vi default.conf
#添加如下内容:
server {
listen 80;
server_name www.007.com;
access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://test; //反向代理服务器的地址,也可以是IP地址。
index index.html index.htm index.php;
}
}
upstream test1 { # 后端web服务器
server 192.168.205.149:8080 weight=1; # 默认端口80,默认权重为1.
}
upstream test2 {
server192.168.205.150:8080;
}
server {
listen 80;
server_name cat.com;
access_log /var/logs/nginx/cat.access.log main; # 自己创文件夹及文件哦!
location / {
proxy_pass http://test1; # 反向代理服务器的地址
index index.html index.htm index.php;
}
}
server {
listen 80;
server_name god.com;
access_log /var/logs/nginx/god.access.log main;
location / {
proxy_pass http://test2;
index index.html index.htm index.php;
}
}
保存,退出
3.重启nginx
systemctl restart nginx
4.数据访问流程
使用浏览器访问http://www.cat.com,由于nginx反向代理接受客户机请求,找到server_name为http://www.cat.com的server节点, 根据proxy_pass对应的http路径,将请求转发到upstream test上,即ip为192.168.205.149的服务器。
感谢大家,点赞,收藏,关注,评论!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)