1. 程式人生 > 其它 >nginx配置7層協議,4層協議

nginx配置7層協議,4層協議

技術標籤:web服務nginx

nginx配置7層協議

舉例講解下什麼是7層協議,什麼是4層協議。

(1)7層協議

OSI(Open System Interconnection)是一個開放性的通行系統互連參考模型,他是一個定義的非常好的協議規範,共包含七層協議。
好,詳情不進行仔細講解,可以自行百度

(2)協議配置

這裡我們舉例,在nginx做負載均衡,負載多個服務,部分服務是需要7層的,部分服務是需要4層的,也就是說7層和4層配置在同一個配置檔案中。

準備三臺機器:

代理服務IP:10.0.105. --配置本地host解析域名;

後端伺服器IP:nginx-a :10.0.105.199/nginx-b:10.0.105.202(yum安裝)後端伺服器將nginx服務啟動

配置代理伺服器的nginx配置檔案

worker_processes  4;

worker_rlimit_nofile 102400;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    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; keepalive_timeout 65; gzip on; upstream testweb { ip_hash; server 10.0.105.199:80 weight=
2 max_fails=2 fail_timeout=2s; server 10.0.105.202:80 weight=2 max_fails=2 fail_timeout=2s; } server { listen 80; server_name www.test.com; charset utf-8; #access_log logs/host.access.log main; location / { proxy_pass http://testweb; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

202伺服器yum安裝的建立新的配置檔案:

[[email protected] ~]# cd /etc/nginx/conf.d/
[[email protected] conf.d]# cp default.conf test.conf
[[email protected] conf.d]# cat test.conf 
server {
    listen       80;
    server_name  localhost;

    location / {
         root   /usr/share/nginx/html;
         index  index.html index.htm;
    }
}
[[email protected] ~]# nginx -s reload

瀏覽器測試訪問:

http://www.test.com/

(2)4層協議

TCP/IP協議
之所以說TCP/IP是一個協議族,是因為TCP/IP協議包括TCP、IP、UDP、ICMP、RIP、TELNETFTP、SMTP、ARP、TFTP等許多協議,這些協議一起稱為TCP/IP協議。

從協議分層模型方面來講,TCP/IP由四個層次組成:網路介面層、網路層、傳輸層、應用層。

在這裡插入圖片描述
nginx在1.9.0 的時候,增加了一個 stream 模組,用來實現OSI第四層協議(網路層和傳輸層)的轉發、代理、負載均衡等。stream模組的用法跟http的用法類似,允許我們配置一組TCP或者UDP等協議的監聽.

配置案例:

#4層tcp負載 
stream {
   upstream myweb {
      hash $remote_addr consistent;
      server 172.17.14.2:8080;
      server 172.17.14.3:8080;
        }
server {
            listen 82;
            proxy_connect_timeout 10s;
            proxy_timeout 30s;
            proxy_pass myweb;
        }
}