Nginx反向代理知識(shí)要點(diǎn)

(官網(wǎng)示例)
ngx_http_proxy_module? ?模塊
server? {
? ? ? ?listen
? ? ? server_name
? ?location? ?/? ?{
? ? ? ? ? ? proxy_pass? ?http://IP:80/;
? ? ? ? ?}
}
(代理轉(zhuǎn)發(fā)至后端IP機(jī)的80端口)
1.? 格式:
? ? ? ? ? location? ? /url? ?{
? ? ? ? ? ? ? ? ? ? ?rewrite
? ? ? ? ? ? ? ? ? ? ?proxy_pass? ?http://back_server:port/newurl;
? ? ? ? ?}
? ? ? ? ?/url—> /newurl
? ? ? ? location這種后端轉(zhuǎn)發(fā)非常的靈活
2.? 正則示例:
? ? ? ? ? location? ?~*\.(jpg|png|gif)? ?{
? ? ? ? ? ? ? ? ? ? proxy_pass? ? http://IP;
? ? ? ? ?}
? ? ? ? ?這里有這注意的要點(diǎn),對(duì)于正則匹配的,不能進(jìn)行后端url的替換,即匹配的都會(huì)加在proxy_pass后面的url后面。正則匹配情況下,proxy_pass后面的url不加任何自定義的url,否則語(yǔ)法錯(cuò)誤。
? ? ? ? ?例如? ?http://www.testnginx.com/1.jpg—(轉(zhuǎn)發(fā)格式)—>? ?http://IP/1.jpg
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? client<——?
? ? ? ? ? ? ? ? ? ?http://www.testnginx.com/images/1.jpg—(轉(zhuǎn)發(fā)格式)—>http://IP/images/1.jpg
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? client<——
(將所有.jpg|png|gif圖片格式,都轉(zhuǎn)發(fā)給后端IP機(jī))
代理向后端發(fā)送需要的值(日志中)
proxy_set_header? ?定義TCP首部
常用配置參數(shù)如下:
server? ? {
? ? ? ? ? ? ? listen80;
? ? ? ? ? ? ? server_name? ??www.testnginx.com;
? ? ? ? ? ? ? access_log? ? /home/work/log/nginx/www.testnginx.com.logmilog
? ? ? ? ? ? ? location? ?/? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? proxy_set_header? ?Host? ??www.testnginx.com;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? proxy_set_header? ? ?X-Forwarded-For$proxy_add_x_forwarded_for;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? proxy_pass? ? http://www.testnginx.com.real_backend;
? ? ? ? ? ? ? ? ? ? }
? ? }
其實(shí)$remoad_addr是沒(méi)有proxy_add_x_forwarded_for準(zhǔn)確的,proxy_add_x_forwarded_for記錄的是準(zhǔn)確的client的IP,而$remoad_addr記錄的準(zhǔn)確是代理機(jī)的ip,并且多次代理他們直接也有差異。
官網(wǎng)給出的解釋是:

英文版:

proxy_ssl_ciphers? ? ?等用來(lái)支持代理機(jī)到物理機(jī)之間的加密數(shù)據(jù)的傳輸,即內(nèi)網(wǎng)環(huán)境下密碼等信息的安全傳輸。
代理緩存(定義如何管理緩存空間緩存條目)
proxy_cache? ??zone | off
(調(diào)用緩存)
緩存實(shí)則即為鍵值對(duì)。
proxy_cache_path? ??path
(定義緩存的路徑,只能用在http段)
? ? ? ?官網(wǎng)實(shí)例:

? ? ? ? ? ? ? ? ? ? levels‘:’表示幾個(gè)子目錄,如圖所示,表示兩個(gè)
? ? ? ? ? ? ? ? ? ? levels的數(shù)字1,2表示子目錄的目錄名字符數(shù),一級(jí)子目錄名字字符數(shù)為1個(gè),二級(jí)子目錄名字字符數(shù)為2。
? ? ? ? ? ? ? ? ? ? keys_zone:表示鍵存儲(chǔ)的位置,大小為10m與proxy_cache的zone進(jìn)行匹配,被prixy_cache調(diào)用
? ? ? ? ? ? ? 上圖的實(shí)用實(shí)例:

proxy_cache_methods? ??GET | HEAD;
(僅在客戶(hù)端使用某種方法時(shí)緩存,默認(rèn)GET,HEAD,其實(shí)還有POST等,但是沒(méi)比較緩存提交的信息,大部分情況,不用定義)
proxy_cache_min_uses? ??number;
(緩存生效的最小次數(shù),默認(rèn)為1)
proxy_cache_purge? ??string;
(管理緩存空間中的緩存條目,如果某個(gè)緩存沒(méi)到期,也沒(méi)滿(mǎn),但是后端真實(shí)服務(wù)器數(shù)據(jù)更改了,那么可以在對(duì)應(yīng)緩存條目上增加該項(xiàng),等用戶(hù)再次拿到緩存的時(shí)候會(huì)刪掉該緩存。注意權(quán)限管理,具體配置方式請(qǐng)?jiān)斠?jiàn)官網(wǎng))
proxy_cache_revalidate? ??on | off;
(當(dāng)緩存到期了,詢(xún)問(wèn)后端服務(wù)器緩存數(shù)據(jù)是否改變,若沒(méi)改變則修改緩存時(shí)間,繼續(xù)使用原緩存,即過(guò)期后重新校驗(yàn))
proxy_cache_use_stale? ??error | timeout |invalid_header | updating | http_500 | http_502 …| off...
(緩存過(guò)期時(shí)并且后端服務(wù)器處于類(lèi)似宕機(jī)或服務(wù)更新等以上后面參數(shù)的各種情況下,是否可以繼續(xù)給客戶(hù)端響應(yīng)緩存內(nèi)容,詳見(jiàn)官網(wǎng))
proxy_cache_valid? ??[code …] time;
(按時(shí)間定義不同響應(yīng)碼的緩存時(shí)間)
proxy_cache_pypass? ??string;
? ? ? ? ? ? ? ? ? ? ? ?$cookie_nocache$arg_nocache $http_authorization
(設(shè)置在指定情況下nginx不會(huì)在cache中取數(shù)據(jù))
proxy_connect_timeout;
(后端服務(wù)器連接的超時(shí)時(shí)間_發(fā)起握手等候響應(yīng)超時(shí)時(shí)間)
proxy_hide_header;
(隱藏由Proxy響應(yīng)客戶(hù)端時(shí)響應(yīng)的首部)
但是在生產(chǎn)環(huán)境好像是沒(méi)有啟用關(guān)于nginx cache的任何參數(shù)
ngx_http_upstream_module? ??模塊
模塊的主要功能:定義可由proxy_pass,fastcgi_pass,uwsgi_pass,scgi_pass和memcached_pass指令引用的服務(wù)器組。
示例:
? ? ? ? ?upstream? ?testnginx_backend? ?{
? ? ? ? ? ? ? ? ? ? ? ? ?server? ?IP1/Domain:Portweight=[number]? ?max_fails=[number]? ?fails_timeout=30s;
? ? ? ? ? ? ? ? ? ? ? ? server? ?IP2/Domain:Port weight=[number]? max_fails=[number]? ?fails_timeout=30s;
? ? ? ? ?}
? ?(生產(chǎn)環(huán)境,一般server后面跟的是自己的內(nèi)網(wǎng)ip)
? ? ? ? ?server? ? {
? ? ? ? ? ? ? ? ? listen? ? ? ? 80;
? ? ? ? ? ? ? ? ? server_name? ? ? ?www.testnginx.com;
? ? ? ? ? ? ? ? ? ? ? …
? ? ? ? ?location? ?/? ?{
? ? ? ? ? ? ? ? ? proxy_pass? ??http://testnginx_backend;
? ? ? ? ? ? ? ? ? …
? ? ? ? ? ? ? ? ? }
? ? ?}
(調(diào)用方式,‘…’省略其它配置)
weight:表示權(quán)重,默認(rèn)為1
max_fails:用作健康檢查,表示fails_timeout(超時(shí)時(shí)間規(guī)定)情況出現(xiàn)幾次,則將server從upstream隊(duì)列中剔除。
fails_timeout:設(shè)定超時(shí)時(shí)間的長(zhǎng)度,即規(guī)定超過(guò)多長(zhǎng)時(shí)間未響應(yīng)則判斷為請(qǐng)求失敗。
down:可以手動(dòng)調(diào)為永久不可用,(類(lèi)似情景用于灰度測(cè)試),通常和ip_hash一起使用
backup:可以標(biāo)記為備用機(jī)使用,即當(dāng)所有主服務(wù)器都不用時(shí),啟用這臺(tái)服務(wù)器。
(等更多信息詳見(jiàn)官網(wǎng))
情景:由于用戶(hù)在進(jìn)行網(wǎng)站數(shù)據(jù)訪(fǎng)問(wèn)時(shí)的請(qǐng)求與數(shù)據(jù)返回一致性問(wèn)題上,傳統(tǒng)的ip_hash方式會(huì)造成某一臺(tái)機(jī)器連接數(shù)巨大,并不利于upstream調(diào)度。所有引進(jìn)cookie這中方式,根據(jù)tcp報(bào)文序列進(jìn)行后端調(diào)度,這樣基于cookie保證數(shù)據(jù)一致性,由于ip_hash這種基于單一機(jī)器的方式。
sticky? ??cook name[expires=time] [domain=domain ] [httponly][secure] [path=path];
sticky? ??route $variable…
sticky? ??learn create=$variable lookup=$variable zone=name:size[timeout=time] [header];
(啟用會(huì)話(huà)關(guān)聯(lián),致使來(lái)自同一客戶(hù)端的請(qǐng)求被傳遞到一組服務(wù)器中的同一服務(wù)器)
? ? ? ? ? ? ? ? ? ?cookie

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (官網(wǎng)示例,詳見(jiàn)官網(wǎng))
? ? ? ? ? ? ? ? ? ? route

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(官網(wǎng)示例,詳見(jiàn)官網(wǎng))
? ? ? ? ? ? ? ? ? learn

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (官網(wǎng)示例,詳見(jiàn)官網(wǎng))
least_conn
(調(diào)度方法,最少鏈接)
keepalive? ??connections;
(后端專(zhuān)用存儲(chǔ)情況下,例如sql查詢(xún)等,可以使用該方法保持與后端的連接,減少連接次數(shù),提升效率。http模式的話(huà),一般不建議使用)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(官網(wǎng)示例,詳見(jiàn)官網(wǎng))
upstream? ? 還有幾個(gè)內(nèi)建模塊,詳見(jiàn)官網(wǎng)說(shuō)明
ngx_http_headers_module? ??模塊
(主要與自定義響應(yīng)報(bào)文首部有關(guān))
add_header? ??name value[always];
(自定義響應(yīng)報(bào)文首部)
? ? ? 例子:add_headerX-via$server_addr
? ? (在瀏覽器中發(fā)起請(qǐng)求后返回的報(bào)文首部信息中,加上server的ip地址)
? ? ?例子:add_headerX-Cache? $upstream_cache_status;
? ? (查看保持訪(fǎng)問(wèn)響應(yīng)緩存的狀態(tài),狀態(tài)可以是“MISS(未命中)”,“BYPASS”,“EXPIRED”,“HIT(命中)”,“STALE(過(guò)期)”等,查看緩存狀態(tài)需要將cache功能啟用,不然沒(méi)有這個(gè)頭部信息)
還有各種自定義的首部可自行官網(wǎng)學(xué)習(xí)
關(guān)于fastcgi使用來(lái)代理php頁(yè)面的,暫時(shí)沒(méi)有接觸,請(qǐng)自行學(xué)習(xí)
一個(gè)nginx在生產(chǎn)環(huán)境中的完整案例:
user? ? nobody? ? nobody
worker_processes? ? 4;#進(jìn)程數(shù)
worker_rlimit_nofile? ? ? ?51200;#所能夠同時(shí)打開(kāi)的文件數(shù)
error_log? ? ? ?logs/error.lognotice;
pid? ? ? ? ? /var/run/nginx.pid
events? {
? ? ?use? ? epoll;
? ? ?worker_connections? ? ? ? ? 51200;#每一個(gè)進(jìn)程承載的最大連接數(shù)
}
http {
? ? ?server_tokens? ? ?off;#禁止版本號(hào)顯示
? ? ?include? ? ?mime.type;
? ? ?proxy_redirect? ? off;
? ? ?proxy_set_header? ? ?Host? ? ?$host;
? ? ?proxy_set_header? ? ?X-Real-IP? ? ? $remote_addr
? ? ?proxy_set_header? ? ?X-Forwarded-For? ? ? $proxy_add_x_forwarded_for;
? ? ?client_max_body_size? ? ?20m;#每個(gè)用戶(hù)報(bào)文body大小
? ? ?client_boby_buffer_size? ? ?256k;#每個(gè)用戶(hù)Nignx處理時(shí)在nginx中開(kāi)辟的內(nèi)存大小
? ? ?proxy_connect_timeout? ? ? 90;#連接建立的超時(shí)時(shí)間
? ? ?proxy_send_timeout? ? ?90;#向后端服務(wù)器發(fā)送報(bào)文的超時(shí)時(shí)間
? ? ?proxy_read_timeout? ? ?90;#響應(yīng)報(bào)文的超時(shí)時(shí)間
? ? ?proxy_buffer_size? ? ?128k;
? ? ?proxy_buffers? ? ? 464k;
? ? ?proxy_busy_buffers_size? ? ? 128k;
? ? ?proxy_temp_file_write_size? ? ?128k;
? ? ?default_type? ? ?application/octet-stream;
? ? ?charset? ? ?utf-8;
? ? ?client_body_temp_path? ? ?/var/tmp/client_body_temp1 2;? ? ? ? ? ? ? ? ?#這幾個(gè)都是臨時(shí)存儲(chǔ)
? ? ?proxy_temp_path? ? ?/var/tmp/proxy_temp1 2;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#或代理文件的路徑
? ? ?fastcgi_temp_path? ? ?/var/tmp/fastcgi_temp 1 2;
? ? ?uwsgi_temp_path? ? ?/var/tmp/uwsgi_temp 1 2;
? ? ?scgi_temp_path? ? ?/var/tmp/scgi_temp 1 2;
? ? ?ignore_invalid_headers? ? ?on;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#忽略無(wú)法理解的報(bào)文信息
? ? ?server_names_hash_max_size? ? ? ? ? ?256;? ? ? ? ? ? ? ? ? ? ?#server_name進(jìn)行hash
? ? ?server_names_hash_bucket_size? ? ? ? ?64;
? ? ?client_header_buffer_size? ? ? ? 8k;
? ? ?large_client_header_buffers? ? ? ?432k;
? ? ?connection_pool_size? ? ? ?256;
? ? ?request_pool_size? ? ? ?64k;
? ? ?output_buffers? ? ? ?2128k;
? ? ?postpone_output? ? ? 1460;
? ? ?client_header_timeout? ? ? 1m;
? ? ?client_body_timeout? ? ? 3m;
? ? ?send_timeout? ? ? 3m;
? ? ?log_format_main? ? ? ? ‘$remote_addr- $remote_user [$time_local] “$host|$request”’
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?‘$status $body_bytes_sent“$http_referer”’
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ‘”http_user_agent” “$http_x_forwarded_for”“$request_time” “$upstream_response_time”’;
? ? ?open_log_file_cache? ? ? max=1000? ? ? ?inactive=20s? ? ? min_uses=1? ? ? valid=1m;
? ? ?access_log? ? ? ? ? ? logs/access.logmain;
? ? ?log_not_found? ? ? ? ? ?on;
? ? ?sendfile? ? ? ? ? ? ? ? ? ? on;
? ? ?tcp_nodelay? ? ? ? ? ? on;
? ? ?tcp_nopush? ? ? ? ? ? ? off;
? ? ?reset_timeout_connection? ? ? ? ? ? ? on;
? ? ?keepalive_timeout? ? ? ? ? ? ? 105;
? ? ?keepalive_requests? ? ? ? ? ? ?100;
? ? ?gzip? ? ? ? ? ? ? on;
? ? ?gzip_http_version? ? ? ? ? ? 1.1;
? ? ?gzip_vary? ? ? ? ? ? ? ? ?on;
? ? ?gzip_proxied? ? ? ? ? ? ?any;
? ? ?gzip_min_length? ? ? ? ? ? ?1024;
? ? ?gzip_comp_level? ? ? ? ? ? ?6;
? ? ?gzip_buffers? ? ? ? ? ? ? ? 168k;
? ? ?gzip_proxied? ? ? ? ? ? ? ? ? ? ? ? ?expired? no-cache? no-store? private? auth no_last_modified? no_eetag;
? ? ?gzip_types? ? ? ? ? ? ? ? ? ? ? ? text/plain? application/x-javascipt? text/cssapplication/xml? application/json;
? ? ?gzip_disable? ? ? ? ? ? ? ? ? ?“MSIE [1-6]\.(?l.*SV1)”;
? ? ?#其實(shí)從這一下的文件都可以使用include path;path是指定的所以server的文件目錄,因?yàn)橐话愎ぷ鞫夹枰蟹浅6嗟挠蛎M(jìn)行維護(hù)
? ? ?upstream? ? ?tomcat8080;? ?{
? ? ? ? ? ? ? ip_hash
? ? ? ? ? ? ? server? ? ? ? 172.18.100.101:8080? ? ? weight=1? ? ?max_fails=2;
? ? ? ? ? ? ? server? ? ? ? 172.18.100.102:8080? ? ? weight=1? ? ?max_fails=2;
? ? ? ? ? ? ? server? ? ? ? 172.18.100.103:8080? ? ? weight=1? ? ?max_fails=2;
}
server? ? ?{
? ? ? ? listen? ? ? 80;
? ? ? ? server_name? ? ??www.testnginx.com
? ? ? ? root? ? ? ? ? /data/webapp/htdocs;
? ? ? ? access_log? ? ? ? ? ? /var/logs/webapp.access.logmain;
? ? ? ? error_log? ? ? ? ? ? ? ?/var/logs/webapp.error.lognotice;
? ? ? ? location? ? ?/? ? ?{
? ? ? ? ? ? ? ? ? ? ? ?location? ?~*^.*/favicon.ico$? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? root? ? ? ? ? ? /data/webapps;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? expires? ? ? ? ? ? 180d;? ? ? ? ? ? ?#圖片緩存時(shí)長(zhǎng)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? }??
? ? ? ? ? ? ? ? ? ? ? if(!-f$request_filename)? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?proxy_pass? ? ? ??http://tomcat8080;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ?}??
}
? ? ? ? ? ? ? ? ? ? ? error_page? ? ? ? ? 500? 502? 503? 504? ? ? ? ? ?/50x.html
? ? ? ? ? ? ? ? ? ? ? ? ? ?location? ?=? ?/50x.html? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ?root? ? ? ? ? ? ?html;
? ? ? }
}
server? ? ? ? ? ?{
? ? ? ? listen? ? ? ? ? 8088;
? ? ? ? server_name? ? ? ? ? ? ? nginx_status;
? ? ? ? ? ? ? ? ? ? ? ?location? ? ? ? /? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?access_log? ? ? ? ? ? off;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?deny? ? ? ? ? ? ?all;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?return? ? ? ? ?503;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ?location? ? ?/status? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stub_status? ? ? ? ? on;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? access_log? ? ? ? ? off;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allow? ? ? ? ? ? ? ? ? ?127.0.0.1;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allow? ? ? ? ? ? ? ? ? ?127.18.100.70;
? ? ? ? ? ? ? ? }
? ? ? ? }
}
Tengine了解
tengine.taobao.org