使用Nginx負載均衡搭建高性能.NETweb應用程序二
來源:程序員人生 發布時間:2015-01-16 08:17:11 閱讀次數:3277次
在文章《使用Nginx負載均衡搭建高性能.NETweb利用程序1》中,讓我們對Nginx有了1個初步認識,下面我們將在windows平臺下面使用Nginx演示集群部署我們的web利用。
1、下載Nginx部署包
到Nginx官網去下載1個windows平臺下面的Nginx部署包,目前我下載的是1個nginx⑴.6.2版本的。
2、命令啟動服務
啟動:start nginx.exe
停止:nginx -s stop
重新加載: nginx -s reload
3、實例搭建
首選:我們要在我們的iis上面把我們做好的web利用部署上去,部署到不同的機器上面,設置好對應的ip和端口號,由于我在本機摹擬出效果,所以我就在本機的iis上臉部署了2個web利用,第1個web利用部署在localhost:8011端口,第2個利用部署為localhost:8012端口,同時為了看到演示效果,我們把里面的WebForm1.aspx頁面做了1個標記,里面標記為:第幾個web利用程序的頁面,實際我們部署系統,不需要這樣做,就是把我們的1個web利用部署到不同機器上面的服務器上,下面所示。
web利用1地址:http://localhost:8011/WebForm1.aspx
web利用2地址:http://localhost:8012/WebForm1.aspx
(1)啟動Nginx服務

(2)修改Nginx配置項,具體配置說明,我們在參數設置部門說明,然后驗證服務是不是正常啟動。

(3)訪問地址http://localhost:8010/WebForm1.aspx視察結果


(4)這樣我們就能夠摹擬出負載均衡效果了,ok
4、其他說明
(1)配置域名訪問:首先我們要在Nginx中添加域名設置,其次我們要在本機設置127.0.0.1映照為域名

這樣我們就能夠這樣訪問了:http://huangxiang:8010/WebForm1.aspx
(2)配置Ngnix啟動的進程數量,我們可以在進程中關注其進程數量變化

5、參數設置
#定義Nginx運行的用戶和用戶組
#user nobody;
#Nginx進程數,建議和cpu總內核1致
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
#定義單個進程的最大連接數(實際最大連接數要除以2)
worker_connections 1024;
}
#定義http
服務器
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 huangxiang.com { #
服務器集群名字
#server 172.16.21.13:8081 weight=1;#
服務器配置 weight是權重的意思,權重越大,分配的幾率越大。
#server 192.168.1.186:8081 weight=1;
#server 172.16.1.14:8081 weight=2;
#server 172.16.1.15:8081 weight=1;
#server 172.16.1.15:80 weight=1;
server 127.0.0.1:8011 weight=1;
server 127.0.0.1:8012 weight=2;
}
#虛擬機主機配置
server {
listen 8010;#端口號
server_name localhost huangxiang.com;#域名可以有多個,多個用空格分開
#charset koi8-r;
#
access_log logs/host.
access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location / {
proxy_pass http://huangxiang.com;
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;
}
# 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 .ht
access 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;
# }
#}
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈