Linux nginx服務之反向代理

                   Linux nginx服務之反向代理

Nginx服務之反向代理:

GSLBGlobal Service LB 全局服務負載均衡:

SLBService LB

應用程序發布:

灰度模型:

 

ngx_http_proxy_module模塊:

 1proxy_pass URL;

Context: location, if in location, limit_except

 

注意:proxy_pass后面的路徑不帶uri時,其會將locationuri傳遞給后端主機;

server {

server_name HOSTNAME;

location /uri/ {

proxy_pass http://hos[:port];

}

  }

http://HOSTNAME/uri –> http://host/uri 

blob.png

 proxy_pass后面的路徑是一個uri時,其會將locationuri替換為proxy_passuri;

server {

server_name HOSTNAME;

location /uri/ {

proxy_pass  http://host/new_uri/;

}

}

http://HOSTNAME/uri/ –> http://host/new_uri/

blob.png

如果location定義其uri時使用了正則表達式的模式,則proxy_pass之后必須不能使用uri; 用戶請求時傳遞的uri將直接附加代理到的服務的之后;

    server {

server_name HOSTNAME;

location ~|~* /uri/ {

proxy_pass http://host;

}

  }

http://HOSTNAME/uri/ –> http://host/uri/;

blob.png

2、proxy_set_header field value;

設定發往后端主機的請求報文的請求首部的值;Context: http, server, location

   proxy_set_header X-Real-IP  $remote_addr;

   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 示例:

   讓后臺realserver主機記錄客戶端訪問真正的地址,而不是nginx反向代理服務器的IP

   nginx反向代理服務器上設置:

blob.png

blob.png

 在后臺realserver httpd服務器上設置LogFormat 

blob.png

blob.png

3、proxy_cache_path :只能用于http配置段中;

定義可用于proxy功能的緩存;Context: http

proxy_cache_path path[levels=levels]  keys_zone=name:size  [inactive=time] [max_size=size]  

4、proxy_cache zone | off;

指明要調用的緩存,或關閉緩存機制;Context: http, server, location

 

5 proxy_cache_key string;

緩存中用于“鍵”的內容;

默認值:proxy_cache_key $scheme$proxy_host$request_uri;

 

6proxy_cache_valid [code …] time;

定義對特定響應碼的響應內容的緩存時長;

 示例:

   nginx主配置文件中定義可用于proxy功能的緩存:

blob.png

nginx默認配置文件中指明要調用的緩存或關閉緩存:

blob.png

blob.png

7、proxy_cache_use_stale

  proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off …;

  當后端服務器出現故障時,后端服務器是何種故障才提供緩存中的內容響應給客戶端。

8、proxy_cache_methods GET | HEAD | POST …;

   為那些請求方法去檢查緩存:默認為GET、HEADPOST沒有什么意義

If the client request method is listed in this directive then the response will be cached. GETand HEADmethods are always added to the list, though it is recommended to specify them explicitly. 

9、proxy_hide_header field;

   指明要隱藏的head

By default, nginx does not pass the header fields Date, Server, X-Pad, and X-Accel-…from the response of a proxied server to a client. The proxy_hide_header directive sets additional fields that will not be passed.

10、proxy_connect_timeout time;

    設置代理連接時長,默認為60s,不能超過75s。

Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds.

 

ngx_http_headers_module模塊

The ngx_http_headers_module module allows adding the Expiresand Cache-Controlheader fields, and arbitrary fields, to a response header.

向由代理服務器響應給客戶端的響應報文添加自定義首部,或修改指定首部的值;

1、add_header name value [always];

   向響應報文中添加首部信息;

   添加自定義首部;

add_header X-Via  $server_addr;  顯示代理服務器的IP地址;

add_header X-Accel $server_name; 顯示代理服務器的主機名;

blob.png

blob.png

指明此次訪問的網頁是由哪個ip地址的服務器代理的。

blob.png

blob.png

2expires [modified] time;

expires epoch | max | off;

用于定義ExpireCache-Control首部的值;

ngx_http_upstream_module模塊

   定義realserver服務組

   The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass directives.

1upstream name { … }

 定義后端服務器組,會引入一個新的上下文;Context: http

upstream httpdsrvs {

server …

server…

     }

 

2server address [parameters];

 upstream上下文中server成員,以及相關的參數;Context: upstream

      address的表示格式:

unix:/PATH/TO/SOME_SOCK_FILE

IP[:PORT]

HOSTNAME[:PORT]

blob.png

blob.png

nginx的默認配置文件中定義反向代理的后端服務器組的組名websrvs

blob.png

blob.png

 parameters(參數):

weight=number

權重,默認為1;

max_fails=number

失敗嘗試最大次數;超出此處指定的次數時,server將被標記為不可用;

            可以對后臺服務器的健康狀態做檢測

fail_timeout=time

設置將服務器標記為不可用狀態的超時時長;

max_conns

當前的服務器的最大并發連接數;

backup

將服務器標記為“備用”,即所有服務器均不可用時此服務器才啟用;

down

標記為“不可用”;

blob.png

3、least_conn;

最少連接調度算法,當server擁有不同的權重時其為wlc;

4、 ip_hash;

源地址hash調度方法;

 

5、consistent_hash;

   一致性hash調度;

 我們舉個例子來說明一致性哈希的好處。

 假設后端集群包含三臺緩存服務器,A、BC。

 請求r1、r2落在A上。

 請求r3、r4落在B上。

 請求r5、r6落在C上。

 使用一致性哈希時,當緩存服務器B宕機時,r1/r2會仍然落在A上,r5/r6會仍然落在C 上,

 也就是說這兩臺服務器上的緩存都不會失效。r3/r4會被重新分配給A或者C,并產生回源。

 使用其它算法,當緩存服務器B宕機時,r1/r2不再落在A上,r5/r6不再落在C上了。

 也就是說A、B、C上的緩存都失效了,所有的請求都要回源。

blob.png

blob.png

5、hash key [consistent];

基于指定的keyhash表來實現對請求的調度,此處的key可以直接文本、變量或二者的組合;

作用:將請求分類,同一類請求將發往同一個real server

If the consistent parameter is specified the ketama consistent hashing method will be used instead.

示例:

hash $request_uri consistent; 將來自同一uri的請求始終發往同一個real server

hash $remote_addr;將來自同一個客戶端的IP地址請求始終發往同一個real server

blob.png

6、keepalive connections;

   定義nginx代理服務器和后端服務器的保持連接。

為每個worker進程保留的空閑的長連接數量;

 

nginx的其它的二次發行版:

  tengine

  OpenResty

ngx_stream_core_module模塊 放置在main配置段中 core:核心 stream:流動

   模擬反代基于tcpudp的服務連接,即工作于傳輸層的反代或調度器;

   http同等級的配置段,stream要放置在main配置段中。

 

1、stream { … }

定義stream相關的服務;Context:main

stream {

upstream sshsrvs {

server 192.168.22.2:22;

server 192.168.22.3:22;

least_conn;

     }

 

  server {

  listen 10.1.0.6:22022;

  proxy_pass sshsrvs;

}

}

blob.png

blob.png

注意:自定義stream基于tcpudp反向代理時,需要把nginx的主配置文件中的http配置段注釋掉。

blob.png

blob.png

blob.png

2、listen

listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];

 

memcached緩存服務器:

   緩存服務器:

 緩存:cache,無持久存儲功能;

 bypass緩存

 k/v cache,僅支持存儲流式化數據;

LiveJournal旗下的Danga Interactive研發,

    特性:

k/v cache:僅可序列化數據;存儲項:k/v;

        k:存放于內存的hash表中;(k是哈希的文件名)

        v:存放于內存中的數據;(v是數據)

智能性一半依賴于客戶端(調用memcachedAPI開發程序),一半依賴于服務端

分布式緩存:互不通信的分布式集群;

分布式系統請求路由方法:取模法,一致性哈希算法;

算法復雜度:O(1)

           O1)就是說n很大的時候,復雜度基本就不增長了,基本就是個常量c

清理過期緩存項:

緩存耗盡:LRU

緩存項過期:惰性清理機制

 

安裝配置:

CentOS 7 base倉庫直接提供:

監聽的端口:11211/tcp, 11211/udp ,以tcp使用的最多。

    主程序:/usr/bin/memcached

配置文件:/etc/sysconfig/memcached  memcached的環境配置文件。

Unit Filememcached.service

         啟動memcached

協議格式:memcached協議

文本格式

二進制格式:編碼效率更高;

    

命令:

統計類:stats, stats items, stats slabs, stats sizes

存儲類:set, add, replace, append, prepend

            set:表示key有值就修改,沒有值就新創建。

            add:表示新創建key值。

            replace

            append:在已有key值之后附加新值。

            prepend:在已有key值的前面追加新值。

命令格式:<command name> <key> <flags> <exptime> <bytes>  

<cas unique>

檢索類:get, delete, incr/decr

            get:獲取key值數據。

            delete:刪除數據。

            incr:把已有的值加1。

            decr:把已有的值減1

blob.png

清空:flush_all:清空整個緩存。

blob.png

示例:

telnet> add KEY <flags> <expiretime> <bytes> \r

telnet> VALUE

blob.png

blob.png

blob.png

blob.png

blob.png

memcached程序的常用選項:

-m <num>Use <num> MB memory max to use for object storage; the default is 64 megabytes.

-c <num>Use <num> max simultaneous connections; the default is 1024.

-u <username>:以指定的用戶身份來運行進程;

-l <ip_addr>:監聽的IP地址,默認為本機所有地址;

-p <num>:監聽的TCP端口, the default is port 11211.

-U <num>Listen on UDP port <num>, the default is port 11211, 0 is off.

-M:內存耗盡時,不執行LRU清理緩存,而是拒絕存入新的緩存項,直到有多余的空間可用時為止;

-f <factor>:增長因子;默認是1.25;

-t <threads>:啟動的用于響應用戶請求的線程數;

 

查看memcached的增長因子過程:

blob.png

memcached默認沒有認證機制,可借用于SASL進行認證;

SASLSimple Authentication Secure Layer

API:

php-pecl-memcache 可能需要依賴epel yum源進行安裝。

php-pecl-memcached 可能需要依賴epel yum源進行安裝。

python-memcached

libmemcached

libmemcached-devel

命令行工具:

   memcached-tool  SERVER:PORT  COMMAND

blob.png

實驗測試:

  使用nginx反向代理,后端httpd realserver組:

  實驗環境:

  測試客戶機:

  client客戶機:IP 192.168.3.7;

  nginx反向代理服務器:

     配置兩塊網卡,網卡1ip192.168.3.5 網卡2192.168.22.1

  后臺realserver配置:

  realserver 1httpd服務器:serverIP192.168.22.2  gateway192.168.22.1

     realserver1)中配置http服務器 upstream server 1

  realserver 2httpd服務器:serverIP192.168.22.3  gateway192.168.22.1

     realserver2)中配置http服務器 upstream server 2

 

1)realserver1)上搭建http服務:

blob.png

blob.png

2)realserver2)上搭建http服務和nginx服務器,nginx服務器監聽8080端口:

blob.png

blob.png

  realserver2)上 配置nginx服務器監聽8080端口:

配置默認主頁為:

blob.png

blob.png

blob.png

blob.png

3)配置nginx反向代理服務器:

nginx的主配置文件中的http配置段中配置upstream 后端服務器組:

blob.png

 nginx的默認配置文件中定義反向代理的后端服務器組的組名websrvs

blob.png

blob.png

nginx配合完成之后使用nginx -t 檢查一下語法是否正確;

如果語法沒有錯誤就可以使用 nginx -s reload systemctl  restart nginx.service 重載或重啟服務。

4)client 192.168.3.7 客戶端 主機上測試請求后端realserver httpd服務器組:

blob.png

5)關閉 realserver1)和realserver2)上的httpd服務進行測試backup主機服務器:

 或是在/etc/nginx/nginx.conf的主配置文件中在upstream配置段中將realserver12標記為down。

blob.png

blob.png

原創文章,作者:zhengyibo,如若轉載,請注明出處:http://www.www58058.com/59610

(0)
zhengyibozhengyibo
上一篇 2016-11-18 18:29
下一篇 2016-11-18 19:01

相關推薦

  • Http協議中的各種長度限制總結

    HTTP1.0的格式 request(HTTP請求消息)結構:一個請求行.部分消息頭,以及實體內容,其中的一些消息內容都是可選擇的.消息頭和實體內容之間要用空行分開. GET /index.html HTTP/1.1 //請求頭,下面都是消息頭.Accept: */*Accept-Languang:en-usConnection:keep-aliveHost…

    Linux干貨 2015-04-04
  • Nginx工作原理和優化、漏洞。

    1.  Nginx的模塊與工作原理 Nginx由內核和模塊組成,其中,內核的設計非常微小和簡潔,完成的工作也非常簡單,僅僅通過查找配置文件將客戶端請求映射到一個location block(location是Nginx配置中的一個指令,用于URL匹配),而在這個location中所配置的每個指令將會啟動不同的模塊去完成相應的工作。 Nginx的模塊…

    Linux干貨 2015-04-04
  • 運維挖坑埋坑之旅—-413 Request Entity Too Large

    原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://nolinux.blog.51cto.com/4824967/1575160        好久沒寫博文了,天天忙的喘不過來氣。最近是該總結總結前段時間的工作了,順便緩解下這兩天的霉…

    Linux干貨 2016-08-15
  • SQL優化大全

    1. 優化SQL步驟 1. 通過 show status和應用特點了解各種 SQL的執行頻率        通過 SHOW STATUS 可以提供服務器狀態信息,也可以使用 mysqladmin extende d-status 命令獲得。 SHOW STATUS 可以根據需要顯示 session 級別的統計結果和 g…

    Linux干貨 2015-04-13
  • 第四周

    第四周 1.復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其他用戶均沒有任何訪問權限。 [root@node1 ~]# cp -r /etc/skel/ /home/tuser1 [root@node1 ~]# chmod -R go= /home/tuser1 [root@node1 ~]# ls -…

    Linux干貨 2017-07-26
  • shell 腳本 之循環 for while until 和 軟件包的管理 【上】

    shell 腳本 之循環 for while until 和 軟件包的管理 【上】 循環執行     將某代碼段重復運行多次     重復運行多少次:             循環次數事先已知    &nbsp…

    系統運維 2016-08-18

評論列表(1條)

  • Cornelia
    Cornelia 2017-04-24 06:10

    Sehr geehrte Damen und Herren,bitte tragen Sie mich in den Hydrogeit-Newsletter ein. Leider f??hrt die Eigeninitiative unter Verwendung des vorgesehenen Formulars zu einem Dat.rbankfehleneBesten Dank!Mit freundlichen Gr????enHolger Krings

欧美性久久久久