在大型网站搭建时,都会考虑如果用户量每日不断增加,大量的并发访问,会不会给网站、数据库带来崩盘的灾难。今天我们就讨论一下,现实中如何解决这些问题的一套最为容易实现的方案。

控制并发,大家都会首先考虑的就是分布式、负载均衡等经常听到的It名词。那网站如何才能实现负载均衡呢,除了世面上的一些负载均衡器外,我们有哪些软件上的解决方案呢,这时候,Nginx、lvs 等名词就会在脑海中浮现。那这些负载均衡的软件如何使用呢,如何读者是.net工程师,大家会选择Nginx,因为它支持Windows服务器,这时候,好多网友会批判一下,说lvs更好更优秀。其实大家不必太在意,其实都一样,只要你能掌控它们就Ok,各有优劣。Nginx配置简单,在中小型项目中使用更为方便,下面我们看下Niginx在Windows下的配置,lvs在Linux的配置下一篇再写。

概述:使用Nginx搭建反向服务器,实现网站服务器集群负载均衡

1、下载Nginx——Windows版,(nginx-1.6.2.zip)在博客末端可下载,解压

2、使用winsw-1.8-bin(windows服务工具).exe工具将Niginx发布成Domain模式,通过Windows服务的方式控制Niginx的运行。在博客末端可下载

(1)配置 winsw运行的Xml文件,如上图:将Winsw工具移植到解压的Niginx文件下,将工具名字修改成“nginxServer.exe”,创建一个xml配置文件“nginxServer.xml”文件,它跟工具的名字一致,当然你也可以不要改名字。xml文件如下:

 <?xml version="1.0" encoding="UTF-8" ?>
<service>
<id>nginx</id>
<name>ngixServer</name>
<description>High Performance Nginx Service</description>
<executable>E:\2014newT\Practise\nginx1.62Server\nginx.exe</executable>
<logpath>E:\2014newT\Practise\nginx1.62Server\</logpath>
<logmode>roll</logmode>
<depend></depend>
<startargument>-p E:\2014newT\Practise\nginx1.62Server</startargument>
<stopargument>-p E:\2014newT\Practise\nginx1.62Server -s stop</stopargument>
</service>

winsw 文件配置

配置很简单主要指定Nginx.exe的位置\log位置等,可以谷歌一下Winsw看看具体的配置信息。

(2)点击nginxServer.exe安装服务,如果你是Win8以上的系统可能装不上,是因为兼容问题,调制兼容Win7模式,以管理员的身份运行即可,如图:

这时查看Windows服务,启动NginxServer服务,如图:

这时,ngixServer服务成功启动了。

3、修改Nginx配置,将代理指向服务器集群,实现网站负载均衡

在解压的Nginx文件夹下找到conf/nginx.conf文件,打开进行配置:

 #user  nobody;
worker_processes ;#启动的线程数 #错误的位置和级别
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid;#pid进程文件的位置 events {
worker_connections ;#每个进程的最大连接数
} http {
include mime.types;
default_type application/octet-stream;
#nginx日志格式定义,在下面可以进行引用
#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 ;
keepalive_timeout ;
upstream linuxidc.com{
server 127.0.0.1:; #服务器集群A
server 127.0.0.1:; #服务器集群B
}
#gzip on; server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm default.aspx;
proxy_pass http://linuxidc.com;
proxy_redirect default;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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 ;
# listen somename:;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 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;
# }
#} }

nginx.conf 配置

不用害怕,修改的地方很少。

(1)worker_processes  4;#启动的线程数 一般为你代理服务器的内核数

(2)在“HTTP”括弧中配置服务器群的网站发布的ip地址和端口号

upstream linuxidc.com{
server x.x.x.x:8091; #服务器A
server x.x.x.x:8092; #服务器B

}

(3)配置代理服务器的地址,即Nginx安装的服务器地址、监听端口、默认地址

server {
listen 8090;    #监听端口
server_name localhost;   #服务器Ip地址

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm default.aspx;  #默认网站首页地址
proxy_pass http://linuxidc.com;
proxy_redirect default;
}

重启Nginx Windows服务,收工完成,创建一个网站,ip、端口号、默认首页要与代理服务器Server配置一致哦,试试吧。。

代码奉上:

http://pan.baidu.com/s/1pJukQ2R

Nginx简单实现网站的负载均衡的更多相关文章

  1. nginx简单反向代理和负载均衡(ubuntu)

    nginx简单反向代理与负载均衡 环境:三台ubuntu 12.04.5 虚拟机    均装有nginx 1.1.19 以下u1(192.168.240.129) ,u2(192.168.240.13 ...

  2. Nginx + Tomcat Windows下的负载均衡配置

     Nginx + Tomcat Windows下的负载均衡配置 一.为什么需要对Tomcat服务器做负载均衡?    Tomcat服务器作为一个Web服务器,其并发数在300-500之间,如果超过50 ...

  3. 在ubuntu上面配置nginx实现反向代理和负载均衡

    上一篇文章(http://www.cnblogs.com/chenxizhang/p/4684260.html),我做了一个实验,就是利用Visual Studio,基于Nancy框架,开发了一个自托 ...

  4. 图文解说:Nginx+tomcat配置集群负载均衡

    图文解说:Nginx+tomcat配置集群负载均衡 博客分类: appserver nginxTomcatUbuntuLinux网络应用  作者:niumd Blog:http://ari.iteye ...

  5. Nginx的反相代理, 负载均衡

    转自 http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...

  6. Nginx反向代理实现Tomcat负载均衡

    这篇短文主要介绍Tomcat的集群和用Nginx反向代理实现Tomcat负载均衡. 1.首先需要对一些知识点进行扫盲(对自己进行扫盲,囧): 集群(Cluster) 简单来说就是用N台服务器构成一个松 ...

  7. nginx,lvs,haproxy负载均衡对比

    Nginx/LVS/HAProxy是目前使用最广泛的三种负载均衡软件,一般对负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术,具体的应用需求还得具体分析. 如果是中小型的Web应用,比 ...

  8. octavia的实现与分析(一)·openstack负载均衡的现状与发展以及lvs,Nginx,Haproxy三种负载均衡机制的基本架构和对比

    [负载均衡] 大量用户发起请求的情况下,服务器负载过高,导致部分请求无法被响应或者及时响应. 负载均衡根据一定的算法将请求分发到不同的后端,保证所有的请求都可以被正常的下发并返回. [主流实现-LVS ...

  9. nginx 配置反向代理,负载均衡实战解析

    前言:NGINX的反向代理和负载均衡是网站架构中经常用到的一种高并发,高可用的方案,下面我们直接实战操作,当然理论也是要的. 一.反向代理 过程:反向代理:客户端 一>代理 <一> ...

随机推荐

  1. [linux]SSH公钥登录

    由于口令密码容易泄露,SSH公钥登录相比口令登录更加安全.SSH可以轻松使用非对称加密技术给两台机子订立契约,步骤如下: 第一步 本地机生成秘钥对 指令:ssh-keygen 功能:在本地(~/.ss ...

  2. 边工作边刷题:70天一遍leetcode: day 85

    Find the Celebrity 要点: 这题从solution反过来想比较好:loop through n同时maintain一个candidate:如果cand认识某个i,那么modify c ...

  3. codeforces 480A A. Exams(贪心)

    题目链接: A. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. SQL里面如何取得前N条数据?

    select * from table order by id limit 10 运用limit可以获取前N个数据

  5. mac在xampp下使用yii2.0开发环境配置

    在mac上装环境,折腾了我好久.先用是mac自带的php,但自带的PHP很多扩展都需要自己安装.libevent,memcache等扩展都安装好了之后,发现pdo_mysql.dll扩展又没有,悲剧的 ...

  6. C和指针笔记 3.6链接属性

    链接属性决定如何处理在不同文件中出现的标识符.标识符的作用域也它的链接属性有关,但这两个属性并不相同. 没有链接属性的标识符(none)总是被当作单独的个体,也就是说该标识符的多个声明被当作独立不同的 ...

  7. 详解反射->Type.System

    反射先了解 一:system.Type 获取基本信息: Type.Name   //类名 Type.FullName //完整路径 Type.Namespace //空间名 public class ...

  8. 通过imeMode禁用键盘只能输入数字

    var obj = document.getElementById('y'); var arr = [48,49,50,51,52,53,54,55,56,57];//数字对应的键码 obj.onke ...

  9. iOS获取窗口当前显示的控制器

    解决类似网易新闻客户端收到新闻推送后,弹出一个UIAlert,然后跳转到新闻详情页面这种需求 1.提供一个UIView的分类方法,这个方法通过响应者链条获取view所在的控制器 - (UIViewCo ...

  10. Linux 守护进程二(激活守护进程)

    //守护进程--读文件 #include <stdio.h> #include <stdlib.h> #include <string.h> #include &l ...