Windows環境下使用Nginx搭建負載平衡

來源:互聯網
上載者:User

標籤:blog   share   char   gzip   work   info   zip   執行   exists   

前言

最近沒有什麼事情,喜歡總結並學習東西!前幾天寫來一個Session共用,那麼我們為什麼需要Session共用?因為我們的應用程式分布在多個伺服器上,為了合理分配使用者的請求,就需要用到負載平衡技術(將請求/資料【均勻】分攤到多個操作單元上執行)。

怎樣實現負載平衡?

1.  使用F5硬體來實現

2. 使用Nginx 工具來搭建一個。

下面我們就講解一下,在Windows環境下,怎樣部署Nginx及常見問題。

一:下載Nginx

去官網下載最新的 Windows-1.11.10 並解壓到英文目錄下

二:Nginx配置

找到 conf 目錄裡的 nginx.conf 檔案,配置Nginx

 

#user  nobody;#指定nginx進程數worker_processes  1;#全域錯誤記錄檔及PID檔案#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伺服器,利用它的反向 Proxy功能提供負載平衡支援http {    #設定mime類型,類型由mime.type檔案定義    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 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出檔案,對於普通應用,        sendfile        on;    #tcp_nopush     on;    #連線逾時時間    #keepalive_timeout  0;    keepalive_timeout  65;    #開啟gzip壓縮    #gzip  on;    #設定負載平衡的伺服器列表 支援多組的負載平衡,可以配置多個upstream  來服務於不同的Server.    #nginx 的 upstream 支援 幾 種方式的分配     #1)、輪詢(預設) 每個請求按時間順序逐一分配到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。     #2)、weight 指定輪詢幾率,weight和訪問比率成正比,用於後端伺服器效能不均的情況。 跟上面樣,指定了權重。    #3)、ip_hash 每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題。     #4)、fair           #5)、url_hash #Urlhash    upstream mysvr {      #weigth參數表示權值,權值越高被分配到的幾率越大         #1.down 表示單前的server暫時不參與負載      #2.weight 預設為1.weight越大,負載的權重就越大。           #3.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這台機器壓力會最輕。        #server 192.168.1.116  down;      #server 192.168.1.116  backup;      server 192.168.1.121  weight=1;      server 192.168.1.122  weight=2;    }    #配置Proxy 伺服器的地址,即Nginx安裝的伺服器位址、監聽連接埠、預設地址    server {        #1.偵聽80連接埠         listen       80;        #對於server_name,如果需要將多個網域名稱的請求進行反向 Proxy,可以配置多個server_name來滿足要求        server_name  localhost;                #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            # 預設首頁目錄在nginx安裝目錄的html子目錄。            root   html;            index  index.html index.htm;                       proxy_pass http://mysvr; #跟載均衡伺服器的upstream對應           }        #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;    #    }    #}}
三:啟動Nginx

 cmd 進入Nginx解壓目錄 執行 start nginx啟動Nginx服務

啟動後如何檢查是否啟動成功呢? 輸入命令 tasklist /fi "imagename eq nginx.exe"   看到以下資訊說明啟動成功了

一切就緒,訪問一下server 裡配置的 server_name 是不是被重新導向到 upstream配置的伺服器上了,是不是很簡單!

 

四:常見問題

如果啟動失敗 可以看下logs目錄下 error.log 檔案裡的錯誤資訊。

我在第一次安裝的時遇到兩個錯誤,也是最容易碰到的問題,在這裡列出來方便大家碰到相同的問題時快速解決。

1.  連接埠佔用問題

我的設定檔裡服務偵聽的是 80 連接埠,由於機器上部署了IIS,80連接埠被預設網站佔用,把網站關閉就可以了,這個問題在錯誤記錄檔裡記錄是這樣的。

failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

2.Nginx所在目錄有中文

錯誤記錄檔大致輸出一下內容

failed (1113: No mapping for the Unicode character exists in the target multi-byte code page)

3. 啟用緩衝時報錯

2015/01/15 17:26:50 [emerg] 17068#20356: shared zone "cache_one" has no equal addresses: 02CF0000 vs 02A200002015/01/15 17:26:50 [alert] 11536#11228: worker process 17068 exited with code 1

我一直沒有找到解決的方法,有人說重啟服務,或者緩衝設定大一點就可以了,我試了一下沒有用的,官網 原文是這樣講的,只能認為windwos下無解了。

: The cache and other modules which require shared memory support do : not work in Windows Vista and later due to address space layout : randomization being enabled in these Windows versions.

五.常用指令

1:start nginx  啟動服務

2:nginx -s reload  修改配置後重新載入生效

3:nginx -s reopen  重新開啟記錄檔

4:nginx -t -c /path/to/nginx.conf 測試nginx設定檔是否正確

5:nginx -s stop  快速停止nginx

6:tasklist /fi "imagename eq nginx.exe"  查看nginx示範開啟

 

有參考網上的文章,有自己的理解,歡迎大家指正。。。。。。

 

Windows環境下使用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.