Linux nginx服務之反向代理

                   Linux nginx服務之反向代理

Nginx服務之反向代理:

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

SLBService LB

應用程序發布:

灰度模型:

 

ngx_http_proxy_module模塊:

 1、proxy_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;

 

6、proxy_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.

1、upstream name { … }

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

upstream httpdsrvs {

server …

server…

     }

 

2、server 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

3least_conn;

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

4、 ip_hash;

源地址hash調度方法;

 

5、consistent_hash;

   一致性hash調度;

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

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

 請求r1、r2落在A上。

 請求r3、r4落在B上。

 請求r5r6落在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配置段中將realserver1、2標記為down

blob.png

blob.png

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

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

相關推薦

  • C語言的謎題

    這幾天,本站推出了幾篇關于C語言的很多文章如下所示: 語言的歧義 [酷殼鏈接] [CSDN鏈接] 誰說C語言很簡單? [酷殼鏈接] [CSDN鏈接] 6個變態的C語言Hello World程序 [酷殼鏈接] [CSDN鏈接] 如何加密/弄亂C源代碼 [酷殼鏈接] [CSDN鏈接] C語言的謎題 [酷殼鏈接] …

    Linux干貨 2016-05-08
  • Linux文件查找之find命令

    前言:        Linux文件種類繁多,難免在維護Linux中會忘記文件的路徑。所有在維護Linux中學好查找命令是非常必要的。     Linux查找命令有很多,比如 whereis,type,which,locate,find(重點)。  &nbsp…

    Linux干貨 2015-06-15
  • 用戶與組管理

    如何成為linux用戶?     在linux系統中,要想從系統中獲取資源、對系統進行管理和控制,就需要向系統申請一個賬號,從而實現特定用途,當申請到賬號后,要對它設置口令,這樣用戶賬號就申請成功了,然后可以登陸系統,當用戶登陸系統時,需要通過系統的認證,認證成功后才給予授權,在用戶的使用過程中還會對用戶進行審計。 為什么要設置…

    Linux干貨 2016-08-08
  • Windows 10 VS. Linux

    原文出處: Linux中國 – zpl1025   前陣子 Windows 10 好像占據了絕大部分頭條,甚至在一些Linux圈里也是一樣。最具代表性的是 betanews.com 的 Brian Fagioli 說 Windows 10 已經為 Linux 桌面系統敲響了喪鐘,Microsoft 如今宣布將為忠實的…

    系統運維 2015-03-24
  • 專屬個人的聊天機器人的實現——圖靈機器人

    先體驗一下:圖靈機器人智能聊天體驗:http://www.tuling123.com/openapi/cloud/proexp.jsp 1、簡介        本篇為大家介紹使用圖靈機器人api實現聊天機器人的方法,該api本身已經擁有了智能聊天的功能,但我們可以自定義知識庫的方式簡單實現專屬個人的聊天機器人。 2、開…

    Linux干貨 2016-03-28
  • Linux之查找命令find簡介

    查找命令find簡介    Linux使用過程中難免會遇到查找功能,類似于Windows中的搜索功能,如果想要找某個文件在什么地方,什么格式的文件等等。在Linux中我們可以使用find、locate、which、whereis等命令。接下來我們就簡單說一下如何使用這幾個命令查找文件。 一、which which命令的作用是,在PATH變…

    Linux干貨 2015-05-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

欧美性久久久久