【高可用HA】Nginx (1) —— Mac下配置Nginx Http負載均衡(Load Balancer)之101例項

Richaaaard發表於2015-12-09

【高可用HA】Nginx (1) —— Mac下配置Nginx Http負載均衡(Load Balancer)之101例項


nginx版本: nginx-1.9.8

參考來源:

nginx.org

【高可用HA】Apache (2) —— Mac下安裝多個Apache Tomcat例項

Nginx on Mac OS X Snow Leopard in 2 Minutes

51cto:Nginx+Tomcat負載均衡配置

csdn:Nginx+Tomcat負載均衡配置

iteye:圖文解說:Nginx+tomcat配置叢集負載均衡

安裝

首先

從nginx官方網站下載

編譯配置

  1. 執行命令

     $ ./configure --prefix=/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a --with-http_ssl_module

    執行結果末尾幾行

     ...
     nginx error log file: "/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/logs/error.log"
     nginx http access log file: "/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/logs/access.log"
     nginx http client request body temporary files: "client_body_temp"
     nginx http proxy temporary files: "proxy_temp"
     nginx http fastcgi temporary files: "fastcgi_temp"
     nginx http uwsgi temporary files: "uwsgi_temp"
     nginx http scgi temporary files: "scgi_temp"    
  2. 執行命令

     $ make

    執行結果末尾幾行

     ...
     objs/src/http/modules/ngx_http_upstream_zone_module.o \
     objs/ngx_modules.o \
     -lpcre -lssl -lcrypto -lz
     /Library/Developer/CommandLineTools/usr/bin/make -f objs/Makefile manpage
     sed -e "s|%%PREFIX%%|/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a|" \
     -e "s|%%PID_PATH%%|/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/logs/nginx.pid|" \
     -e "s|%%CONF_PATH%%|/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/conf/nginx.conf|" \
     -e "s|%%ERROR_LOG_PATH%%|/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/logs/error.log|" \
     < man/nginx.8 > objs/nginx.8
  3. 執行命令

     $ sudo make install

    執行結果末尾幾行

     test -d '/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/logs'        || mkdir -p '/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/logs'
     test -d '/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/logs' ||         mkdir -p '/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/logs'
     test -d '/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/html'        || cp -R html '/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a'
     test -d '/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/logs' ||         mkdir -p '/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/logs'
  4. 修改伺服器啟動配置

    • 將埠修改成85(與其他文章中httpd的埠區分),並修改location配置Proxy

      server {
      listen 85;
      server_name localhost;

        #charset koi8-r;
      
        #access_log  logs/host.access.log  main;
      
        # location / {
        #     root   html;
        #     index  index.html index.htm;
        # }
      
        location / {  
            proxy_connect_timeout   3;  
            proxy_send_timeout      30;  
            proxy_read_timeout      30;  
            proxy_pass http://localhost;  
        }

      ...

    • 增加轉發路由的配置,並指向之前配好的兩臺Tomcat伺服器

        upstream localhost {  
            #根據ip計算將請求分配各那個後端tomcat,許多人誤認為可以解決session問題,其實並不能。  
            #同一機器在多網情況下,路由切換,ip可能不同  
            #ip_hash;   
            server localhost:8081;  
            server localhost:8082;  
        }           

執行

執行命令

$ ./sbin/nginx

提示錯誤

nginx: [alert] could not open error log file: open() "/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/logs/error.log" failed (13: Permission denied)
2015/12/09 16:26:55 [emerg] 16550#0: mkdir() "/Users/Richard/Documents/Dev/servers/cluster/nginx/node-a/client_body_temp" failed (13: Permission denied)    
*這是因為nginx沒有相應的許可權

執行命令授權或者以sudo執行

$ sudo ./sbin/nginx

啟動

$ sudo ./sbin/nginx -s stop 

測試

用瀏覽器訪問localhost:85,頁面中會交替出現"Node-A!!"和"Node-B!!"

【高可用HA】Nginx (1) —— Mac下配置Nginx Http負載均衡(Load Balancer)之101例項

結束

相關文章