IIS+NGINX 负载web服务器
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,在我国使用nginx网站用户有:百度、新浪、网易、腾讯、京东等。
我自己搭建的nginx 版本号为1.6.3。(nginx 下载地址为:https://nginx.org/en/download.html)
下载完成后进行解压:
要实现负载均衡需要修改conf/nginx.conf的配置信息,修改配置信息之后重新启动nginx服务,可以通过nginx -s reload指令实现。这里我使用的是windows自带的dos。我个人常用的命令如下:
*启动
直接点击Nginx目录下的nginx.exe 或者 cmd运行start nginx
关闭
nginx -s stop 或者 nginx -s quit
stop表示立即停止nginx,不保存相关信息
quit表示正常退出nginx,并保存相关信息
重启(因为改变了配置,需要重启)
nginx -s reload*
上面需要注意的是,在dos窗口使用命令时,要切换到nginx所在的文件目录下面。
比如我的在nginx目录为:C:\nginx-1.6.3\nginx-1.6.3
如下图:
站点搭建及配置
由于我只有一台电脑,而且用来测试,我就在iis里面创建了两个站点,站点里面的代码使用端口号来进行区分的。
1.在iis里面搭建两个测试站点
站点下只有一个简单的default.aspx页面,用来输出当前服务器信息。由于我没有两台机器,所以将两个站点都部署到本机了,分别绑定了8081和8082两个端口。如下图:
default.aspx页面的代码如下:
protected void Page_Load(object sender, EventArgs e)
{
int port = Request.Url.Port;
if (port == 8081)
{
Response.Write("第一个页面<br/>");
}
else if (port == 8082)
{
Response.Write("第二个页面<br/>");
}
else
{
Response.Write(port.ToString()+"<br/>");
}
Response.Write("请求开始时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"<br/>");
Response.Write("服务器名称:" + Server.MachineName + "<br/>"); //服务器名称
Response.Write("服务器IP地址:" + Request.ServerVariables["LOCAL_ADDR"] + "<br/>"); //服务器IP地址
Response.Write("HTTP访问端口:" + Request.ServerVariables["SERVER_PORT"]);//HTTP访问端口"
Response.Write(".NET解释引擎版本:" + ".NET CLR" + Environment.Version.Major + "." + Environment.Version.Minor + "quot;." + Environment.Version.Build + "." + Environment.Version.Revision + "<br/>"); //.NET解释引擎版本
Response.Write("服务器操作系统版本:" + Environment.OSVersion.ToString() + "<br/>");//服务器操作系统版本
Response.Write("服务器IIS版本:" + Request.ServerVariables["SERVER_SOFTWARE"] + "<br/>");//服务器IIS版本
Response.Write("服务器域名:" + Request.ServerVariables["SERVER_NAME"] + "<br/>");//服务器域名
Response.Write("虚拟目录的绝对路径:" + Request.ServerVariables["APPL_RHYSICAL_PATH"] + "<br/>");//虚拟目录的绝对路径
Response.Write("执行文件的绝对路径:" + Request.ServerVariables["PATH_TRANSLATED"] + "<br/>");//执行文件的绝对路径
Response.Write("虚拟目录Session总数:" + Session.Contents.Count.ToString() + "<br/>"); //虚拟目录Session总数
Response.Write("虚拟目录Application总数:" + Application.Contents.Count.ToString() + "<br/>");//虚拟目录Application总数
Response.Write("域名主机:" + Request.ServerVariables["HTTP_HOST"] + "<br/>");//域名主机
Response.Write("服务器区域语言:" + Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"] + "<br/>");//服务器区域语言
Response.Write("用户信息:" + Request.ServerVariables["HTTP_USER_AGENT"] + "<br/>");
Response.Write("CPU个数:" + Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS") + "<br/>");//CPU个数
Response.Write("CPU类型:" + Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER") + "<br/>");//CPU类型
Response.Write("请求来源地址:" + Request.Headers["X-Real-IP"] + "<br/>");
}
2.修改nginx配置信息
修改nginx监听端口,在conf/nginx.conf文件中修改http server下的listen节点值,我这里使用的是80端口。如下修改:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index default.aspx;
# index index.html index.htm;
#其中ngintest.com 对应着upstream设置的集群名称
proxy_pass http://ngintest.com; (这里需要在C:\Windows\System32\drivers\etc\hosts文件中添加设置,否则有可能使用ngintest.com访问时无法使用 如代码下面的图。其中ngintest.com也是设置服务器集群的名称)
#设置主机头和客户端真实地址,以便服务器获取客户端真实IP
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
}
在IIS中80端口被默认站点使用的话,建议更改默认站点的端口号,这样便于进行测试。修改默认站点端口的操作如下图:右键默认站点-》编辑绑定。
在http节点下添加upstream(服务器集群),server设置的是集群服务器的信息,我这里搭建了两个站点,配置了两条信息。
#服务器集群#
upstream ngintest.com{
server 192.168.164.130:8081 weight=1;
server 192.168.164.130:8082 weight=1;
}
在http节点下找到location节点修改
location / {
root html;
index default.aspx; #修改主页为default.aspx
#设置主机头和客户端真实地址,以便服务器获取客户端真实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;
}
修改完成配置文件以后记得重启nginx服务,最终完整配置文件信息如下
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index default.aspx;
# index index.html index.htm;
#其中ngintest.com 对应着upstream设置的集群名称
proxy_pass http://ngintest.com;
#设置主机头和客户端真实地址,以便服务器获取客户端真实IP
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
#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;
#}
}
#服务器集群#
upstream ngintest.com{
server 192.168.164.130:8081 weight=1;
server 192.168.164.130:8082 weight=1;
}
运行结果如下图:
在这里我想特别强调一点是 由于设置了
proxy_pass http://ngintest.com;
#设置主机头和客户端真实地址,以便服务器获取客户端真实IP
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
所以要自定义错误的时候这个 proxy_intercept_errors on; 一定要设置,否则捕捉不到定义的错误页。
关于nginx里面的详细配置,请自行百度查询,里面的内容太多,本文就不在阐述。
摘自
http://blog.csdn.net/u010533180/article/details/52784696
IIS+NGINX 负载web服务器的更多相关文章
- nginx高性能WEB服务器系列之六--nginx负载均衡配置+健康检查
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- 《Nginx高性能Web服务器》系列分享专栏
<Nginx高性能Web服务器>系列分享专栏 [作者:Poechant] Nginx是目前最流行的基于BSD-like协议.轻量级.高性能的HTTP服务器.反向代理服务器和电子邮件(SMT ...
- nginx高性能WEB服务器系列之九--nginx运维故障日常解决方案
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- nginx高性能WEB服务器系列之八--nginx日志分析与切割
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- nginx高性能WEB服务器系列之七--nginx反向代理
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- nginx高性能WEB服务器系列之五--实战项目线上nginx多站点配置
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- nginx高性能WEB服务器系列之四配置文件详解
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- nginx高性能WEB服务器系列之三版本升级
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- nginx高性能WEB服务器系列之二命令管理
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
随机推荐
- xampp lampp 改变网页root目录的方法
This is an old question but I haven't seen it properly answered yet. Here is what you need to do: In ...
- 修改jupyter notebook的默认打开地址
1)在Anaconda Prompt终端中输入下面命令,查看你的notebook配置文件在哪里: jupyter notebook --generate-config 2)通过搜索关键词:c.Note ...
- cocos2d-x JS 各类点、圆、矩形之间的简单碰撞检测
这里总结了一下点.圆.矩形之间的简单碰撞检测算法 (ps:矩形不包括旋转状态) 点和圆的碰撞检测: 1.计算点和圆心的距离 2.判断点与圆心的距离是否小于圆的半 isCollision: functi ...
- torch随机数 manual_seed
import torch seed = 2018 torch.manual_seed(seed) torch.cuda.manual_seed(seed) a=torch.rand([1,5]) # ...
- Rocketmq源码导入eclipse时报错的解决方法
1,Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-help-plugi ...
- php 去除变态空格字符方法,空格trim不掉问题解决思路
前言:今天过滤一段文本,后面有2个空格,用trim去不掉,用preg_match也去不掉,去网上翻阅了无数的方法,终于找到了非常好的一个解决方法.该文章来源于https://my.oschina.ne ...
- JavaScript 初知
JavaScript 初知 1.JavaScript 独立的语言2.浏览器就是JavaScript的语言解释器3.JavaScript 代码存在于HTML中4.单行注释用“//”.多行注释 /* */ ...
- ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails ()
centos7.5 删除表空间文件失败 问题: mysql> alter table country discard tablespace; ERROR 1451 (23000): Cannot ...
- FFT 快速傅里叶变换 学习笔记
FFT 快速傅里叶变换 前言 lmc,ikka,attack等众多大佬都没教会的我终于要自己填坑了. 又是机房里最后一个学fft的人 早背过圆周率50位填坑了 用处 多项式乘法 卷积 \(g(x)=a ...
- Linux md5sum 的用法
MD5 算法常常被用来验证网络文件传输的完整性,防止文件被篡改.MD5 全称是报文摘要算法,此算法对任意长度 的信息逐位计算,产生一个二进制长度为 128 位(十六进制长度 32 位)的报文摘要,不同 ...