NGINX学习笔记 WINDOWS平台
中型网站负载均衡架构
客户端 ----负载均衡器 ------反向代理服务器-----------WEB服务器---数据库
IE NGINX SQUID APACHE SQL
NGINX用途分为两类
一类 是只做 负载均衡和冗余
#user nobody;
worker_processes 1;
#pid logs/nginx.pid;
events {
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;
#gzip on;
upstream www.songfeifei.com.cn {
server 192.168.1.120:8000 weight=2;
server 192.168.1.130:80 weight=1;
}
#weight参数是设置服务器负载比例,常用于服务器性能不一致的情况。
server {
listen 80;
server_name 192.168.1.120;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://www.songfeifei.com.cn;
proxy_redirect default;
}
#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;
}}}
一类是做高效的WEB服务器。即不同动态页面 使用不同的节点进行解析。可以是本机的IIS引擎和 PHP FASTCGI解析。
比如 NGINX监听80端口。
把ASP请求转发到 127.0.0.1:801 就是IIS监听的端口
把PHP请求转发到127.0.01:802 就是NGINX FASTCGI监听的端口。
以下是nginx配置文件,有一点要注意,不管先装或者后装IIS,都需要修改IIS的默认端口,然后相应的修改配置文件里的IIS相关端口
#asp(aspx)支持,将客户端的请求转交给IIS
location ~* \.(asp|aspx|asa)$ {
root /host/wwwroot/www;
index index.asp index.aspx;
proxy_pass http://127.0.0.1:88;
proxy_set_header X-Real-IP $remote_addr;
}
#java(jsp)支持,将客户端的请求转交给resin
location ~* \.(jsp|do)$ {
root /host/wwwroot/www;
index index.jsp index.do;
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Real-IP $remote_addr;
}
#将客户端的请求转交给fastcgi
location ~ .*\.(php|php5|php4|shtml|xhtml|phtml|htm|html|shtml|shtm)?$ {
fastcgi_pass 127.0.0.1:9000;
include /host/nginx/conf/fastcgi_params;
}
目的是借助nginx的处理能力把php和静态等页面交给nginx处理,iis只负责处理asp和aspx相关内容,resin只处理java相关内容,所有内容从nginx 80端口输出
关于实验 两台虚拟机 一台真实机器
虚拟机A 安装NGINX 80端口 , IIS支持ASP 801端口 192.168.1.120
虚拟机B 安装IIS 80端口 PHP MYSQL 192.168.1.130
关于他们的网页目录,放置 ZBLOG 程序和DZ论坛。
第一个实验
实现内网两台机器均衡负载。即访问192.168.120 的主机是否能打开 A的页面 和B的页面。
模拟故障。设置虚拟机A的IIS停止工作。观察是否能够继续访问,NGINX是否把HTTP请求转发给 虚拟机B的 IIS。
结果
;可以实现每一个请求准确转发给 A B机器。也可以实现故障检测和自动冗余功能。
注意
;作为负载的两台WEB服务器应该保证 页面内容完全一致。在两台WEB页面均正常的情况下,NGINX会轮询每一个请求,所以应该使用FTP软件实时同步网页数据。比如论坛应该使用 远程附件功能和 后端数据库,已实现不同用户看到相同内容。
注意;实验中使用的是NGINX WINDOS版 nginx-0.7.66稳定版。
第二个实验
设在虚拟机A的NGINX配置,客户端请求的 ASP脚本通过虚拟机的IIS进行解析。PHP脚本通过虚拟机B的IIS PHP进行解析
即 ASP 192.168.1.120:801
PHP 192.168.1.130:80
结果:
经过测试 可以实现。本机的PHP请求转发到另外一台服务器进行解析,但是 由于只转发PHP。会导致网页显示不完整。HTML还是在本服务器。所以,两大功能最好不要混用。
结论:
不同端口映射为不同脚本解释引擎最好只在本机执行。
负载均衡可以在多台服务器直接执行。
第三个实验
可以做域名顶替实验。就是监听本地端口,然后把请求转发给 其他网站。这样百度会认为该网站的内容是你的域名下。
server
{
listen 80;
server_name 301302.net;
location / {
proxy_pass www.songfeifei.com.cn;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
结果
:可以实现 域名替换首页访问
结论
:无实际应用价值。
WINDOWS下 NGINX常用调试命令
c:\nginx\nginx.exe -t 检测脚本是否正确
start c:\nginx\nginx.exe 启动NGINX
cd c:\nginx\
nginx -s stop 关闭NGINX 也可以在任务管理器 关闭进程 一般有两个进程。