Windows系统中配置Nginx负载均衡

Windows系统中配置Nginx详细步骤之负载均衡

A.下载nginx

下载路径:http://nginx.org/en/download.html
这里下载的版本:nginx-1.10.3

B.解压到C盘并开始配置nginx.conf,搭建负载均衡

  1. 解压文件
    在这里插入图片描述
  2. 启动nginx
    在这里插入图片描述
  3. 检查nginx是否成功
    在这里插入图片描述
  4. Nginx配置后的截图
    在这里插入图片描述
  5. Nginx配置后的源码

#user  nobody;
worker_processes  1;  #允许生成的进程数,默认为1

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {    
	accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    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;
	
	#配置的服务器集群;当然这里也可以配置其它域服务器里面的iis站点地址
	upstream tomcatserver{
        server 192.168.183.129:801 weight=1;
        server 192.168.183.129:802 weight=2;
        server 192.168.183.129:803 weight=2;
		server 192.168.183.130:804 weight=3;
    }

    #gzip  on;

    server {
        listen       8282; #监听的端口号
        server_name  192.168.183.129; #监听的地址

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;  #根目录
            index  index.html index.htm; #设置默认页
			proxy_pass   http://tomcatserver; #请求转向tomcatserver 定义的服务器列表
        }

        #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;
    #    }
    #}

}

C.asp.net mvc代码

  1. C#代码
    public class UserController : Controller
    {
        private string ServerDataDB = ConfigHelper.GetConfigStr("ServerDataDB");
        // GET: User
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult GetSearch() 
        {
            object json = new { };
            try
            {
                string fullUrl = Request.Url.AbsoluteUri; //请求地址
                string querystring = Request.Url.PathAndQuery;  //请求方法
                string authority = Request.Url.Authority; //请求域名
                string[] domain = authority.Split(':');
                string sql = "";
                string userNameCount = "Admin_" + GetNoncestr(8);
                if (domain[1].Contains("801"))
                {
                    sql = "insert into [User] ([AbsoluteUri],[AbsoluteAccountName],[AbsoluteAccountPort],[UserName],[UserPwd],[Creater],[CreateTime]) values ('" + fullUrl + "','" + domain[0] + "','" + domain[1] + "','"+ userNameCount + "','" + userNameCount + "','" + userNameCount + "',getdate())";
                }
                else if (domain[1].Contains("802"))
                {
                    sql = "insert into [User] ([AbsoluteUri],[AbsoluteAccountName],[AbsoluteAccountPort],[UserName],[UserPwd],[Creater],[CreateTime]) values ('" + fullUrl + "','" + domain[0] + "','" + domain[1] + "','" + userNameCount + "','" + userNameCount + "','" + userNameCount + "',getdate())";
                }
                else if (domain[1].Contains("803"))
                {
                    sql = "insert into [User] ([AbsoluteUri],[AbsoluteAccountName],[AbsoluteAccountPort],[UserName],[UserPwd],[Creater],[CreateTime]) values ('" + fullUrl + "','" + domain[0] + "','" + domain[1] + "','" + userNameCount + "','" + userNameCount + "','" + userNameCount + "',getdate())";
                }
                else if (domain[1].Contains("804"))
                {
                    sql = "insert into [User] ([AbsoluteUri],[AbsoluteAccountName],[AbsoluteAccountPort],[UserName],[UserPwd],[Creater],[CreateTime]) values ('" + fullUrl + "','" + domain[0] + "','" + domain[1] + "','" + userNameCount + "','" + userNameCount + "','" + userNameCount + "',getdate())";
                }
                else
                {
                    sql = "insert into [User] ([AbsoluteUri],[AbsoluteAccountName],[AbsoluteAccountPort],[UserName],[UserPwd],[Creater],[CreateTime]) values ('','','','','','',getdate())";
                }

                new DbHelperSQL(ServerDataDB).ExecuteSql(sql);

                json = new
                {
                    code = 0,
                    url = fullUrl,
                    authorityName= domain[0],
                    authorityPort = domain[1],
                };
            }
            catch (Exception ex)
            {
                json = new
                {
                    code = 0,
                    url = "",
                    authorityName = "",
                    authorityPort = "",
                };
            }

            return Json(json);
        }

        /// <summary>
        /// 获取随机字符串
        /// </summary>
        /// <param name="codeCount">多少位的随机字符串</param>
        /// <returns></returns>
        public string GetNoncestr(int codeCount)
        {
            int rep = 0;
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + rep;
            rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
            for (int i = 0; i < codeCount; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                }
                else
                {
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                }
                str = str + ch.ToString();
            }
            return str;
        }
    }

  1. Sql Server表
USE [test]
GO

/****** Object:  Table [dbo].[User]    Script Date: 2020/7/1 17:50:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[User](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[AbsoluteUri] [nvarchar](255) NULL,
	[AbsoluteAccountName] [nvarchar](255) NULL,
	[AbsoluteAccountPort] [nvarchar](100) NULL,
	[UserName] [nvarchar](100) NULL,
	[UserPwd] [nvarchar](100) NULL,
	[Creater] [nvarchar](100) NULL,
	[CreateTime] [datetime] NULL,
 CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

D.配置的iis站点(多个文件夹的程序都是一样的;只是为了好测试是否实现了负载均衡的效果)

  1. iis页面
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

E.想要的负载均衡效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

F.nginx常用命令

    nginx -s quit       优雅停止nginx,有连接时会等连接请求完成再杀死worker进程  

	start nginx         开启nginx服务器 
	
    nginx -s reload     优雅重启,并重新载入配置文件nginx.conf

	nginx -s stop       关闭nginx服务
	
    nginx -s reopen     重新打开日志文件,一般用于切割日志

    nginx -v            查看版本  

    nginx -t            检查nginx的配置文件

    nginx -h            查看帮助信息

   nginx -V       详细版本信息,包括编译参数 

    nginx  -c filename  指定配置文件

转载自:https://blog.csdn.net/qq_41863998/article/details/107066896

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值