1. 程式人生 > >Nginx負載均衡在windows上的配置

Nginx負載均衡在windows上的配置

一、下載Nginx windows部署包

下載後解壓到c或d盤

二、命令啟動服務

cmd: start nginx
nginx -s stop quick exit
nginx -s quit graceful quit
nginx -s reload changing configuration, starting a new worker, quitting an old worker gracefully
nginx -s reopen reopening log files

三、測試服務是否安裝成功

在瀏覽器輸入http://localhost,如果頁面顯示如下,則成功

如果error.log中出現:2011/06/16 15:23:55 [emerg] 7136#4040: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions),則未安裝成功,80埠被佔用了,在nginx.conf檔案中把listen埠改成其他的埠比如8099.

四、例項搭建

    首選:我們要在我們的iis上面把我們做好的web應用部署上去,部署到不同的機器上面,設定好對應的ip和埠號,因為我在本機模擬出效果,所以我就在本機的iis上面部署了2個web應用,第一個web應用部署在localhost:8011埠,第二個應用部署為localhost:8012埠,同時為了看到演示效果,我們把裡面的WebForm1.aspx頁面做了一個標記,裡面標記為:第幾個web應用程式的頁面,實際我們部署系統,不需要這樣做,就是把我們的一個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

五、引數設定

配置C伺服器(前端負載轉發伺服器)nginx的配置檔案 nginx.conf
以下標紅的就是需要配置的.其中ip_hash很重要(可以保證每個訪客可以固定一個後端,保證session不會出問題)
#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 localhost
{
ip_hash;
server 192.168.0.1:81;
server 192.168.0.2:81;
}
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://localhost; 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 .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; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }