windows下搭建nginx負載均衡


1.下載Windows版本的Nginx 

http://nginx.org/en/download.html

  2.解壓Nginx包,配置conf文件下的nginx.conf文件

 

 

3.配置說明:

#user  nobody;
#N工作進程數,默認為1
worker_processes  1;

#錯誤日志保存路徑
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid文件路徑
#pid        logs/nginx.pid;

#工作模式及連接數上限
events {
    #單個后台worker process進程的最大並發鏈接數,默認1024,可以按照最大客戶端連接數max_clients配置
    #nginx作為http服務器的時候:
    #max_clients = worker_processes * worker_connections

    #nginx作為反向代理服務器的時候:
    #max_clients = worker_processes * worker_connections/4
    worker_connections  1024;
}

#http服務器配置,做web服務器或反向代理
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;#開啟高效傳輸模式
    
    #在nginx中,tcp_nopush配置與tcp_nodelay“互斥”。它可以配置一次發送數據包的大小。
    #也就是說,數據包累積到一定大小后就發送。
    #在nginx中tcp_nopush必須和sendfile配合使用。
    #tcp_nopush     on;

    #連接超時時間 單位是秒
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    #啟用gzip壓縮
    #gzip  on;

    upstream web70{ #有域名的可以用域名
        #負載的一個實例,可以是外網IP
        #默認輪詢,把每個請求按順序逐一分配到不同的server,如果server掛掉,能自動剔除。

        #其他配置:
        #1.在負載的實例后面加weigth參數,表示權值,權值越高被分配到的幾率越大
        #譬如server 127.0.0.1:71 weight=1;   

        #2.把請求分配到連接數最少的server
        #least_conn;

        #3.每個請求會按照訪問ip的hash值分配,這樣同一客戶端連續的Web請求都會被分發到同一server進行處理
        #可以解決session的問題。如果server掛掉,能自動剔除。
        #ip_hash;

        server 127.0.0.1:71; #weight=1;   
        server 127.0.0.1:72; #weight=1; 
    }

    #對外web服務器實例
    server {
        listen       70;#監聽端口
        server_name  localhost;#服務器名,默認localhost
        
        #默認字符編碼,默認koi8-r,可改為utf-8
        #charset koi8-r;
        charset utf-8;
        
        #本服務器實例日志路徑
        #access_log  logs/host.access.log  main;

        #默認請求配置
        location / {
            root   html;#web服務器根目錄,用來放網站程序的目錄
            index  index.html index.htm;#默認首頁
            proxy_pass  http://web70; #請求轉向的url地址,反向代理則填寫對應upstream后面的域名
            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 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;
    #    }
    #}

}

4.打開控制台進入Nginx目錄,輸入nginx.exe命令來啟動Nginx

  

 5.打開任務管理器可以看到Nginx已經在運行了,此時控制台也可以關閉了

 

6.配置負載均衡web實例

  

 7.然后刷新Nginx的服務器,顯示第一個負載的web實例

   8.再刷新下,顯示第二個負載的web實例

  Nginx其他命令:

nginx.exe -s stop     //停止nginx
nginx.exe -s reload  //重新加載nginx
nginx.exe -s quit     //退出nginx

 Nginx配置文件下下載 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM