Windows系统中配置Nginx详细步骤之负载均衡
A.下载nginxB.解压到C盘并开始配置nginx.conf,搭建负载均衡C.asp.net mvc代码D.配置的iis站点(多个文件夹的程序都是一样的;只是为了好测试是否实现了负载均衡的效果)E.想要的负载均衡效果F.nginx常用命令
A.下载nginx
下载路径:http://nginx.org/en/download.html
这里下载的版本:nginx-1.10.3
B.解压到C盘并开始配置nginx.conf,搭建负载均衡
解压文件
启动nginx
检查nginx是否成功
Nginx配置后的截图
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 tomcat_server
{
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
://tomcat_server
; #请求转向tomcat_server 定义的服务器列表
}
#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代码
C#代码
public class UserController : Controller
{
private string ServerDataDB
= ConfigHelper
.GetConfigStr("ServerDataDB");
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
);
}
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
;
}
}
Sql Server表
USE [test
]
GO
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站点(多个文件夹的程序都是一样的;只是为了好测试是否实现了负载均衡的效果)
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 指定配置文件