在nginx中配置proxy_pass時,若是是按照^~匹配路徑時,要注意proxy_pass後的url最後的/。當加上了/,至關因而絕對根路徑,則nginx不會把location中匹配的路徑部分代理走;若是沒有/,則會把匹配的路徑部分也給代理走。javascript
好比下面設置: location ^~ /wangshibo/ { proxy_cache js_cache; proxy_set_header Host js.test.com; proxy_pass http://js.test.com/; } 如上面的配置,若是請求的url是http://servername/wangshibo/test.html會被代理成http://js.test.com/test.html 而若是這麼配置 location ^~ /wangshibo/ { proxy_cache js_cache; proxy_set_header Host js.test.com; proxy_pass http://js.test.com; } 則請求的url是http://servername/wangshibo/test.html會被代理到http://js.test.com/wangshibo/test.html 固然,能夠用以下的rewrite來實現/的功能 location ^~ /wangshibo/ { proxy_cache js_cache; proxy_set_header Host js.test.com; rewrite /wangshibo/(.+)$ /$1 break; proxy_pass http://js.test.com; }
=====================注意一些配置細節=========================php
upstream bobo { ip_hash; server 192.168.10.73:80 max_fails=3 fail_timeout=15s; server 192.168.10.74:80 max_fails=3 fail_timeout=15s; } server { listen 80; server_name bobo.kevin.com; access_log /data/nginx/logs/bobo.kevin.com-access.log main; error_log /data/nginx/logs/bobo.kevin.com-error.log; location / { proxy_pass http://bobo; proxy_redirect off ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 600; proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404; proxy_max_temp_file_size 128m; #proxy_cache mycache; #proxy_cache_valid 200 302 1h; #proxy_cache_valid 301 1d; #proxy_cache_valid any 1m; } location /los/ { proxy_pass http://fvt.kevin.com; } } location /han/ { proxy_pass http://dcc.kevin.com/; } } 解釋說明: 1)訪問http://bobo.kevin.com的請求會負載分發到192.168.10.73:80和192.168.10.74:80上面 2)訪問http://bobo.kevin.com/los/.... 會代理跳轉到http://fvt.kevin.com/los/.... 上面 3)訪問http://bobo.kevin.com/han/.... 會代理跳轉到http://dcc.kevin.com/.... 上面 4)注意proxy_pass跳轉upstream模塊時的配置和跳轉到一個完整url的配置(後者不須要加那些header配置以及健康檢查配置)
以下面一個配置實例html
1)第一種配置 [root@BJLX_16_202_V vhosts]# cat ssl-wangshibo.conf upstream at { server 192.168.1.202:8080 max_fails=3 fail_timeout=30s; } server { listen 443; server_name www.wangshibo.com; ssl on; ### SSL log files ### access_log logs/wangshibo_access.log; error_log logs/wangshibo_error.log; ### SSL cert files ### ssl_certificate ssl/wang.cer; ssl_certificate_key ssl/wang.key; location /attendance/ { proxy_pass http://at; //不須要加上"/" proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_redirect off; } } 訪問https://www.wangshibo.com/attendance/和http://192.168.1.202:8080/attendance結果是一致的。 2)第二種配置 [root@BJLX_16_202_V vhosts]# cat ssl-wangshibo.conf upstream at { server 192.168.1.202:8080 max_fails=3 fail_timeout=30s; } server { listen 443; server_name www.wangshibo.com; ssl on; ### SSL log files ### access_log logs/wangshibo_access.log; error_log logs/wangshibo_error.log; ### SSL cert files ### ssl_certificate ssl/wang.cer; ssl_certificate_key ssl/wang.key; location / { proxy_pass http://at/attendance/; //必定要加上"/" proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_redirect off; } } 訪問https://www.wangshibo.com和http://192.168.1.202:8080/attendance結果是一致的。
以下配置,想要實現的需求:
192.168.1.27是後端的real server,8080端口是公司的ehr人事系統端口。
又因爲該系統涉及到微信接口訪問,即http://ehr.wang.com/attendance和http://ehr.wang.com/app
因爲是內部系統,安全考慮,因此要求:
1)登陸ehr人事系統的時候要求使用內網登陸,即http://192.168.1.27:8080,訪問前要先登陸公司***
2)登陸微信接口http://ehr.wang.com/attendance和http://ehr.wang.com/app使用外網登陸,即便用解析後域名登陸。
3)訪問http://ehr.wang.com,強制跳轉爲https://ehr.wang.com/attendancejava
[root@BJLX_4_21_P vhosts]# cat ehr.conf server { listen 80; server_name ehr.wang.com; access_log logs/ehr_access.log; error_log logs/ehr_error.log; return 301 https://$server_name$request_uri; } [root@BJLX_4_21_P vhosts]# cat ssl-ehr.conf upstream ehr { server 192.168.1.27:8080 max_fails=3 fail_timeout=30s; } server { listen 443; server_name ehr.wang.com; ssl on; ### SSL log files ### access_log logs/ehr_access.log; error_log logs/ehr_error.log; ### SSL cert files ### ssl_certificate ssl/wang.cer; ssl_certificate_key ssl/wang.key; #ssl_session_timeout 5m; location / { return 301 https://ehr.wang.com/attendance; } location /attendance/ { proxy_pass http://ehr; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # proxy_set_header X-Forwarded-Proto https; #proxy_set_header X-Forwarded-Proto https; proxy_redirect off; } location /app/ { proxy_pass http://ehr; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # proxy_set_header X-Forwarded-Proto https; #proxy_set_header X-Forwarded-Proto https; proxy_redirect off; } } 注意: 因爲從瀏覽器訪問(http)到源站的real server之間要通過Nginx反向代理層(https) 須要將proxy_set_header X-Forwarded-Proto https;這一行註釋掉,不然上面的配置無效。 若是中間沒有代理層,直接是在real server本機進行nginx的反向代理(即本機nginx反代到本機的8080端口),則這個參數無需註釋(已通過驗證)
---------------------------------------------------------------------------------------------------------------------------------------nginx
再看下面一個需求: 訪問https://toc.wang.com/storage/file/ 反向代理到http://192.168.80.105:80/storage/file/ 訪問https://toc.wang.com/storage/file/uploadOne 反向代理到http://192.168.80.130:8080/storage/file/uploadOne 訪問https://toc.wang.com/storage/file/upload 反向代理到http://192.168.80.130:8080/storage/file/upload 訪問其餘https://toc.wang.com的url 反向代理到http://192.168.80.105:8080 在nginx的LB層的反向代理配置以下: [root@nginx-web01 vhosts]# cat ssl-toc.conf upstream toc-105-80 { server 192.168.80.105:80 max_fails=3 fail_timeout=10s; } upstream toc-130-8080-1 { server 192.168.80.130:8080 max_fails=3 fail_timeout=10s; } upstream toc-130-8080-2 { server 192.168.80.130:8080 max_fails=3 fail_timeout=10s; } upstream toc-105-8080 { server 192.168.80.105:8080 max_fails=3 fail_timeout=10s; } server { listen 443; server_name toc.wang.com; ssl on; ### SSL log files ### access_log logs/toc_access.log; error_log logs/toc_error.log; ### SSL cert files ### ssl_certificate ssl/wang.cer; ssl_certificate_key ssl/wang.key; # ssl_session_timeout 5m; location /storage/file/ { proxy_pass http://toc-105-80/storage/file/; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_set_header X-Forwarded-Proto https; proxy_redirect off; } location /storage/file/uploadOne { proxy_pass http://toc-130-8080-1/storage/file/uploadOne; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_set_header X-Forwarded-Proto https; proxy_redirect off; } location /storage/file/upload { proxy_pass http://toc-130-8080-2/storage/file/upload; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_set_header X-Forwarded-Proto https; proxy_redirect off; } location / { proxy_pass http://toc-105-8080; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_set_header X-Forwarded-Proto https; proxy_redirect off; } } -------------------------------------------------------------------------------------------------- 訪問外網地址https://test.wang.com/storage/* 反向代理到內網地址 http://storage.wang-inc.com/*,即實現外網的連接訪問到內網的文件地址。 1)內網url的配置 [root@lb-ng01 vhosts]# cat storage.wang-inc.conf upstream storage { server 192.168.1.25:8080 max_fails=3 fail_timeout=10s; server 192.168.1.26:8080 max_fails=3 fail_timeout=10s; } server { listen 80; server_name storage.wang-inc.com storage.wang.cn; access_log logs/storage_access.log main; error_log logs/storage_error.log; location / { proxy_pass http://storage; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; } } 2)外網反向代理配置 [root@nginx-web01 vhosts]# cat ssl-test.conf upstream storage { server 192.168.1.25:8080 max_fails=3 fail_timeout=10s; server 192.168.1.26:8080 max_fails=3 fail_timeout=10s; } server { listen 443; server_name test.wang.com; ssl on; ### SSL log files ### access_log logs/test_access.log main; error_log logs/test_error.log; ### SSL cert files ### ssl_certificate ssl/wang.cer; ssl_certificate_key ssl/wang.key; ssl_session_timeout 5m; location /storage/ { proxy_pass http://storage/; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_set_header X-Forwarded-Proto https; proxy_redirect off; } } ------------------------------------------------------------------------------------------------- 訪問http://im.wang.com/_matrix/push/v1/notify 反向代理到192.168.1.36的5000端口 [root@nginx-web01 vhosts]# cat im.conf upstream matrix { server 192.168.1.36:5000 max_fails=3 fail_timeout=10s; } server { listen 80; server_name im.wang.com; access_log logs/im_access.log main; error_log logs/im_error.log; location /_matrix/push/v1/notify { proxy_pass http://matrix; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_set_header X-Forwarded-Proto https; proxy_redirect off; } }
HTTP頭域(proxy_set_header)列表與解釋
HTTP 頭域是HTTP協議中請求(request)和響應(response)中的頭部信息,其實就是HTTP通訊的操做參數,告訴web服務器和瀏覽器怎樣處理這個通訊。
HTTP頭從一個請求信息或者響應信息的第二行開始(第一行是請求行或者響應行),以兩個CR-LF字符組結束(CR:回車符,\r,LF:換行符\n)
而每一個HTTP頭是字符串形式的,用冒號分割的鍵值對,多個HTTP頭之間用CR-LF字符組隔開。web
某些http頭能夠有註釋,例如user-agent,server,via。但這些註釋會被服務器或者瀏覽器忽略IETF組織已經將一些核心的HTTP頭定義在RFC2616規範中,
這些HTTP頭是每一個基於HTTP協議的軟件必須實現的,而其餘一些更新和擴展的頭域也必須被基於HTTP的軟件實現。固然,各個軟件也能夠定義本身的頭域。ajax
另外一方面,RFC2616規範中並無限制每一個HTTP頭的長度,或者限制HTTP頭的數量,但出於性能和安全的考慮,多數服務器都會本身做規定,例如apache2.3
就規定每一個HTTP頭不能超過8190個字節,每一個請求不能超過100個HTTP頭。apache
如下來看看發送一個請求(request)時候,可能包含的各個HTTP頭和它的解釋。bootstrap
------------標準的請求頭------------後端
Accept: 瀏覽器(或者其餘基於HTTP的客戶端程序)能夠接收的內容類型(Content-types),例如 Accept: text/plain Accept-Charset:瀏覽器能識別的字符集,例如 Accept-Charset: utf-8 Accept-Encoding:瀏覽器能夠處理的編碼方式,注意這裏的編碼方式有別於字符集,這裏的編碼方式一般指gzip,deflate等。 例如 Accept-Encoding: gzip, deflate Accept-Language:瀏覽器接收的語言,其實也就是用戶在什麼語言地區,例如簡體中文的就是 Accept-Language: zh-CN Authorization:在HTTP中,服務器能夠對一些資源進行認證保護,若是你要訪問這些資源,就要提供用戶名和密碼,這個用戶名和密碼就是在Authorization 頭中附帶的,格式是「username:password」字符串的base64編碼,例如:Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==中, basic指使用basic認證方式, QWxhZGRpbjpvcGVuIHNlc2FtZQ==使用base64解碼就是「Aladdin:open sesame」 Cache-Control:這個指令在request和response中都有,用來指示緩存系統(服務器上的,或者瀏覽器上的)應該怎樣處理緩存,由於這個頭域比較重要, 特別是但願使用緩 存改善性能的時候,內容也較多,因此我想在下一篇博文中主要介紹一下。 Connection:告訴服務器這個user agent(一般就是瀏覽器)想要使用怎樣的鏈接方式。值有keep-alive和close。http1.1默認是keep-alive。keep-alive就是 瀏覽器和服務器 的通訊鏈接會被持續保存,不會立刻關閉,而close就會在response後立刻關閉。但這裏要注意一點,咱們說HTTP是無狀態的,跟 這個是否keep-alive沒有關係,不要認爲keep-alive是對HTTP無狀態的特性的改進。 Cookie:瀏覽器向服務器發送請求時發送cookie,或者服務器向瀏覽器附加cookie,就是將cookie附近在這裏的。例如:Cookie:user=admin Content-Length:一個請求的請求體的內存長度,單位爲字節(byte)。請求體是指在HTTP頭結束後,兩個CR-LF字符組以後的內容, 常見的有POST提交的表單數據,這個Content-Length並不包含請求行和HTTP頭的數據長度。 Content-MD5:使用base64進行了編碼的請求體的MD5校驗和。例如:Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ== Content-Type:請求體中的內容的mime類型。一般只會用在POST和PUT方法的請求中。例如:Content-Type: application/x-www-form-urlencoded Date:發送請求時的GMT時間。例如:Date: Tue, 15 Nov 1994 08:12:31 GMT Expect:指示須要使用服務器某些特殊的功能。(這個我不是很清楚) From:發送這個請求的用戶的email地址。例如:From: user@example.com Host:被服務器的域名或IP地址,若是不是通用端口,還包含該端口號,例如:Host: www.some.com:182 If-Match:一般用在使用PUT方法對服務器資源進行更新的請求中,意思就是,詢問服務器,如今正在請求的資源的tag和這個If-Match的tag相不相同,若是相同, 則證實服務器上的這個資源仍是舊的,如今能夠被更新,若是不相同,則證實該資源被更新過,如今就不用再更新了(不然有可能覆蓋掉其餘人所作的更改)。 If-Modified-Since:詢問服務器如今正在請求的資源在某個時間以來有沒有被修改過,若是沒有,服務器則返回304狀態來告訴瀏覽器使用瀏覽器本身本地的緩存, 若是有修改過,則返回200,併發送新的資源(固然若是資源不存在,則返回404。) If-None-Match:和If-Modified-Since用意差很少,不過不是根據時間來肯定,而是根據一個叫ETag的東西來肯定。關於etag我想在下一篇博客介紹一下。 If-Range:告訴服務器若是這個資源沒有更改過(根據If-Range後面給出的Etag判斷),就發送這個資源中在瀏覽器缺乏了的某些部分給瀏覽器, 若是該資源以及被修改過,則將整個資源從新發送一份給瀏覽器。 If-Unmodified-Since:詢問服務器如今正在請求的資源在某個時刻以來是否沒有被修改過。 Max-Forwards:限制請求信息在代理服務器或網關中向前傳遞的次數。 Pragma:好像只有一個值,就是:no-cache。Pragma:no-cache 與cache-control:no-cache相同,只不過cache-control:no-cache是http1.1專門指定的, 而Pragma:no-cache能夠在http1.0和1.1中使用 Proxy-Authorization:鏈接到某個代理時使用的身份認證信息,跟Authorization頭差很少。例如:Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== Range:在HTTP頭中,"Range"字眼都表示「資源的byte形式數據的順序排列,而且取其某一段數據」的意思。Range頭就是表示請求資源的從某個數值到某個數值間的數據, 例如:Range: bytes=500-999 就是表示請求資源從500到999byte的數據。數據的分段下載和多線程下載就是利用這個實現的。 Referer:指當前請求的URL是在什麼地址引用的。例如在www.a.com/index.html頁面中點擊一個指向www.b.com的超連接, 那麼,這個www.b.com的請求中的Referer就是www.a.com/index.html。一般咱們見到的圖片防盜鏈就是用這個實現的。 Upgrade:請求服務器更新至另一個協議,例如:Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11 User-Agent:一般就是用戶的瀏覽器相關信息。例如:User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0 Via:用來記錄一個請求通過了哪些代理或網關才被送到目標服務器上。例如一個請求從瀏覽器出發(假設使用http/1.0),發送給名爲 SomeProxy的內部代理, 而後被轉發至www.somenet.com的公共代理(使用http/1.1),最後被轉發至目標服務器www.someweb.com,那麼在someweb.com中收到的via 頭應該是: via:1.0 someProxy 1.1 www.someweb.com(apache 1.1) Warning:記錄一些警告信息。
------------通用但非標準的HTTP頭(一般,非標準的頭域都是用「X-」開頭,例如"x-powered-by")------------
X-Requested-With:主要是用來識別ajax請求,不少javascript框架會發送這個頭域(值爲XMLHttpRequest) DNT:DO NOT TRACK的縮寫,要求服務器程序不要跟蹤記錄用戶信息。DNT: 1 (開啓DNT) DNT: 0 (關閉DNT)火狐,safari,IE9都支持這個頭域, 而且於2011年3月7日被提交至IETF組織實現標準化 X-Forwarded-For:記錄一個請求從客戶端出發到目標服務器過程當中經歷的代理,或者負載平衡設備的IP。 X-Forwarded-Proto:記錄一個請求最初從瀏覽器發出時候,是使用什麼協議。由於有可能當一個請求最初和反向代理通訊時,是使用https, 但反向代理和服務器通訊時改變成http協議,這個時候,X-Forwarded-Proto的值應該是https Front-End-Https:微軟使用與其負載平衡的一個頭域。 X-ATT-DeviceId:AT&A的產品中使用的頭域,不過不是很清楚用途。
------------------曾經註釋"proxy_set_header X-Forwarded-Proto https;"的一個坑-------------------
訪問問卷系統https://wj.wang.com/limesurvey/index.php/admin/authentication/sa/login,登陸的時候自動跳轉到了公司官網首頁。
這個問卷系統只能經過https方式訪問,http方式訪問的都自動跳轉到公司官網。
緣由就是由於在nginx代理層註釋了這個參數: [root@nginx-web01 vhosts]# cat ssl-wj.conf upstream ssl-wj { server 192.168.1.22:10086 max_fails=3 fail_timeout=10s; } server { listen 443; server_name wj.wang.com; ssl on; ### SSL log files ### access_log logs/wj_access.log; error_log logs/wj_error.log; ### SSL cert files ### ssl_certificate ssl/wang.cer; ssl_certificate_key ssl/wang.key; ssl_session_timeout 5m; location / { proxy_pass http://ssl-wj/; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # proxy_set_header X-Forwarded-Proto https; //打開這個參數的註釋便可 proxy_redirect off; } }
=======================================================================
以下Nginx代理轉發需求:
訪問http://grace.kevin.com的80端口的請求,轉發至後端192.168.10.173和192.168.10.174的80端口(這部分是靜態頁的請求)。
當訪問地址中匹配/leasecore/、/cms/api/、/cap/api/、/crm/api/、/cms/third/的上下文path時,轉發至192.168.10.173和192.168.10.173的8080端口(tomcat處理)
LB層的Nginx轉發配置: =============================================================== [root@inner-lb01 vhosts]# cat grace.kevin.com.conf upstream zl-80 { server 192.168.10.173:80 max_fails=3 fail_timeout=15s; server 192.168.10.174:80 max_fails=3 fail_timeout=15s; } upstream leasecore { server 192.168.10.173:8080 max_fails=3 fail_timeout=15s; server 192.168.10.174:8080 max_fails=3 fail_timeout=15s; } upstream cms-api { server 192.168.10.173:8080 max_fails=3 fail_timeout=15s; server 192.168.10.174:8080 max_fails=3 fail_timeout=15s; } upstream cap-api { server 192.168.10.173:8080 max_fails=3 fail_timeout=15s; server 192.168.10.174:8080 max_fails=3 fail_timeout=15s; } upstream crm-api { server 192.168.10.173:8080 max_fails=3 fail_timeout=15s; server 192.168.10.174:8080 max_fails=3 fail_timeout=15s; } upstream cms-third { server 192.168.10.173:8080 max_fails=3 fail_timeout=15s; server 192.168.10.174:8080 max_fails=3 fail_timeout=15s; } server { listen 80; server_name grace.kevin.com; access_log /data/nginx/logs/grace.kevin.com-access.log main; error_log /data/nginx/logs/grace.kevin.com-error.log; location / { proxy_pass http://zl-80; proxy_redirect off ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 600; proxy_ignore_client_abort on; proxy_next_upstream error timeout invalid_header http_502 http_503 http_504; proxy_max_temp_file_size 128m; } location ^~ /leasecore/ { proxy_pass http://leasecore; proxy_redirect off ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 600; proxy_ignore_client_abort on; proxy_next_upstream error timeout invalid_header http_502 http_503 http_504; proxy_max_temp_file_size 128m; } location ^~ /cms/api/ { proxy_pass http://cms-api; proxy_redirect off ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 600; proxy_ignore_client_abort on; proxy_next_upstream error timeout invalid_header http_502 http_503 http_504; proxy_max_temp_file_size 128m; } location ^~ /cap/api/ { proxy_pass http://cap-api; proxy_redirect off ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 600; proxy_ignore_client_abort on; proxy_next_upstream error timeout invalid_header http_502 http_503 http_504; proxy_max_temp_file_size 128m; } location ^~ /crm/api/ { proxy_pass http://crm-api; proxy_redirect off ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 600; proxy_ignore_client_abort on; proxy_next_upstream error timeout invalid_header http_502 http_503 http_504; proxy_max_temp_file_size 128m; } location ^~ /cms/third/ { proxy_pass http://cms-third; proxy_redirect off ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 600; proxy_ignore_client_abort on; proxy_next_upstream error timeout invalid_header http_502 http_503 http_504; proxy_max_temp_file_size 128m; } } 後端192.168.10.173和192.168.10.174的nginx配置 ============================================================== [root@zl-app02 ~]# cat /data/nginx/conf/vhosts/uatzl-zpp.conf server { listen 80; server_name uatzl-app02.kevin.cn; access_log /data/nginx/logs/access.log main; error_log /data/nginx/logs/error.log; location / { root /data/lease_app/dist; index index.html index.htm; } } [root@zl-app02 ~]# ps -ef|grep tomcat app 28224 1 0 Dec13 ? 00:06:00 /data/jdk1.8.0_121/bin/java -Djava.util.logging.config.file=/data/tomcat-8.0.47/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Djava.endorsed.dirs=/data/tomcat-8.0.47/endorsed -classpath /data/tomcat-8.0.47/bin/bootstrap.jar:/data/tomcat-8.0.47/bin/tomcat-juli.jar -Dcatalina.base=/data/tomcat-8.0.47 -Dcatalina.home=/data/tomcat-8.0.47 -Djava.io.tmpdir=/data/tomcat-8.0.47/temp org.apache.catalina.startup.Bootstrap start root 32535 32509 0 14:38 pts/1 00:00:00 grep tomcat
=======================================================================
順便貼個配置案例: 好比:訪問http://www.kevin.com 跳轉到http://192.168.1.20:9040/portal-pc/ upstream scf_cluster { ip_hash; server 192.168.1.20:9020; server 192.168.1.21:9020; } upstream portal_cluster { ip_hash; server 192.168.1.20:9040; server 192.168.1.21:9040; } upstream file_cluster{ ip_hash; server 192.168.1.20:9020; } upstream workflow_cluster{ ip_hash; server 192.168.1.20:9020; server 192.168.1.21:9020; } upstream batch_cluster{ server 192.168.1.20:9020; server 192.168.1.21:9020; } upstream mobi_cluster{ server 192.168.1.20:8080; } server { listen 80; server_name www.kevin.com; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location /scf { proxy_pass http://scf_cluster/scf; proxy_redirect http://scf_cluster/scf http://www.kevin.com/scf; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location / { proxy_pass http://portal_cluster/portal-pc/; proxy_redirect http://portal_cluster/portal-pc/ http://www.kevin.com/; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /msdp-file { proxy_pass http://file_cluster/msdp-file; proxy_redirect http://file_cluster/msdp-file http://www.kevin.com/msdp-file; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /upload { proxy_pass http://file_cluster/upload; proxy_redirect http://file_cluster/upload http://www.kevin.com/upload; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /activiti-workflow-console { proxy_pass http://workflow_cluster/activiti-workflow-console; proxy_redirect http://workflow_cluster/activiti-workflow-console http://www.kevin.com/activiti-workflow-console; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /batch-framework-web { proxy_pass http://batch_cluster/batch-framework-web; proxy_redirect http://batch_cluster/batch-framework-web http://www.kevin.com/batch-framework-web; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 80; server_name mobi.kevin.com; location / { proxy_pass http://mobi_cluster; proxy_redirect http://mobi_cluster/ http://mobi.kevin.com/; 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; } } server{ listen 80; server_name kevin.com; rewrite ^(.*)$ https://www.kevin.com$1 permanent; }