Nginx负载均衡

跟風遠走 提交于 2019-11-27 18:27:48

俗话说知识是人类进步的阶梯,这个还真有点像,比如前几天学会了IIS的部署,一个坎迈过去之后就有另一个坎在等着,所以Nginx这个坎就摆在了面前。Nginx一般在大型网站中用来做负载均衡的比较多,今天主要是简单演示下它在Window下的安装与配置使用。关于它的介绍我这里就不班门弄斧了,百科以及其他博客也介绍的比较清楚。

一、下载安装

可在http://nginx.org/en/download.html网站下载Nginx。我选择的是稳定版本。

下载完之后就是安装,我把它放在D:\NGINX目录下,可以直接点击nginx-1.10.2下的nginx.exe文件,但是安装的时候一闪而过,并不知道安装成功与否,我就遇到了这样的情况,它默认配置的是80端口,我浏览器输入Localhost时没任何输出,这时可以打开logs文件,查看下日志,在error文件中还真找到了输出,原来是有错误。

2016/12/03 21:29:23 [emerg] 6940#1604: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

百度查了下,这种情况可能有下面的几种情况.
1.防火墙未关闭,防火墙我检查是关闭了

2.有其他应用在占用80端口,我把浏览器关闭之后,再运行还是会报错,用cmd命令行更加明显直接显示错误信息。这时我又把IIS停了,然后再运行就没报错了。在浏览器输入http://localhost/就会有下面页面输出。

 二、负载均衡

1.为了做负载均衡测试,我在IIS上部署了两个网站,一个是MVC,一个是WebForm.端口号分别是8080,8082.

2.修改host,自定义一个域名用来表示本地域名。C:\Windows\System32\drivers\etc\hosts

为这两个网站设置绑定主机名

3.配置Nginx

修改nginx-1.10.2\conf\nginx配置文件如下。这里主要是设置Nginx监听8081端口,并负载均衡到集群upstream:admin.cuiyw.com中的8080、8082两个端口上。权重weight都是1.这里只是些简单的配置,关于Nginx的配置还有好多内容,以后专门写一博客整理。

worker_processes  4;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream admin.cuiyw.com {
       server 127.0.0.1:8080 weight=1;
       server 127.0.0.1:8082 weight=1;

}

server {
        listen       8081;
        location / {
            #其中admin.cuiyw.com对应着upstream设置的集群名称
             proxy_pass http://admin.cuiyw.com;
           #设置主机头和客户端真实地址,以便服务器获取客户端真实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_connect_timeout    600;
             proxy_read_timeout       600;
             proxy_send_timeout       600;
        }
    }
}

还有在配置过程中也出了好多个错误,如下面的一些,如果出现错误看日志是最好的解决问题的第一步骤。

2016/12/03 21:21:10 [emerg] 7128#7996: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

2016/12/03 22:11:45 [notice] 2144#5864: signal process started
2016/12/03 22:15:31 [emerg] 7620#9116: invalid number of arguments in "proxy_pass" directive in D:\NGINX\nginx-1.10.2/conf/nginx.conf:50

2016/12/03 22:40:41 [error] 6872#6868: OpenEvent("Global\ngx_reload_8748") failed (2: The system cannot find the file specified)
2016/12/03 22:40:50 [emerg] 6844#5620: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

2016/12/03 22:56:34 [alert] 7824#7312: *12270 1024 worker_connections are not enough while connecting to upstream, client: 127.0.0.1, server: , request: "GET /favicon.ico HTTP/1.0", upstream: "http://127.0.0.1:80/favicon.ico", host: "admin.cuiyw.com", referrer: "http://admin.cuiyw.com/"

2016/12/03 23:07:11 [error] 1740#1852: *22500 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:80/", host: "admin.cuiyw.com:8081"
2016/12/03 23:15:22 [error] 8144#3420: *32742 maximum number of descriptors supported by select() is 1024 while waiting for request, client: 127.0.0.1, server: 0.0.0.0:80
2016/12/03 23:15:22 [error] 8144#3420: *32740 WSARecv() failed (10053: An established connection was aborted by the software in your host machine) while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /favicon.ico HTTP/1.0", upstream: "http://127.0.0.1:80/favicon.ico", host: "admin.cuiyw.com", referrer: "http://admin.cuiyw.com/"
2016/12/03 23:26:04 [notice] 6252#6592: signal process started

4.运行结果

在输入http://admin.cuiyw.com:8081/之后会出现两种页面,一种是MVC一种是WebForm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!