正向代理代理的對象是客戶端,反向代理代理的是服務端,這是兩者之間最大的區(qū)別。Nginx即可以實現(xiàn)正向代理,也可以實現(xiàn)反向代理。
正向代理
先通過一個小案例演示下Nginx正向代理的簡單應用,需求如下:
(1)服務端的設置:當客戶端發(fā)送請求之后,需要在指定日志文件里面輸出客戶端的ip地址即可
http {
#指定日志格式
log_format main 'client send request=>clientIp=$remote_addr serverIp=>$host';
server{
listen 80;
server_name localhost;
#指定日志文件位置 和格式
access_log logs/access.log main;
location {
root html;
index index.html index.htm;
}
}
}
(2)使用客戶端訪問服務端,打開日志查看結(jié)果
(3)代理服務器設置:獲取到客戶端要訪問服務端的ip以及端口
server {
listen 82;
# 設置DNS的IP,用來解析proxy_pass中的域名
resolver 8.8.8.8;
location /{
proxy_pass http://$host$request_uri;
}
}
(4)查看代理服務器的IP(192.168.200.146)和Nginx配置監(jiān)聽的端口(82)
(5)在客戶端配置代理服務器
(6)設置完成后,再次通過瀏覽器訪問服務端
通過對比,上下兩次的日志記錄,會發(fā)現(xiàn)雖然我們是客戶端訪問服務端,但是如何使用了代理,那么服務端能看到的只是代理發(fā)送過去的請求,這樣的化,就使用Nginx實現(xiàn)了正向代理的設置。
但是Nginx正向代理,在實際的應用中不是特別多,所以我們簡單了解下,接下來我們繼續(xù)學習Nginx的反向代理,這是Nginx比較重要的一個功能。
反向代理的常用指令
nginx反向代理模塊的指令是由ngx_http_proxy_module
模塊進行解析,該模塊在安裝Nginx的時候已經(jīng)自己加裝到Nginx中了,常用指令如下:
proxy_pass: 設置被代理服務器地址,可以是主機名稱、IP地址加端口號形式
語法 | proxy_pass URL; |
---|---|
默認值 | — |
位置 | location |
URL:是要設置的被代理服務器地址,包含傳輸協(xié)議(http
,https://
)、主機名稱或IP地址加端口號、URI等要素。
舉例:客戶端:192.168.200.1 代理服務器192.168.221.199 服務器 192.168.221.198
代理服務器199的nginx配置
server {
listen 8081;
server_name localhost;
location / {
# 設置訪問服務器的nginx
proxy_pass http://192.168.221.198;
}
}
在192.168.200.1上訪問199的8081端口
在編寫proxy_pass的時候,后面的值要不要加"/"?
接下來通過例子來說明剛才提到的問題:
server {
listen 8081;
server_name localhost;
location / {
#proxy_pass http://192.168.221.198;
proxy_pass http://192.168.221.198/;
#以上兩個訪問效果是一樣的
}
}
server {
listen 8081;
server_name localhost;
location /server {
#proxy_pass http://192.168.221.198;
#會將server拼接到ip后面,訪問的是http://192.168.221.198/server/index.html
proxy_pass http://192.168.221.198/;
#不拼接,訪問的是http://192.168.221.198/index.html
}
}
proxy_set_header: 更改Nginx服務器接收到的客戶端請求的請求頭信息,然后將新的請求頭發(fā)送給代理的服務器
語法 | proxy_set_header field value; |
---|---|
默認值 | proxy_set_header Host $proxy_host; proxy_set_header Connection close; |
位置 | http、server、location |
需要注意的是,如果想要看到結(jié)果,必須在被代理的服務器上來獲取添加的頭信息。
被代理服務器: [192.168.221.198]
server {
listen 8081;
server_name localhost;
location / {
default_type text/plain;
return 200 $http_username;
}
}
代理服務器: [192.168.221.199]
server {
listen 8081;
server_name localhost;
location /server {
proxy_pass http://192.168.221.198:8081/;
proxy_set_header username TOM;
}
}
訪問測試
proxy_redirect: 重置頭信息中的Location和Refresh的值。
語法 | proxy_redirect redirect replacement; proxy_redirect default; proxy_redirect off; |
---|---|
默認值 | proxy_redirect default; |
位置 | http、server、location |
服務端[192.168.221.198]
server {
listen 8081;
server_name localhost;
# 請求資源不存在,臨時重定向到index歡迎頁面
location / {
root html;
index index.html index.htm;
if (!-f $request_filename){
return 302 http://192.168.221.198/;
}
}
}
代理服務端[192.168.221.199]
server {
listen 8081;
server_name localhost;
location / {
# 請求跳轉(zhuǎn)到198
proxy_pass http://192.168.221.198:8081/;
}
}
請求http://192.168.221.199:8081/abc.html時,頁面不存在,返回302跳轉(zhuǎn)到192.168.221.198,看到了真實服務器的地址,在現(xiàn)實開發(fā)中是不安全的
修改代理服務器[192.168.221.199]
server {
listen 8081;
server_name localhost;
location / {
proxy_pass http://192.168.221.198:8081/;
# 將真實ip替換為代理服務器的ip地址
proxy_redirect http://192.168.221.198/ http://192.168.221.199/;
}
}
server {
listen 80;
server_name localhost;
location / {
#將代理服務器的首頁換成真實服務器的
proxy_pass http://192.168.221.198/;
}
}
proxy_redirect redirect replacement;
redirect:目標Location的值
replacement:要替換的值
proxy_redirect default;
default 有了默認選項
將location塊的uri變量作為replacement,
將proxy_pass變量作為redirect進行替換
proxy_redirect off
關閉proxy_redirect的功能
反向代理實戰(zhàn)
服務器1,2,3存在兩種情況
第一種情況:三臺服務器的內(nèi)容不一樣。
第二種情況: 三臺服務器的內(nèi)容是一樣。
- 如果服務器1、服務器2和服務器3的內(nèi)容不一樣,可以根據(jù)用戶請求來分發(fā)到不同的服務器。(根據(jù)端口區(qū)分不同服務器)
代理服務器 [192.168.221.199]
server {
listen 8082;
server_name localhost;
location /server1 {
proxy_pass http://192.168.221.198:9001/;
}
location /server2 {
proxy_pass http://192.168.221.198:9002/;
}
location /server3 {
proxy_pass http://192.168.221.198:9003/;
}
}
服務端[192.168.221.198]
server {
listen 9001;
server_name localhost;
default_type text/html;
return 200 '<h1>192.168.200.198:9001</h1>'
}
server {
listen 9002;
server_name localhost;
default_type text/html;
return 200 '<h1>192.168.200.198:9002</h1>'
}
server {
listen 9003;
server_name localhost;
default_type text/html;
return 200 '<h1>192.168.200.198:9003</h1>'
}
- 如果服務器1、服務器2和服務器3的內(nèi)容是一樣的,該如何處理?參考nginx負載均衡
Nginx的安全控制
關于web服務器的安全是比較大的一個話題,里面所涉及的內(nèi)容很多,Nginx反向代理是如何來提升web服務器的安全呢?
通過代理分開了客戶端到應用程序服務器端的連接,實現(xiàn)了安全措施。在反向代理之前設置防火墻,僅留一個入口供代理服務器訪問。
使用SSL對流量進行加密
就是將常用的http請求轉(zhuǎn)變成https請求,這兩個之間的區(qū)別簡單的來說兩個都是HTTP協(xié)議,只不過https是身披SSL外殼的http。
HTTPS是一種通過計算機網(wǎng)絡進行安全通信的傳輸協(xié)議。它經(jīng)由HTTP進行通信,利用SSL/TLS建立全通信,加密數(shù)據(jù)包,確保數(shù)據(jù)的安全性。
- SSL(Secure Sockets Layer)安全套接層
- TLS(Transport Layer Security)傳輸層安全
上述這兩個是為網(wǎng)絡通信提供安全及數(shù)據(jù)完整性的一種安全協(xié)議,TLS和SSL在傳輸層和應用層對網(wǎng)絡連接進行加密。
為什么要使用https
http協(xié)議是明文傳輸數(shù)據(jù),存在安全問題,而https是加密傳輸,相當于http+ssl,并且可以防止流量劫持。
Nginx要想使用SSL,需要滿足一個條件即需要添加一個模塊--with-http_ssl_module
,默認沒有支持,需要自己添加,該模塊在編譯的過程中又需要OpenSSL的支持,需提前準備【參考nginx源碼安裝】。
nginx添加SSL的支持
完成 --with-http_ssl_module
模塊的增量添加
拷貝nginx之前的配置信息
將原有/usr/local/nginx/sbin/nginx進行備份
在nginx的安裝源碼進行配置指定對應模塊 ./configure --with-http_ssl_module
通過make模板進行編譯
將objs下面的nginx移動到/usr/local/nginx/sbin下
在源碼目錄下執(zhí)行 make upgrade進行升級,這個可以實現(xiàn)不停機添加新模塊的功能
Nginx的SSL相關指令
該模塊的指令都是通過ngx_http_ssl_module模塊來解析的。
ssl: 用來在指定的服務器開啟HTTPS,可以使用 listen 443 ssl,后面這種方式更通用些。
語法 | ssl on | off; |
---|---|
默認值 | ssl off; |
位置 | http、server |
server{
listen 443 ssl; #等值于 ssl on
}
ssl_certificate: 為當前這個虛擬主機指定一個帶有PEM格式證書的證書。
語法 | ssl_certificate file; |
---|---|
默認值 | — |
位置 | http、server |
ssl_certificate_key: 該指令用來指定PEM secret key文件的路徑
語法 | ssl_ceritificate_key file; |
---|---|
默認值 | — |
位置 | http、server |
ssl_session_cache: 該指令用來配置用于SSL會話的緩存
語法 | ssl_sesion_cache off|none|[builtin[:size]] [shared:name:size] |
---|---|
默認值 | ssl_session_cache none; |
位置 | http、server |
off:禁用會話緩存,客戶端不得重復使用會話
none:禁止使用會話緩存,客戶端可以重復使用,但是并沒有在緩存中存儲會話參數(shù)
builtin:內(nèi)置OpenSSL緩存,僅在一個工作進程中使用。
shared:所有工作進程之間共享緩存,緩存的相關信息用name和size來指定
name:緩存名稱
size:緩存大小 提升緩存效率
ssl_session_timeout: 開啟SSL會話功能后,設置客戶端能夠反復使用儲存在緩存中的會話參數(shù)時間。
語法 | ssl_session_timeout time; |
---|---|
默認值 | ssl_session_timeout 5m; |
位置 | http、server |
ssl_ciphers: 指出允許的密碼,密碼指定為OpenSSL支持的格式
語法 | ssl_ciphers ciphers; |
---|---|
默認值 | ssl_ciphers HIGH:!aNULL:!MD5; |
位置 | http、server |
使用openssl ciphers
查看openssl支持的格式
ssl_prefer_server_ciphers: 指定是否服務器密碼優(yōu)先客戶端密碼
語法 | ssl_perfer_server_ciphers on|off; |
---|---|
默認值 | ssl_perfer_server_ciphers off; |
位置 | http、server |
生成證書
需要生成帶有PEM格式的證書和指定PEM secret key的證書
方式一:使用阿里云/騰訊云等第三方服務進行購買。
參考從阿里云上申請ssl證書,配置https
使用域名,去掉url地址欄不安全標示
# 地址欄訪問https://www.nginx521.cn
server {
listen 443 ssl;
server_name www.nginx521.cn;
ssl_certificate /root/nginx_cert/www.nginx521.cn.key;
ssl_certificate_key /root/nginx_cert/www.nginx521.cn.pem;
#會話緩存 共享緩存:緩存名稱:緩存大小
ssl_session_cache shared:SSL:1m;
#超時時間 5min
ssl_session_timeout 5m;
# 允許的密碼格式
ssl_ciphers HIGH:!aNULL:!MD5;
# 是否允許服務器密碼優(yōu)先于客戶端密碼
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
# 不加https直接域名訪問www.nginx521.cn
server {
listen 80;
server_name www.nginx521.cn;
location / {
#root html;
#index index.html index.htm;
rewirte ^(.*) https://www.nginx521.cn$1;
}
方式二:使用openssl生成證書
確認當前系統(tǒng)是否有安裝openssl
openssl version
按照下面的命令進行生成
mkdir /root/cert
cd /root/cert
#生存對應的server.key
openssl genrsa -des3 -out server.key 1024
# 生存csr
openssl req -new -key server.key -out server.csr
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
開啟SSL實例
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /root/cert/server.crt;
ssl_certificate_key /root/cert/server.key;
#會話緩存 共享緩存:緩存名稱:緩存大小
ssl_session_cache shared:SSL:1m;
#超時時間 5min
ssl_session_timeout 5m;
# 允許的密碼格式
ssl_ciphers HIGH:!aNULL:!MD5;
# 是否允許服務器密碼優(yōu)先于客戶端密碼
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
驗證 訪問https://192.168.221.199
反向代理系統(tǒng)調(diào)優(yōu)
反向代理值Buffer和Cache,詳情參考【nginx緩存】
Buffer翻譯過來是"緩沖",Cache翻譯過來是"緩存"。
相同點:
兩種方式都是用來提供IO吞吐效率,都是用來提升Nginx代理的性能。
不同點:
緩沖主要用來解決不同設備之間數(shù)據(jù)傳遞速度不一致導致的性能低的問題,緩沖中的數(shù)據(jù)一旦此次操作完成后,就可以刪除。
緩存主要是備份,將被代理服務器的數(shù)據(jù)緩存一份到代理服務器,這樣的話,客戶端再次獲取相同數(shù)據(jù)的時候,就只需要從代理服務器上獲取,效率較高,緩存中的數(shù)據(jù)可以重復使用,只有滿足特定條件才會刪除.
Proxy Buffer相關指令
proxy_buffering : 用來開啟或者關閉代理服務器的緩沖區(qū)
語法 | proxy_buffering on|off; |
---|---|
默認值 | proxy_buffering on; |
位置 | http、server、location |
proxy_buffers: 用來指定單個連接從代理服務器讀取響應的緩存區(qū)的個數(shù)和大小
語法 | proxy_buffers number size; |
---|---|
默認值 | proxy_buffers 8 4k | 8K;(與系統(tǒng)平臺有關) |
位置 | http、server、location |
number:緩沖區(qū)的個數(shù)
size:每個緩沖區(qū)的大小,緩沖區(qū)的總大小就是number*size
proxy_buffer_size: 用來設置從被代理服務器獲取的第一部分響應數(shù)據(jù)的大小。保持與proxy_buffers中的size一致即可,當然也可以更小。
語法 | proxy_buffer_size size; |
---|---|
默認值 | proxy_buffer_size 4k | 8k;(與系統(tǒng)平臺有關) |
位置 | http、server、location |
proxy_busy_buffers_size: 用來限制同時處于BUSY狀態(tài)的緩沖總大小。
語法 | proxy_busy_buffers_size size; |
---|---|
默認值 | proxy_busy_buffers_size 8k|16K; |
位置 | http、server、location |
proxy_temp_path: 當緩沖區(qū)存滿后,仍未被Nginx服務器完全接受,響應數(shù)據(jù)就會被臨時存放在磁盤文件上,該指令設置文件路徑
語法 | proxy_temp_path path; |
---|---|
默認值 | proxy_temp_path proxy_temp; |
位置 | http、server、location |
注意path最多設置三層。
proxy_temp_file_write_size: 用來設置磁盤上緩沖文件的大小。
語法 | proxy_temp_file_write_size size; |
---|---|
默認值 | proxy_temp_file_write_size 8K|16K; |
位置 | http、server、location |
通用網(wǎng)站的配置文章來源:http://www.zghlxwxcb.cn/news/detail-776749.html
proxy_buffering on;
proxy_buffer_size 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
根據(jù)項目的具體內(nèi)容進行相應的調(diào)節(jié)文章來源地址http://www.zghlxwxcb.cn/news/detail-776749.html
到了這里,關于Nginx(4)nginx的反向代理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!