3、nginx實現反向代理負載均衡

一、反向代理

需求:php

兩個tomcat服務經過nginx反向代理html

nginx服務器:192.168.101.3nginx

tomcat1服務器:192.168.101.5apache

tomcat2服務器:192.168.101.6後端

以下圖:tomcat

 

1.1. 啓動tomcat

tomcat使用apache-tomcat-7.0.57版本,在192.168.101.5192.168.101.6虛擬機上啓動tomcat服務器

1.2. nginx反向代理配置

根據上邊的需求在nginx.conf文件中配置反向代理,以下:session

#配置一個代理即tomcat1服務器

upstream tomcat_server1 {
server 192.168.101.5:8080; } #配置一個代理即tomcat2服務器 upstream tomcat_server2 { server 192.168.101.6:8080; }

  #配置一個虛擬主機app

 
 

    server {負載均衡

 
 

        listen 80; #監聽端口

 
 

        server_name aaa.test.com; #對外提供的域名,若是對內部,就用內網IP,或localhost

 
 

        location / {

 
 

                #域名aaa.test.com的請求所有轉發到tomcat_server1即tomcat1服務上

                proxy_pass http://tomcat_server1;

                #歡迎頁面,按照從左到右的順序查找頁面

                index index.jsp index.html index.htm;

        }

    }

 

    server {

        listen 80;

        server_name bbb.test.com;

        location / {

                 #域名bbb.test.com的請求所有轉發到tomcat_server2即tomcat2服務上

                  proxy_pass http://tomcat_server2;

                  index index.jsp index.html index.htm;

        }

    }

 

:若是你的應用沒在根目錄但在根目錄的子目錄,不想用戶每次訪問輸入www.aa.com/yingyong ,就需用到重寫.

  #重寫url
        location =/ {
          rewrite ^ /yingyong last;    #若是你的應用沒在根目錄但在根目錄的子目錄,不想用戶每次訪問輸入www.aa.com/yingyong ,就需用到重寫.
        }
        
        location /yingyong {     #獲取重寫位置
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
          proxy_pass http://yingyong_proxy;    #把請求轉向真實的後端服務
        }

若是在同一個域名下有多臺服務器提供服務,此時須要nginx負載均衡。

2. 負載均衡

 需求

nginx做爲負載均衡服務器,用戶請求先到達nginx,再由nginx根據負載配置將請求轉發至 tomcat服務器。

nginx負載均衡服務器:192.168.101.3

tomcat1服務器:192.168.101.5

tomcat2服務器:192.168.101.6

2.1. nginx實現負載均衡配置

根據上邊的需求在nginx.conf文件中配置負載均衡,以下:

upstream tomcat_server_pool{

        server 192.168.101.5:8080 weight=10; #weight默認爲1,權重越高處理的請求越高

        server 192.168.101.6:8080 weight=5;

        } 

    server {

        listen 80;

        server_name aaa.test.com;

        location / {

                 proxy_pass http://tomcat_server_pool;

                 index index.jsp index.html index.htm;

        }

    }

注:

節點說明:

http節點裏添加:

#定義負載均衡設備的 Ip及設備狀態

upstream myServer {   

    server 127.0.0.1:9090 down;

    server 127.0.0.1:8080 weight=2;

    server 127.0.0.1:6060;

    server 127.0.0.1:7070 backup;

}

 

在須要使用負載的Server節點下添加

proxy_pass http://myServer;

 

upstream 每一個設備的狀態:

down 表示單前的server暫時不參與負載

weight  默認爲1.weight越大,負載的權重就越大。

max_fails :容許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤

fail_timeout:max_fails 次失敗後,暫停的時間。

backup: 其它全部的非backup機器down或者忙的時候,請求backup機器。因此這臺機器壓力會最輕。

 附貼一個我目前使用的配置,方便之後查看。

#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;

upstream tomcat_server {
            server XXX.XX.XX.XX:8080;
        }

    #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;

    server {
        listen       80;
        server_name  laoyeye.net;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            rewrite (.*) http://www.laoyeye.net;
        }

        #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;
        #}
    }
    server {

        listen 80;

        server_name www.laoyeye.net;

        location / {

                 #域名www.laoyeye.net的請求所有轉發到tomcat_server即tomcat服務上
                 proxy_pass http://tomcat_server;
              proxy_set_header Host $host:$server_port;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Real-PORT $remote_port;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                 index index.jsp index.html index.htm;

        }

    }
                 
    # 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;
    #    }
    #}

}
相關文章
相關標籤/搜索