面試官:資料庫自增 ID 用完了會咋樣?
阿新 • • 發佈:2021-07-13
nginx反向代理
概述
正向代理代理的物件是客戶端,反向代理代理的是服務端,這是兩者之間最大的區別。Nginx即可以實現正向代理,也可以實現反向代理。我們先來通過一個小案例演示下Nginx正向代理的簡單應用。
nginx反向代理的配置語法
Nginx反向代理模組的指令是由ngx_http_proxy_module模組進行解析,該模組在安裝Nginx的時候已經自己加裝到Nginx中了,接下來我們把反向代理中的常用指令一一介紹下:proxy_pass
proxy_set_header
proxy_redirect
proxy_pass
該指令用來設定被代理伺服器地址,可以是主機名稱、IP地址加埠號形式。語法 | proxy_pass URL; |
預設值 | - |
位置 | location |
URL:為要設定的被代理伺服器地址,包含傳輸協議( http , https:// )、主機名稱或IP地址加埠號、URI等要素。
示例:
proxy_pass http://www.baidu.com; location /server{} proxy_pass http://192.168.200.146; http://192.168.200.146/server/index.html proxy_pass http://192.168.200.146/; http://192.168.200.146/index.html
編寫proxy_pass的時候,後面是否需要加“/”?
示例
server { listen 80; server_name localhost; location /{ #proxy_pass http://192.168.200.146; proxy_pass http://192.168.200.146/; } }當客戶端訪問 http://localhost/index.html,效果是一樣的
server{ listen 80; server_name localhost; location /server{ #proxy_pass http://192.168.200.146; proxy_pass http://192.168.200.146/; } } v> 當客戶端訪問 http://localhost/server/index.html這個時候,第一個proxy_pass就變成了http://localhost/server/index.html
第二個proxy_pass就變成了http://localhost/index.html效果就不一樣了
proxy_set_header
語法 | proxy_set_header field value; |
預設值 |
proxy_set_header Host $proxy_host; proxy_set_header Connection close; |
位置 | http、server、location |
server { listen 8080; server_name localhost; default_type text/plain; return 200 $http_username; }代理伺服器: [192.168.38。159]
server { listen 8080; server_name localhost; location /server { proxy_pass http://192.168.38.153:8080/; proxy_set_header username TOM; } }
訪問測
proxy_redirect
該指令是用來重置頭資訊中的"Location"和"Refresh"的值。語法 |
proxy_redirect redirct replacement; proxy_redirect default; proxy_redirect off; |
預設值 |
proxy_redirct default; |
位置 | http、server、location |
示例:
服務端[192.168.38.159]server { listen 8081; server_name localhost; if (!-f $request_filename){ return 302 http://192.168.200.146; } }
代理服務端[192.168.38.153]
server { listen 8081; server_name localhost; location / { proxy_pass http://192.168.200.146:8081/; proxy_redirect http://192.168.200.146 http://192.168.200.133; } }》該指令的幾組選項
proxy_redirect redirect replacement;
redirect:目標,Location的值
replacement:要替換的值
proxy_redirect default;
default; 將location塊的uri變數作為replacement, 將proxy_pass變數作為redirect進行替換proxy_redirect off;
關閉proxy_redirect的功能
nginx反向代理實戰
伺服器1,2,3存在兩種情況第一種情況: 三臺伺服器的內容不一樣。
第二種情況: 三臺伺服器的內容是一樣。
1. 如果伺服器1、伺服器2和伺服器3的內容不一樣,那我們可以根據使用者請求來分發到不同的伺服器。
代理伺服器 server { listen 8082; server_name localhost; location /server1 { proxy_pass http://192.168.200.146:9001/; } location /server2 { proxy_pass http://192.168.200.146:9002/; } location /server3 { proxy_pass http://192.168.200.146:9003/; } }server1
服務端
server { listen 9001; server_name localhost; default_type text/html; return 200 '<h1>192.168.200.146:9001</h1>' } server2
server { listen 9002; server_name localhost; default_type text/html; return 200 '<h1>192.168.200.146:9002</h1>' } server3
server { listen 9003; server_name localhost; default_type text/html; return 200 '<h1>192.168.200.146:9003</h1>' }
nginx的安全控制
關於web伺服器的安全是比較大的一個話題,裡面所涉及的內容很多,Nginx反向代理是如何來提升web伺服器的安全呢?1、安全隔離
通過代理分開了客戶端到應用程式伺服器端的連線,實現了安全措施。在反向代理之前設定防火牆,僅留一個入口供代理伺服器訪問。如何使用SSL對流量進行加密
翻譯成大家能熟悉的說法就是將我們常用的http請求轉變成https請求,那麼這兩個之間的區別簡單的來說兩個都是HTTP協議,只不過https是身披SSL外殼的http. HTTPS是一種通過計算機網路進行安全通訊的傳輸協議。它經由HTTP進行通訊,利用SSL/TLS建立全通訊,加密資料包,確保資料的安全性。 SSL(Secure Sockets Layer)安全套接層 TLS(Transport Layer Security)傳輸層安全上述這兩個是為網路通訊提供安全及資料完整性的一種安全協議,TLS和 SSL在傳輸層和應用層對網路連線進行加密。 總結來說為什麼要使用https:
http協議是明文傳輸資料,存在安全問題,而https是加密傳輸,相當於 http+ssl,並且可以防止流量劫持。Nginx要想使用SSL,需要滿足一個條件即需要新增一個模組--with-http_ssl_module ,而該模組在編譯的過程中又需要OpenSSL的支援
nginx新增SSL的支援
(1)完成 --with-http_ssl_module模組的增量新增》將原有/usr/local/nginx/sbin/nginx進行備份 》拷貝nginx之前的配置資訊 》在nginx的安裝原始碼進行配置指定對應模組 ./configure --with-http_ssl_module 》通過make模板進行編譯 》將objs下面的nginx移動到/usr/local/nginx/sbin下 》在原始碼目錄下執行 make upgrade進行升級,這個可以實現不停機添 加新模組的功能
Nginx的SSL相關指令
因為剛才我們介紹過該模組的指令都是通過ngx_http_ssl_module模組來解析的。 》ssl:該指令用來在指定的伺服器開啟HTTPS,可以使用 listen 443 ssl,後面這種方式更通用些。語法 | ssl on | off |
預設值 | ssl off |
位置 | http、server |
server{ listen 443 ssl; }》ssl_certificate:為當前這個虛擬主機指定一個帶有PEM格式證書的證書。
語法 | ssl_certificate file; |
預設值 | - |
位置 | http、server |
語法 | ssl_certificate_key file; |
預設值 | - |
位置 | http、server |
語法 | ssl_sesion_cache off|none|[builtin[:size]] [shared:name:size]; |
預設值 | ssl_session_cache none; |
位置 | http、server |
builtin:內建OpenSSL快取,僅在一個工作程序中使用。
shared:所有工作程序之間共享快取,快取的相關資訊用name和size來指定
》ssl_session_timeout:開啟SSL會話功能後,設定客戶端能夠反覆使用儲存在快取中的會話引數時間。
語法 | ssl_session_timeout time; |
預設值 | ssl_session_timeout 5m; |
位置 | http、server |
語法 | ssl_ciphers ciphers; |
預設值 | ssl_ciphers HIGH:!aNULL:!MD5; |
位置 | http、server |
》ssl_prefer_server_ciphers:該指令指定是否伺服器密碼優先客戶端密碼
語法 | ssl_perfer_server_ciphers on|off; |
預設值 | ssl_perfer_server_ciphers off; |
位置 | http、server |
生成證書
方式一:使用阿里雲/騰訊雲等第三方服務進行購買。 方式二:使用openssl生成證書 先要確認當前系統是否有安裝opensslopenssl version
安裝下面的命令進行生成
mkdir /root/cert cd /root/cert openssl genrsa -des3 -out server.key 1024 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 server.cert; ssl_certificate_key server.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; } }驗證
反向代理系統調優
反向代理值Buffer和Cache
Buffffer翻譯過來是"緩衝",Cache翻譯過來是"快取"。總結:
相同點:
兩種方式都是用來提供IO吞吐效率,都是用來提升Nginx代理的效能。
不同點:
緩衝主要用來解決不同裝置之間資料傳遞速度不一致導致的效能低的問 題,緩衝中的資料一旦此次操作完成後,就可以刪除。
快取主要是備份,將被代理伺服器的資料快取一份到代理伺服器,這樣的 話,客戶端再次獲取相同資料的時候,就只需要從代理伺服器上獲取,效 率較高,快取中的資料可以重複使用,只有滿足特定條件才會刪除.
(1)Proxy Buffer 相關指令
》proxy_buffering :該指令用來開啟或者關閉代理伺服器的緩衝區;語法 | proxy_buffering on|off; |
預設值 | proxy_buffering on; |
位置 | http、server、location |
語法 | proxy_buffers number size; |
預設值 | proxy_buffers 8 4k | 8k;(與系統平臺有關) |
位置 | http、server、location |
》proxy_buffer_size:該指令用來設定從被代理伺服器獲取的第一部分響應資料的大小。保持與proxy_buffffers中的size一致即可,當然也可以更小。
語法 | proxy_buffer_size size; |
預設值 | proxy_buffer_size 4k | 8k;(與系統平臺有關) |
位置 | http、server、location |
語法 | proxy_buffer_size size; |
預設值 | proxy_buffer_size 4k | 8k;(與系統平臺有關) |
位置 | http、server、location |
》proxy_temp_path:當緩衝區存滿後,仍未被Nginx伺服器完全接受,響應資料就會被臨時存放在磁碟檔案上,該指令設定檔案路徑
語法 | proxy_temp_path path; |
預設值 | proxy_temp_path proxy_temp;(與系統平臺有關) |
位置 | http、server、location |
》proxy_temp_fifile_write_size:該指令用來設定磁碟上緩衝檔案的大小。
語法 | proxy_temp_fifile_write_size size; |
預設值 | proxy_temp_fifile_write_size 8K|16K;(與系統平臺有關) |
位置 | http、server、location |
proxy_buffering on; proxy_buffer_size 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k;根據專案的具體內容進行相應的調節。