前言

最近没有什么事情,喜欢总结并学习东西!前几天写来一个Session共享,那么我们为什么需要Session共享?因为我们的应用程序分布在多个服务器上,为了合理分配用户的请求,就需要用到负载均衡技术(将请求/数据【均匀】分摊到多个操作单元上执行)。

怎样实现负载均衡?

1.  使用F5硬件来实现

2. 使用Nginx 工具来搭建一个。

下面我们就讲解一下,在Windows环境下,怎样部署Nginx及常见问题。

一:下载Nginx

去官网下载最新的 Windows-1.11.10 并解压到英文目录下

二:Nginx配置

找到 conf 目录里的 nginx.conf 文件,配置Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#user  nobody;
#指定nginx进程数
worker_processes  1;
 
#全局错误日志及PID文件
#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服务器,利用它的反向代理功能提供负载均衡支持
http {
 
    #设定mime类型,类型由mime.type文件定义
    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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,   
    sendfile        on;
    #tcp_nopush     on;
 
    #连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #开启gzip压缩
    #gzip  on;
 
 
 
    #设定负载均衡的服务器列表 支持多组的负载均衡,可以配置多个upstream  来服务于不同的Server.
    #nginx 的 upstream 支持 几 种方式的分配
    #1)、轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
    #2)、weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 跟上面样,指定了权重。
    #3)、ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
    #4)、fair      
    #5)、url_hash #Urlhash
    upstream mysvr {
      #weigth参数表示权值,权值越高被分配到的几率越大  
      #1.down 表示单前的server暂时不参与负载
      #2.weight 默认为1.weight越大,负载的权重就越大。    
      #3.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。 
      #server 192.168.1.116  down;
      #server 192.168.1.116  backup;
      server 192.168.1.121  weight=1;
      server 192.168.1.122  weight=2;
    }
 
    #配置代理服务器的地址,即Nginx安装的服务器地址、监听端口、默认地址
    server {
        #1.侦听80端口
        listen       80;
 
        #对于server_name,如果需要将多个域名的请求进行反向代理,可以配置多个server_name来满足要求
        server_name  localhost;
         
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            # 默认主页目录在nginx安装目录的html子目录。
            root   html;
            index  index.html index.htm;          
            proxy_pass http://mysvr; #跟载均衡服务器的upstream对应  
        }
 
        #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;
    #    }
    #}
 
}

三:启动Nginx

cmd 进入Nginx解压目录 执行 start nginx启动Nginx服务

启动后如何检查是否启动成功呢? 输入命令 tasklist /fi "imagename eq nginx.exe"   看到以下信息说明启动成功了

一切就绪,访问一下server 里配置的 server_name 是不是被重定向到 upstream配置的服务器上了,是不是很简单!

四:常见问题

如果启动失败 可以看下logs目录下 error.log 文件里的错误信息。

我在第一次安装的时遇到两个错误,也是最容易碰到的问题,在这里列出来方便大家碰到相同的问题时快速解决。

1.  端口占用问题

我的配置文件里服务侦听的是 80 端口,由于机器上部署了IIS,80端口被默认站点占用,把站点关闭就可以了,这个问题在错误日志里记录是这样的。

1
failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

2.Nginx所在目录有中文

错误日志大致输出一下内容

1
failed (1113: No mapping for the Unicode character exists in the target multi-byte code page)

3. 启用缓存时报错

1
2
2015/01/15 17:26:50 [emerg] 17068#20356: shared zone "cache_one" has no equal addresses: 02CF0000 vs 02A20000
2015/01/15 17:26:50 [alert] 11536#11228: worker process 17068 exited with code 1

我一直没有找到解决的方法,有人说重启服务,或者缓存设置大一点就可以了,我试了一下没有用的,官网 原文是这样讲的,只能认为windwos下无解了。

1
2
3
: The cache and other modules which require shared memory support do
: not work in Windows Vista and later due to address space layout
: randomization being enabled in these Windows versions.

五.常用指令

1:start nginx  启动服务

2:nginx -s reload  修改配置后重新加载生效

3:nginx -s reopen  重新打开日志文件

4:nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确

5:nginx -s stop  快速停止nginx

6:tasklist /fi "imagename eq nginx.exe"  查看nginx示范开启

Windows环境下使用Nginx搭建负载均衡的更多相关文章

  1. windows环境下基于nginx搭建rtmp服务器

    基于nginx搭建rtmp服务器需要引入rtmp模块,引入之后需重新编译nginx linux环境几个命令行就能实现编译,笔者未尝试,网上有很多教程. windows环境还需要安装一系列的编译环境,例 ...

  2. windows平台下利用Nginx做负载均衡

    1.下载nginx(http://nginx.org/en/download.html)安装包,解压,并使用cmd命令转到nginx.exe所在的目录 2.执行cmd命令start nginx启动ng ...

  3. Windows 环境下基于 nginx 的本地 PyPI 源

    Windows 环境下基于 nginx 的本地 PyPI 源的搭建: 1.登录 nginx 官网,下载安装包

  4. php-cgi和php-fpm,Windows环境下解决Nginx+php并发访问阻塞问题。

    php-cgi 是运行php,php-fpm是守护php-cgi进程 nginx配置目录运行php        location  ~ \.php$        {                 ...

  5. 【高可用HA】Nginx (1) —— Mac下配置Nginx Http负载均衡(Load Balancer)之101实例

    [高可用HA]Nginx (1) -- Mac下配置Nginx Http负载均衡(Load Balancer)之101实例 nginx版本: nginx-1.9.8 参考来源: nginx.org [ ...

  6. Linux下利用nginx实现负载均衡

    linux下利用nginx实现负载均衡 前提条件: 1,安装好jdk 2,安装好tomcat和nginx(可以参考我前两篇文章) 满足前提条件后,要用nginx实现负载均衡,主要是靠配置nginx的配 ...

  7. Linux和Windows环境下安装Nginx

    Nginx的安装 windows环境下安装Nginx 安装 Nginx 的下载地址如下: http://nginx.org/en/download.html 选择时尽量选择 Stable 稳定版本,点 ...

  8. Nginx简单介绍以及linux下使用Nginx进行负载均衡的搭建

    1.Nginx简介 Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5 ...

  9. Nginx搭建负载均衡集群

    (1).实验环境 youxi1 192.168.5.101 负载均衡器 youxi2 192.168.5.102 主机1 youxi3 192.168.5.103 主机2 (2).Nginx负载均衡策 ...

随机推荐

  1. Chapter 3. Lexical Structure

    /** * Expression = Expression1 [ExpressionRest] * ExpressionRest = [AssignmentOperator Expression1] ...

  2. goahead3.6.3就基本使用(后台上传信息到html页面),高手请忽略

    声明:这里面的代码均为网上找的然后有小小的改动,并非原创.但文章为原创 一..编译 1.1,.下载:进入http://embedthis.com/goahead/下载goahead3.6.3(2017 ...

  3. mysql查看版本

    四种方式: 1. 命令行 ------------------->$ mysql -V mysql Ver , for Linux (x86_64) using EditLine wrapper ...

  4. 实例说明optimize table在优化mysql时很重要

    今天在看CU的时候,发现有人问有关optimize来表优化的问题,当年因为这个问题,困扰我很长一段时间,今天有空我把这个问题,用实际数据来展示出来,让大家可以亲眼来看看,optimize table的 ...

  5. POJ 1577 Falling Leaves 二叉搜索树

    HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of ...

  6. NSSM - the Non-Sucking Service Manager

    nssm is a service helper which doesn't suck. srvany and other service helper programs suck because t ...

  7. java爬虫爬取https协议的网站时,SSL报错, java.lang.IllegalArgumentException TSLv1.2 报错

    目前在广州一家小公司实习,这里的学习环境还是挺好的,今天公司从业十几年的大佬让我检查一下几年前的爬虫程序是否还能使用…… 我从myeclipse上check out了大佬的程序,放到workspace ...

  8. XML的基本概念和Android下的使用

    1. XML的基本概念 1. 什么是XML: 1). XML是指可扩展标记语言(eXtensible Markup Language),它是一种标记语言,很类似HTML.它被设计的宗旨是表示数据,而非 ...

  9. 利用jquery的ajax实现跨域,内部其实是jsonp协议了,不是XHRhttp协议

    一.同源策略 要理解跨域,先要了解一下“同源策略”.所谓同源是指,域名,协议,端口相同.所谓“同源策略“,简单的说就是基于安全考虑,当前域不能访问其他域的东西. 一些常见的是否同源示例可参照下表: 在 ...

  10. 设置tomcat字符编码

    Tomcat的默认编码是ISO-8859-1,如果有是get请求时,会出现乱码,这种情况可以修改Tomcat的编码解决,当然也可以写个过滤器来解决. 在tomcat的conf目录下,编辑server. ...