windows利用Nginx搭建网站负载均衡
来源:互联网 发布:淘宝被恶意退款 编辑:程序博客网 时间:2024/05/22 08:12
大型网站一般都会用两台以上的服务器做负载均衡,当然要实现负载均衡有很多种方式,有软件实现,也有专门的硬件负载均衡设备。由于负载均衡的硬件设备价格不菲,所以很多公司还是采用软件的方式,用得最多,高性能和稳定的当然是Nginx。最开始是Nginx只有Linux版本,让我们庆幸的是现在也推出了Windows版本。今天我就来实践一把在windows平台下利用Nginx搭建网站负载均衡解决方案。
先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。那么负载均衡的前提就是要有多台服务器才能实现,也就是两台以上即可。我手里暂时没有多余的服务器,所以我就在自己的PC上模拟一下多台机器。
一、下载安装Nginx
Nginx官方下载地址:http://nginx.org/en/download.html
打开上面的网址,我们选择Windows版本,最新是1.9.4。如下图:
下载之后把它解压放到一个盘的根目录,我放在是D盘。
二、配置代理
找到conf目录下的nginx.conf配置文件,把默认监听端口80修改为8081。
server{
listen 8081;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the staticpage /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
因为如果你的机器开启了IIS,80端口是被占用的,Nginx会启动不起来。
在cmd下切换到Nginx根目录并开启Nginx:
C:\Users\Administrator>cdD:\nginx-1.9.4
C:\Users\Administrator>d:
如果没什么问题,你可以在任务管理器中找到nginx的进程。
现在我们在浏览器中输入:http://localhost:8081/,会有下面的欢迎信息:
那么恭喜你,nginx就配置好了。下面我们来配置最关键的负载均衡部分。
三、配置Nginx负载均衡
3.1、准备两个网站
在本地创建网站根目录wwwroot,然后创建两个网站目录分别为site1和site2,并且分别在site1和site2新建一个index.html。
site1/index.html:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>Site1</p>
</body>
</html>
site2/index.html:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>Site2</p>
</body>
</html>
接下来在IIS上创建两个网站Site1和Site2分别指向上面创建的site1和site2目录。Site1端口用的是9002,Site2端口是9003。这样保证http://localhost:9002/和http://localhost:90023/两个网站能够正常访问。
说明:为了后面能够看出负载的效果,我们故意把上面两个index.html内容写的不一样。真实的负载均衡网站都一样的。
3.2、修改nginx.conf配置文件
上面我们把Site1和Site2准备好了,现在我们来配置负载需要的相关信息。为这两个网站定义一个统一的访问入口,我们随便起一个域名:a.com
然后在hosts中加入下面的一行代码:
127.0.0.1 a.com
这样把a.com解析到我们的本地。
配置upstream转发服务器设定负载均衡列表列表,添加upstream:
upstream a.com {#必须跟proxy_pass保持一致
server 192.168.1.101:9002;
server 192.168.1.101:9003;
}
配置中心服务器虚拟主机信息,把Server更改为:
server {
listen 8081;
server_name 192.168.1.101;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#设置主机头和客户端真实地址,以便服务器获取客户端真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#禁用缓存
proxy_buffering off;
#设置反向代理的地址
proxy_pass http://a.com;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
更改之后的完整的nginx.conf如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream a.com {#必须跟proxy_pass保持一致
server 192.168.1.101:9002;
server 192.168.1.101:9003;
}
server {
listen 8081;
server_name 192.168.1.101;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#设置主机头和客户端真实地址,以便服务器获取客户端真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#禁用缓存
proxy_buffering off;
#设置反向代理的地址
proxy_pass http://a.com; #nginx路由的网站,提供对外入口
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
四、测试效果
到此,windows利用Nginx搭建网站负载均衡就配置完毕。下面我们重启一下Nginx。
D:\nginx-1.9.4>nginx-s stop
D:\nginx-1.9.4>startnginx
在浏览器在输入:http://a.com:8081/,然后用ctrl+F5,强制刷新。会看到一会儿是site1,一会儿是site2的内容,site1和site2交替出现。
强制刷新后
可以发现这里切换是按照1:1的方式来回切换,通过配置文件你可以站点的权重:
upstream a.com {
server 192.168.1.101:9092 weight=2;
server 192.168.1.101:9093 weight=1;
}
weight即为对应网站的权重。
在Linux中,安装Nginx比较麻烦一些,因为需附加安装一些包,如gzip,pcre等等。
可以在Linux上部署你的站点,比如站点3,如果你要把Windows服务器作为反向代理服务器,那么在upstream site添加一个新的站点:
upstream a.com {
server 192.168.1.101:9092 weight=2;
server 192.168.1.101:9093 weight=1;
server 192.168.1.102:8080;
}
你还可以利用Nginx对反向代理服务器上对静态文件(如JPG,GIF,CSS,JS等等)进行缓存,这样当你需要从Web服务器去请求静态资源的时候,可以直接从反向代理服务器上取得本地的一个资源,这样减少了对Web服务器的压力。
本站文章除注明转载外,均为本站原创或翻译,欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,共创和谐网络环境。
转载请注明:文章转载自:蓝狐软件工作室»原创实践--windows利用Nginx搭建网站负载均衡
本文标题:原创实践--windows利用Nginx搭建网站负载均衡
本文地址:http://www.lanhusoft.com/Article/367.html
- windows利用Nginx搭建网站负载均衡
- windows nginx -- 搭建 负载均衡
- nginx实现网站负载均衡(windows+IIS负载实测)
- nginx实现网站负载均衡(windows+IIS负载实测)
- nginx搭建负载均衡
- nginx 负载均衡搭建
- nginx负载均衡搭建
- 搭建nginx负载均衡
- 搭建nginx 负载均衡
- [Nginx]搭建nginx+tomcat负载均衡(Windows版)
- 利用nginx搭建简单图片服务器实现负载均衡
- linux、windows下利用Nginx对Tomcat做负载均衡
- windows下利用nginx 做IIS负载均衡
- windows使用nginx实现网站负载均衡测试实例
- windows使用nginx实现网站负载均衡测试实例
- windows下nginx+tomcat+memcache负载均衡集群搭建
- Nginx 在windows下配合iis搭建负载均衡过程
- Nginx 在windows下配合iis搭建负载均衡过程
- 数据结构之队列 C++实现
- hdu 4462 构造子集+枚举
- U1-Introduction
- UILabel
- Android网络连接----使用HttpClient连接网络
- windows利用Nginx搭建网站负载均衡
- 美国进入“非结构化”数据分析新时代
- hdu 2609 How many(最小表示法模板+set判重)
- 图层叠加之美(二)
- Android 解决非UI线程操作UI的两种办法 :Handler 和 AsynTask
- Mybatis中javaType和jdbcType对应
- Javascript设计模式与开发实践读书笔记(1-3章)
- Set集合详解
- Reverse Integer