Nginx簡單實現網站的負載平衡

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   使用   

 在大型網站搭建時,都會考慮如果使用者量每日不斷增加,大量的並發訪問,會不會給網站、資料庫帶來崩盤的災難。今天我們就討論一下,現實中如何解決這些問題的一套最為容易實現的方案。

控制並發,大家都會首先考慮的就是分布式、負載平衡等經常聽到的It名詞。那網站如何才能實現負載平衡呢,除了世面上的一些負載平衡器外,我們有哪些軟體上的解決方案呢,這時候,Nginx、lvs 等名詞就會在腦海中浮現。那這些負載平衡的軟體如何使用呢,如何讀者是.net工程師,大家會選擇Nginx,因為它支援Windows伺服器,這時候,好多網友會批判一下,說lvs更好更優秀。其實大家不必太在意,其實都一樣,只要你能掌控它們就Ok,各有優劣。Nginx配置簡單,在中小型項目中使用更為方便,下面我們看下Niginx在Windows下的配置,lvs在Linux的配置下一篇再寫。

概述:使用Nginx搭建反向伺服器,實現網站伺服器叢集負載平衡

1、下載Nginx——Windows版,(nginx-1.6.2.zip)在部落格末端可下載,解壓

2、使用winsw-1.8-bin(windows服務工具).exe工具將Niginx發布成Domain模式,通過Windows服務的方式控制Niginx的運行。在部落格末端可下載

(1)配置 winsw啟動並執行Xml檔案,如:將Winsw工具移植到解壓的Niginx檔案下,將工具名字修改成“nginxServer.exe”,建立一個xml設定檔“nginxServer.xml”檔案,它跟工具的名字一致,當然你也可以不要改名字。xml檔案如下:

 1 <?xml version="1.0" encoding="UTF-8" ?> 2 <service> 3 <id>nginx</id> 4 <name>ngixServer</name> 5 <description>High Performance Nginx Service</description> 6 <executable>E:\2014newT\Practise\nginx1.62Server\nginx.exe</executable> 7 <logpath>E:\2014newT\Practise\nginx1.62Server\</logpath> 8 <logmode>roll</logmode> 9 <depend></depend>10 <startargument>-p E:\2014newT\Practise\nginx1.62Server</startargument>11 <stopargument>-p E:\2014newT\Practise\nginx1.62Server -s stop</stopargument>12 </service>
winsw 檔案配置

配置很簡單主要指定Nginx.exe的位置\log位置等,可以Google一下Winsw看看具體的配置資訊。

(2)點擊nginxServer.exe安裝服務,如果你是Win8以上的系統可能裝不上,是因為相容問題,調製相容Win7模式,以管理員的身份運行即可,

這時查看Windows服務,啟動NginxServer服務,

這時,ngixServer服務成功啟動了。

3、修改Nginx配置,將代理指向伺服器叢集,實現網站負載平衡

在解壓的Nginx檔案夾下找到conf/nginx.conf檔案,開啟進行配置:

  1 #user  nobody;  2 worker_processes  4;#啟動的線程數  3   4 #錯誤的位置和層級    5 #error_log  logs/error.log;  6 #error_log  logs/error.log  notice;  7 #error_log  logs/error.log  info;  8   9 #pid        logs/nginx.pid;#pid進程檔案的位置   10  11  12 events { 13     worker_connections  1024;#每個進程的最大串連數   14 } 15  16  17 http { 18     include       mime.types; 19     default_type  application/octet-stream; 20      #nginx日誌格式定義,在下面可以進行引用   21     #log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘ 22     #                  ‘$status $body_bytes_sent "$http_referer" ‘ 23     #                  ‘"$http_user_agent" "$http_x_forwarded_for"‘; 24  25     #access_log  logs/access.log  main; 26  27     sendfile        on; 28     #tcp_nopush     on; 29  30     #keepalive_timeout  0; 31     keepalive_timeout  65; 32     upstream linuxidc.com{ 33         server 127.0.0.1:8091;    #伺服器叢集A  34          server 127.0.0.1:8092;    #伺服器叢集B 35     } 36     #gzip  on; 37  38     server { 39         listen       8090; 40         server_name  localhost; 41  42         #charset koi8-r; 43  44         #access_log  logs/host.access.log  main; 45  46         location / { 47            root  html; 48            index  index.html index.htm default.aspx; 49            proxy_pass  http://linuxidc.com; 50            proxy_redirect  default; 51         } 52  53         #error_page  404              /404.html; 54  55         # redirect server error pages to the static page /50x.html 56         # 57         error_page   500 502 503 504  /50x.html; 58         location = /50x.html { 59             root   html; 60         } 61  62         # proxy the PHP scripts to Apache listening on 127.0.0.1:80 63         # 64         #location ~ \.php$ { 65         #    proxy_pass   http://127.0.0.1; 66         #} 67  68         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 69         # 70         #location ~ \.php$ { 71         #    root           html; 72         #    fastcgi_pass   127.0.0.1:9000; 73         #    fastcgi_index  index.php; 74         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; 75         #    include        fastcgi_params; 76         #} 77  78         # deny access to .htaccess files, if Apache‘s document root 79         # concurs with nginx‘s one 80         # 81         #location ~ /\.ht { 82         #    deny  all; 83         #} 84     } 85  86  87     # another virtual host using mix of IP-, name-, and port-based configuration 88     # 89     #server { 90     #    listen       8000; 91     #    listen       somename:8080; 92     #    server_name  somename  alias  another.alias; 93  94     #    location / { 95     #        root   html; 96     #        index  index.html index.htm; 97     #    } 98     #} 99 100 101     # HTTPS server102     #103     #server {104     #    listen       443 ssl;105     #    server_name  localhost;106 107     #    ssl_certificate      cert.pem;108     #    ssl_certificate_key  cert.key;109 110     #    ssl_session_cache    shared:SSL:1m;111     #    ssl_session_timeout  5m;112 113     #    ssl_ciphers  HIGH:!aNULL:!MD5;114     #    ssl_prefer_server_ciphers  on;115 116     #    location / {117     #        root   html;118     #        index  index.html index.htm;119     #    }120     #}121 122 }
nginx.conf 配置

不用害怕,修改的地方很少。

(1)worker_processes  4;#啟動的線程數 一般為你Proxy 伺服器的核心數

(2)在“HTTP”括弧中設定管理員群的網站發布的ip地址和連接埠號碼

upstream linuxidc.com{
server x.x.x.x:8091; #伺服器A
server x.x.x.x:8092; #伺服器B

}

(3)配置Proxy 伺服器的地址,即Nginx安裝的伺服器位址、監聽連接埠、預設地址

server {
listen 8090;    #監聽連接埠
server_name localhost;   #伺服器Ip地址

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm default.aspx;  #預設網站首頁地址
proxy_pass http://linuxidc.com;
proxy_redirect default;
}

重啟Nginx Windows服務,收工完成,建立一個網站,ip、連接埠號碼、預設首頁要與Proxy 伺服器Server配置一致哦,試試吧。。

代碼奉上:

http://pan.baidu.com/s/1pJukQ2R

 

Nginx簡單實現網站的負載平衡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.