1. 程式人生 > >Nginx配置使用upstream負載均衡和proxy_cache快取

Nginx配置使用upstream負載均衡和proxy_cache快取

ngx_http_upstream_module模組:Nginx負載均衡模組

Syntax:      upstreamname { ... }

Default:     —

Context:    http

Defines a group of servers. Servers can listen ondifferent ports. In addition, servers listening on TCP and UNIX-domain socketscan be mixed.

簡單示例:

    upstream backend {

        server backend1.example.com weight=5;

        server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;

        server unix:/tmp/backend3;

        server backup1.example.com  backup;

    }

配置示例:

    upstream backend {

        server backend1.example.com       weight=5;

        server backend2.example.com:8080;

        server unix:/tmp/backend3;

        server backup1.example.com:8080   backup;

        server backup2.example.com:8080   backup;

    }

    server {

        location / {

            proxy_pass http://backend;

        }

    }

前置條件,編輯後端web 伺服器測試頁面

nginx作為後端web伺服器:192.168.88.130:8080  test.field.com

[root@testhtml]# pwd

/usr/share/nginx/html

[root@testhtml]# cat  index.html

<h1>Welcometo Nginx on test.field.com!!</h1>

<p><em>If you see this page,test OK! </em></p>

<p>Thank you!</p>

[root@test html]#

Http作為後端web伺服器:192.168.88.129:80  web2.field.com

[root@web2html]# pwd

/var/www/html

[root@web2html]# cat index.html

<h1>Welcometo Http on web2.field.com!!</h1>

<p>If yousee this page,test ok. </p>

<p><em>Thank you!</em></p>

案例1、負載均衡基礎應用

編輯nginx.conf,新增如下內容:

   upstreamupservers {

        server  192.168.88.129;

        server  192.168.88.130:8080;

    }

編輯conf.d/default.conf,新增如下內容:

    location/field/ {

     proxy_pass  http://upservers/;

    }

    location ~*\.(jpg|png|gif)$ {

     proxy_pass  http://upservers;

}

[root@wwwnginx]# vi nginx.conf

user  nginx;

worker_processes  1;

error_log   /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       /etc/nginx/mime.types;

    default_type   application/octet-stream;

    log_format main  '$remote_addr - $remote_user[$time_local] "$request" $http_host '

                      '$status $body_bytes_sent"$http_referer" '

                      '"$http_user_agent""$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log  main;

    proxy_cache_path  /cache/nginx/ levels=1:1keys_zone=mycache:32m;   

upstream  upservers {

 server  192.168.88.129;

server  192.168.88.130:8080;

}

    sendfile        on;

    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip on;

    include /etc/nginx/conf.d/*.conf;

}

[[email protected]]# vi default.conf

server {

    listen      8080;

    server_name localhost;

    #charset koi8-r;

    #access_log /var/log/nginx/log/host.access.log main;

    location / {

       root  /usr/share/nginx/html;

       # proxy_pass http://192.168.88.130/;

       index index.html index.php index.htm;

    }

    location /field/ {

proxy_pass  http://upservers/;

     proxy_set_header Host      $host;

     proxy_set_header X-Real-IP $remote_addr;

    }

    location ~* \.(jpg|png|gif)$ {

proxy_pass   http://upservers;

     proxy_set_header X-Real-IP $remote_addr;

    }

    error_page  500 502 503 504  /50x.html;

    location = /50x.html {

        root   /usr/share/nginx/html;

    }

}

[[email protected]]# service nginx restart

停止 nginx:[確定]

正在啟動 nginx:[確定]

[[email protected]]#

wind7訪問 http://192.168.88.131:8080/field/

重新整理頁面,頁面內容會在【Welcome to Http on web2.field.com!!!】和【Welcome to Nginx on test.field.com!!】上跳轉

案例2、負載均衡定義加權輪詢

定義加權輪詢,預設為輪詢

    upstream  upservers {

        server  192.168.88.129  weight=n;

        server  192.168.88.130  weight=m;

    }

[root@wwwnginx]# vim nginx.conf

    upstream  upservers {

server  192.168.88.129  weight=2;

server  192.168.88.130:8080  weight=1;

}                                  

[root@wwwnginx]# service nginx reload

重新載入 nginx:[確定]

依次重新整理頁面,頁面內容會在【Welcome to Http on web2.field.com!!!】和【Welcome to Nginx on test.field.com!!】上以2:1跳轉

案例3、負載均衡定義根據客戶端IP進行排程

ip_hash:根據客戶端IP進行排程:

每一個客戶端訪問時都生成一個hash碼,來自同一客戶端的定向到同一伺服器

    upstream  upservers {

        ip_hash;

    }

[root@wwwnginx]# vim nginx.conf

    upstream  upservers {

           ip_hash;

server  192.168.88.129  weight=2;

server  192.168.88.130:8080  weight=1;

    }

[root@wwwnginx]# service nginx configtest

nginx: theconfiguration file /etc/nginx/nginx.conf syntax is ok

nginx:configuration file /etc/nginx/nginx.conf test is successful

[root@wwwnginx]# service nginx reload

重新載入 nginx:[確定]

始終定位到【Welcome to Http on web2.field.com!!!】頁面

始終定位到【Welcome to Nginx on test.field.com!!】頁面

[[email protected]]# curl  http://192.168.88.131:8080/field/

<h1>Welcometo Nginx on test.field.com!!</h1>

<p><em>If you see this page,test OK! </em></p>

<p>Thankyou!</p>

[[email protected]]#

案例4、後端伺服器健康狀態檢測:

max_fails=number 檢查number次失敗時定義為真實失敗;

fail_timeout=time 每次檢查失敗的超時時間;

[root@wwwnginx]# vim  nginx.conf

    upstream  upservers {

server  192.168.88.129  weight=2  max_fails=2  fail_timeout=1;

server  192.168.88.130:8080  weight=1  max_fails=2  fail_timeout=1;

    }

[root@wwwnginx]# service  nginx  reload

重新載入 nginx:[確定]

wind7訪問 http://www.field.com:8080/field/

重新整理頁面,頁面內容會在【Welcome to Http on web2.field.com!!!】和【Welcome to Nginx on test.field.com!!】上以2:1跳轉

測試:

1).關閉後端httpd服務

[root@web2html]# service  httpd  stop

停止 httpd:[確定]

[root@web2html]#

wind7訪問 http://www.field.com:8080/field/

頁面只顯示【Welcome to Nginx on test.field.com!!】

2).關閉後端Nginx服務

[[email protected]]# service  nginx  stop

停止 nginx:[確定]

[[email protected]]#

wind7訪問 http://www.field.com:8080/field/,

無法訪問,頁面顯示【An error occurred.】

3).依次開啟後端httpd、Nginx服務,訪問恢復。

案例5、手動標記狀態

backup:備用

down:下線

[root@wwwnginx]# vim nginx.conf

   upstream upservers {

server  192.168.88.129  max_fails=2  fail_timeout=1;

        server  192.168.88.130:8080  max_fails=2  fail_timeout=1 backup;

    }

[root@wwwnginx]# service  nginx configtest

nginx: theconfiguration file /etc/nginx/nginx.conf syntax is ok

nginx:configuration file /etc/nginx/nginx.conf test is successful

[root@wwwnginx]# service nginx reload

重新載入 nginx:[確定]

測試:

1).伺服器正常,只上線129主機的http服務

win7訪問http://192.168.88.131:8080/field/,重新整理頁面,始終定位到【Welcome to Http onweb2.field.com!!!】頁面

130主機用curl訪問http://192.168.88.131:8080/field/,始終定位到【Welcome to Http on web2.field.com!!!】頁面

[[email protected]]# curl  http://192.168.88.131:8080/field/

<h1>Welcometo Http on web2.field.com!!</h1>

<p>If yousee this page,test ok. </p>

<p><em>Thankyou!</em></p>

[[email protected]]#

2).關閉後端httpd服務,備用的130主機會上線。

[root@web2html]# service  httpd  stop

停止 httpd:[確定]

win7訪問http://192.168.88.131:8080/field/,重新整理頁面,始終定位到【Welcome to Nginx ontest.field.com!!】頁面

129主機用curl訪問http://192.168.88.131:8080/field/,始終定位到【Welcome to Nginx on test.field.com!!】頁面

[root@web2html]# curl  http://192.168.88.131:8080/field/

<h1>Welcometo Nginx on test.field.com!!</h1>

<p><em>If you see this page,test OK! </em></p>

<p>Thankyou!</p>

3).開啟129主主機後端httpd服務,備用的130主機會下線,129主機上線。

[root@web2html]# service httpd start

正在啟動 httpd:[確定]

win7訪問http://192.168.88.131:8080/field/,重新整理頁面,始終定位到【Welcome to Http on web2.field.com!!!】頁面

案例6、自定義相應報文首部: 

編輯default.conf,新增如下內容:

add_header X-Via $server_addr;

add_header X-Cache $upstream_cache_status;

[[email protected]]# vi  default.conf

server {

    listen      8080;

    server_name localhost;

add_headerX-Via $server_addr;

add_headerX-Cache $upstream_cache_status;

    #charset koi8-r;

    #access_log  /var/log/nginx/log/host.access.log main;

    location / {

       root  /usr/share/nginx/html;

       # proxy_pass  http://192.168.88.130/;

       index  index.html  index.php  index.htm;

    }

    location  /field/ {

     proxy_pass  http://upservers/;

     proxy_set_header  Host      $host;

     proxy_set_header  X-Real-IP  $remote_addr;

proxy_set_header        X-Forwarded-For  $proxy_add_x_forwarded_for;

    }

    location  ~* \.(jpg|png|gif)$  {

     proxy_pass  http://upservers;

     proxy_set_header  X-Real-IP  $remote_addr;

    }

}

[[email protected]]# service nginx reload

重新載入 nginx:[確定]

[[email protected]]#

win7訪問http://192.168.88.131:8080/field/,F12開啟控制檯

可以看到報文首部:

X-Via:192.168.88.131

案例7、負載均衡加入快取機制

 Syntax:    proxy_cache_path path [levels=levels][use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size][manager_files=number] [manager_sleep=time] [manager_threshold=time][loader_files=number] [loader_sleep=time] [loader_threshold=time][purger=on|off] [purger_files=number] [purger_sleep=time][purger_threshold=time];

Default:     —

Context:    http

快取檔案路徑定義levels=1:2,一級子目錄一個字元表示,2級子目錄兩個字元表示

總共有62*62*62個檔案

   proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;

file names in a cache will look like this:

   /data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c  

$upstream_cache_status 包含以下幾種狀態:

MISS:未命中,請求被傳送到後端

HIT:快取命中

EXPIRED:快取已經過期請求被傳送到後端

UPDATING:正在更新快取,將使用舊的應答

STALE:後端將得到過期的應答

簡單配置如下: 

配置快取:nginx.conf

 proxy_cache_path   /cache/nginx/ levels=1:1keys_zone=mycache:32m;

加入頭資訊:default.conf

    add_header  X-Via  $server_addr;

    add_header  X-Cache  $upstream_cache_status;

啟用快取:

     proxy_cache  mycache;

     proxy_cache_valid  200  1d;

#200 1d 表示這個zone中返回200的快取檔案如果在1天內沒有被訪問,那麼檔案會被cache manager程序刪除掉    

     proxy_cache_valid  301  302  10m;

     proxy_cache_valid  any  1m;

     proxy_cache_use_stale  error  http_500  http_502  http_503  http_504     

[root@www nginx]#vi  nginx.conf

proxy_cache_path  /cache/nginx/levels=1:2 keys_zone=mycache:32m;

    upstream upservers {

        server  192.168.88.129  max_fails=2  fail_timeout=1;

        server  192.168.88.130:8080  max_fails=2  fail_timeout=1  backup;

    }

[[email protected]]# vi default.conf

server {

    listen      8080;

    server_name  localhost;

add_headerX-Via  $server_addr;

add_headerX-Cache  $upstream_cache_status;

    #charset koi8-r;

    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {

       root   /usr/share/nginx/html;

       # proxy_pass http://192.168.88.130/;

       index  index.html  index.php  index.htm;

    }

    location /field/ {

     proxy_cache  mycache;

proxy_cache_valid  200  1d;

proxy_cache_valid  301  302  10m;

proxy_cache_valid  any  1m;

proxy_cache_use_stale  error  http_500 http_502  http_503  http_504;

     proxy_pass  http://upservers/;

     proxy_set_header  Host      $host;

     proxy_set_header  X-Real-IP  $remote_addr;

     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

    } 

    location  ~* \.(jpg|png|gif)$ {

    proxy_cache  mycache;

proxy_cache_valid  200  1d;

proxy_cache_valid  301  302  10m;

proxy_cache_valid  any  1m;

proxy_cache_use_stale  error  http_500  http_502  http_503  http_504;

     proxy_pass  http://upservers;

     proxy_set_header  X-Real-IP  $remote_addr;

    }

    error_page   500  502  503  504  /50x.html;

    location = /50x.html {

        root   /usr/share/nginx/html;

    }

}

訪問http://www.field.com:8080/field/,F12開啟控制檯

可以看到

X-Cache:MISS

X-Via:192.168.88.131

#MISS:快取未命中,請求被傳送到後端

此時已經在Nginx代理伺服器形成快取檔案

[root@www ba]#pwd

/cache/nginx/5/ba

[root@www ba]#ll

總用量 4

-rw-------. 1nginx nginx 546 4月  20 16:43141b735b7bb97cf9c8914596bd253ba5

[root@www ba]#cat 141b735b7bb97cf9c8914596bd253ba5

KEY:http://upservers/

HTTP/1.1 200 OK

Date: Fri, 20Apr 2018 08:43:20 GMT

Server:Apache/2.2.15 (CentOS)

Last-Modified:Fri, 20 Apr 2018 07:28:38 GMT

ETag:"dbec8-6e-56a42a2f67580"

Accept-Ranges:bytes

Content-Length:110

Connection:close

Content-Type:text/html; charset=UTF-8

<h1>Welcometo Http on web2.field.com!!</h1>

<p>If yousee this page,test ok. </p>

<p><em>Thankyou!</em></p>

再重新整理一次頁面,可以看到:

X-Cache:HIT

X-Via:192.168.88.131

快取命中,此時請求不會被傳送到後端。