windows 搭建 nginx 负载均衡
一、软件介绍
软件
Nginx
版本
1.8.1
类型
压缩包
实现功能:负载均衡,反向代理
下载地址
http://nginx.org/en/download.html
二、安装说明
1.从官网下载压缩包进行解压
2.该文档解压目录为 E 盘,解压后的目录为 E:\nginx-1.8.1
3.修改 E:\nginx-1.8.1\conf\nginx.conf 配置文件(警告:打开该文件不要用记事本打开),内容
如下:
多台服务器以 ip+端口的形式写入 upstream tomcat{},weight 表示权重,即 nginx 将
任务进行分配时,将更多的任务分配给权重较大的服务器。backup 表示备用,即如果
有两台服务器,其中一台作为备用服务器,当另外一台服务器挂了之后,会启动备用
服务器。
格式如下:server ip:端口 weight=2;
4.常用命令(操作命令需在 nginx 的根目录执行,即 E:\nginx-1.8.1\)
启动 nginx:start nginx;
重新加载 nginx: nginx.exe –s reload;
安全退出 nginx:nginx.exe –s quit;
强制退出 nginx:nginx.exe –s stop;
查看端口:netstat -ano|findstr 80(该命令为查看 80 端口);
5.配置文件内容(其他参数配置请参考 http://www.nginx.cn/doc/)
worker_processes 1;
events {
worker_connections 1024;
}
http {
include
mime.types;#文件扩展名与文件类型映射表
default_type application/octet-stream;#默认文件类型,默认为 text/plain
sendfile
on;#允许 sendfile 方式传输文件,默认为 off,可以在 http 块,server 块,location 块。
keepalive_timeout 65;#连接超时时间,默认为 75s,可以在 http,server,location 块
#gzip on;
upstream tomcat {
server 120.79.14.152:8085 weight=2;
server 127.0.0.1:8085 weight=4; #这个可以配置权重,权重越大,访问该服务器的机率就越大
server 120.79.14.152:8086 backup;
}
server {
listen 80;#监听端口
server_name test.cdmtn.com;#监听地址
location / {
root html;
index /tpl/index/login.html;
proxy_pass http://tomcat;#可通过域名访问
proxy_redirect default;
proxy_connect_timeout 1; #单位为秒 连接的超时时间。 我设置成 1,表示是 1 秒后超时会连接到另外一台服务器
proxy_send_timeout 1; #这个指定设置了发送请求给 upstream 服务器的超时时间。超时设置不是为了整个发送期间,而是在两次 write
操作期间。如果超时后,upstream 没有收到新的数据,nginx 会关闭连接
proxy_read_timeout 1; #该指令设置与代理服务器的读超时时间。它决定了 nginx 会等待多长时间来获得请求的响应。这个时间不是获得
整个 response 的时间,而是两次 reading 操作的时间
}
error_page 500 502 503 504 /50x.html;#错误页
}
}