一:Nginx简介
简介:Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器
今天我们来搭建一套能够体会Nginx具体应用的实例。

我们选择最新的版本进行下载,下载后进行安装。
二 安装部署
我把nginx 安装在D:\nginx\nginx-1.15.9

然后,我们在dos 命令中启动nginx

在启动后会出现窗口一闪而过的现象,是正常的,我们可以在进程中查看nginx启动是否成功。

我们看到nginx启动成功
三准备访问页面
现在这个步骤应该是最重要的,我们需要准备访问的环境,来验证nginx 的两大功能。负载均衡和反向代理。
大家可以在自己用两个tomcat服务器启动两个web项目,我这里为了测试方便,用的是nodejs 启动的两个监听:http://127.18.144.23:5789/ 和 127.18.144.23.4789


确保我们能够正常访问两个不同端口的服务,准备工作做好后,接下来我们来验证实例。
四 验证实例效果
我们打开nginx.conf 配置文件

1.反向代理

我们首先找到server部分,将头部内容改成如上所示。
修改完后需要重置nginx服务器,命令为:nginx -s reload
然后在浏览器输入地址:http://localhost:8800/

输入:http://localhost:8800/rel

2.负载均衡
我们在nginx.conf 文件中加入如下代码:
upstream webservers {
server 127.18.144.23:4789 weight=10;
server 127.18.144.23:5789 weight=10;
}
然后,修改location中 proxy_pass 访问的地址为:http://webservers,如下图所示:

修改完后需要重置nginx服务器,命令为:nginx -s reload
这时候我们输入访问地址:http://localhost:8800/
刷新页面,我们会看到内容在两个地址之间来回切换,代表我们负载均衡达到了效果。
五 最后
综上所述,上述实例简单验证了Nginx的两大功能,反向代理和负载均衡。在实际的业务场景中可能需要更加复杂厚重的软硬系统结合来承担更高的客户端流量,从而保障我们系统的高并发,高可用。
网友评论