最简单的Nginx负载均衡(Load Balancing)

把大象装冰箱分三部:1.把冰箱门打开;2.把大象装进去;3.把冰箱门关上。

在此,创建一个最简单的Nginx负载均衡环境,也分三步走。

第一步:两台笔记本,IP分别为192.168.1.103, 192.168.1.102; 两台都下载了wamp,测试好环境,保证通过103机子能成功访问 http:192.168.1.102:808和http://192.168.1.103:808(说明一下:使用808端口是因为Nginx默认使用的是80端口,进入httpd.conf 把端口修改成808即可)

打开C:\Windows\System32\drivers\etc\hosts文件,最后添加一行 192.168.103    s.com.同样另外一台添加 192.168.1.102   s.com.

 

第二步: 将IP为103的机子暂时作为load balancer,下载nginx,解压至C:盘,改名为nginx,打开C:\nginx\conf\nginx.conf配置文件,找到server{…}在上面添加以下内容:

upstream s.com{
server 192.168.1.102:808;
server 192.168.1.103:808;
}

修改server里面内容,找到server_name  localhost; 将其修改为:server_name  s.com;

找到server里location / {….}修改为:

location / {
proxy_pass http://s.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

修改完如下:

upstream s.com{
server 192.168.1.102:808;
server 192.168.1.103:808;
}

server {
listen 80;
server_name s.com;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
proxy_pass http://s.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}

第三步:打开cmd,进入C:\nginx>  输入nginx启动nginx,打开任务管理器会看到两个nginx进程,表示启动成功。

在103机子上打开浏览器输入:http://s.com/   过几秒刷新一次, 会看到页面在102和103上切换。恭喜,成功啦~

最简单的Nginx负载均衡(Load Balancing)原文链接:https://www.cnblogs.com/songwanzi/p/3537272.html
本文来源 爱码网,其版权均为 原网址 所有 与本站无关,文章内容系作者个人观点,不代表 本站 对观点赞同或支持。如需转载,请注明文章来源。

© 版权声明

相关文章