Linux(Centos)安装Nginx,后台搭建IIS实现负载均衡(二)
2017/11/20 21:12:42
之前完成了WMWare下安装CentOS,Win7,并在CentOS下安装了Nginx。
接下来说
如何配置Nginx来实现负载均衡
配置IP,准备IIS:
配置虚拟机下的两个Win7的IP,假如为:192.168.186.131 和 192.168.186.132,并在两个Win7下安装IIS,安装完IIS后,修改默认页内容,一个静态页输出131,一个静态页输出132。
现在,我们假如在Web中访问131的机器,则看到一个写着131的Web界面,访问132机器的话,则看到一个写着132的Web界面。
两个Win7的防火墙要关掉,否则其它机器访问不了。
配置Nginx
现在配置Nginx,将对CentOS的请求分担到131和132机器上。这里主要是对配置文件的配置。
在目录 /usr/local/nginx/conf 下找到 nginx.conf 文件,打开并修改。
修改后的结果如下:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #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 web_pool{ server 192.168.186.132:80 weight=10; server 192.168.186.131:80 weight=10; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://web_pool; } #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; # } #} }
重要的部分如下:
upstream web_pool{
server 192.168.186.132:80 weight=10;
server 192.168.186.131:80 weight=10;
}
在Server上面定义了一个集群,集群名字为:web_pool,这里随便定义。upstream:上游的意思
weight为权重,权重越大,处理的请求越多。
然后在location / 中添加:
proxy_pass http://web_pool;
来启用代理
修改完配置后,保存文件,并让nginx加载新的配置,命令如下:
./nginx -s reload
测试成果:
现在在CentOS中输入localhost,会看到显示131,或132,不断刷新,会看到两个值在交替。
版权说明
作者:SQBER
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
{0}
{5}
{1}
{2}回复
{4}
*昵称:
*邮箱:
个人站点:
*想说的话: