本文旨在測試 nginx對 http服務 https 服務 的代理方式。
部署測試 http服務
準備測試服務程序 gintest 并啟動如下
[root@localhost ~]# sudo nohup ./gintest 9000 & [1] 4229 [root@localhost ~]# nohup: 忽略輸入并把輸出追加到"nohup.out" [root@localhost ~]# [root@localhost ~]# curl http:localhost:9000 curl: (6) Could not resolve host: http:localhost; 未知的錯誤 [root@localhost ~]# curl http://localhost:9000 {"message":"測試程序,服務器ip:192.168.90.9,端口:9080"}[
nginx反向代理 http服務
安裝 nginx 并修改配置文件 ,對 web服務進行代理如下
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://192.168.90.9:9000; root html; index index.html index.htm; } #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; }
啟動 nginx 并訪問,結果如下 代理成功
nginx 通過 https反向代理 http服務
使用 https 服務需要安裝http_ssl_module 否則會報錯 『ssl parameter requires ngx_http_ssl_module』
重新編譯安裝 nginx
nginx源碼根目錄執行如下命令 ,重新編譯 ,輸出目錄保持不變
./configure --with-http_stub_status_module --with-http_ssl_module make && make install 安裝
成功后可以看到我們的 nginx 目錄下已經安裝了新版本的 nginx
配置 nginx
提前準備好一套證書和私鑰, tls.crt tls.key
nginx http 模塊配置中,增加如下配置
server { listen 443 ssl; server_name www.mytest.com; ssl_certificate /root/tls.crt; ssl_certificate_key /root/tls.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { proxy_pass http://192.168.90.9:9000; root html; index index.html index.htm; } }
重新加載 配置
[root@localhost conf]# ../sbin/nginx -s reload
訪問測試
通過https訪問應用成功,如下:
這種情況下需要將 應用的證書配置到nginx,如果 nginx代理了很多應用,每個應用證書不同,那么需要配置很多證書。
nginx提供了 stream 模塊用于 tcp/udp 請求直接轉發后臺服務器處理,不用配置證書。如下
Nginx Stream模塊負載均衡測試
準備測試用 https服務
準備一個 java https 服務
部署 tomcat及 web示例應用
生成tomcat 證書
[root@mysql tomcat]# keytool -genkey -v -alias tomcat -keyalg RSA -keystore /root/tomcat/certs/tomcat.keystore -validity 365 輸入密鑰庫口令: 再次輸入新口令: 您的名字與姓氏是什么? [Unknown]: zhang 您的組織單位名稱是什么? [Unknown]: pcitc 您的組織名稱是什么? [Unknown]: ptitc 您所在的城市或區域名稱是什么? [Unknown]: bj 您所在的省/市/自治區名稱是什么? [Unknown]: bj 該單位的雙字母國家/地區代碼是什么? [Unknown]: bj CN=zhang, OU=pcitc, O=ptitc, L=bj, ST=bj, C=bj是否正確? [否]: y 正在為以下對象生成 2,048 位RSA密鑰對和自簽名證書 (SHA256withRSA) (有效期為 365 天): CN=zhang, OU=pcitc, O=ptitc, L=bj, ST=bj, C=bj 輸入 <tomcat> 的密鑰口令 (如果和密鑰庫口令相同, 按回車): [正在存儲/root/tomcat/certs/tomcat.keystore] Warning: JKS 密鑰庫使用專用格式。建議使用 "keytool -importkeystore -srckeystore /root/tomcat/certs/tomcat.keystore -destkeystore /root/tomcat/certs/tomcat.keystore -deststoretype pkcs12" 遷移到行業標準格式 PKCS12。 [root@mysql tomcat]# ll certs 總用量 4 -rw-r--r-- 1 root root 2201 3月 3 00:14 tomcat.keystore
配置 tomcat SSL 連接
tomcat 配置文件 server.xml中增加如下內容 :
https服務訪問測試
通過 https 訪問web服務成功
https://192.168.90.20:8443/testWeb/index.jsp
nginx stream 安裝配置
nginx 安裝 stream 模塊
重新編譯安裝 nginx
./configure --with-stream make && make install
配置 nginx stream
nginx.conf配置文件中增加 stream配置(和 http 模塊并列)
訪問測試
通過 nginx 443 端口可以在沒有配置證書的情況下可以通過 https訪問后臺的應用。
負載均衡策略
該模塊支持的負載均衡策略如下:
Round Robin 輪詢– 默認情況下,NGINX 使用 Round Robin 算法對流量進行負載平衡,將其按順序定調用上游組中的服務器。因為是默認方法,所以沒有 round?robin 指令 不配置即為 輪詢。
Least Connections 最少連接數:( least_conn) - NGINX 選擇當前活動連接數較少的服務器。
Least Time ( least_time;僅適用于 NGINX Plus ) – 選擇具有最低平均延遲和最少活動連接數的服務器。用于計算最低平均延遲的方法取決于 least_time 指令中包含以下哪些參數:
connect – 連接上游服務器的時間
first_byte – 接收第一個數據字節的時間
last_byte – 從服務器接收完整響應的時間
示例如下:
upstream stream_backend { least_time first_byte; server backend1.example.com:12345; server backend2.example.com:12345; server backend3.example.com:12346; }
Hash ( hash) – NGINX 根據用戶定義的header選擇服務器,例如源 IP 地址 ( $remote_addr),如下:
upstream stream_backend { hash $remote_addr; server backend1.example.com:12345; server backend2.example.com:12345; server backend3.example.com:12346; }
哈希負載平衡方法常用于保持會話。指定一個 consistent 選項 可以使用 ketama 一致性hash算法:
hash $remote_addr consistent;
默認采用輪詢算法
nginx stream 中upstream 配置兩個后臺服務器如下:
upstream testjava{ server 192.168.90.20:8443; server 192.168.90.21:8443; } server { listen 443; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass testjava; }
進行訪問測試 兩個后臺服務器輪流被訪問
一致性 hash 算法測試
nginx.conf stream 模塊 中配置如下
upstream testjava{ hash $remote_addr consistent; server 192.168.90.20:8443; server 192.168.90.21:8443; } server { listen 443; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass testjava; }
多次訪問都訪問到同一個后臺服務器,證明會話保持成功
參考文檔:http://www.wjhsh.net/felixzh-p-8696552.html
附:負載均衡四層和七層的區別
1.區別:四層負載,說的是基于IP+端口的負載均衡;七層負載,說的是基于WEB請求,URL等應用信息的負載均衡。同理,還有基于二層和三成的。二層的就是基于MAC地址,二層負載均衡會通過一個虛擬MAC地址接受請求,然后再分配到真實的MAC地址。三層負載就是通過一個虛擬IP地址,然后再分配到真實的IP。四層就是通過虛機的IP+端口接收請求,然后再分配到真實的服務器;七層就是通過虛機主機名或者URL接收請求,再根據一些規則分配到真實的服務器,常見的應用是nginx。
2.所謂的負載均衡,就是根據請求的信息不同,來決定怎么樣轉發流量。四層負載均衡,就是根據請求的ip+端口,根據設定的規則,將請求轉發到后端對應的IP+端口上。七層負載均衡,則是在四層基礎上,再去考慮應用層的特征。比如說一個WEB服務器的負載均衡,除了根據IP+80端口來判斷之外,還可以根據七層URL,瀏覽器類別,來決定如何去轉發流量。
3.四層交換機主要分析IP層和TCP/UDP層,實現四層流量負載,這種負載不關心七層的應用協議。七層的交換機除了支持四層之外,還要分析應用層,如HTTP協議、URL、cookie等信息。四層常見軟件是haproxy,LVS,七層常見軟件是nginx。
總結
到此這篇關于Nginx七層及四層反向代理配置的文章就介紹到這了,更多相關Nginx反向代理配置內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/zhangxm_qz/article/details/123235100