Je una application ambayo inalisten kwenye port fulani na ungependa port isiwepo kwenye url ya hiyo application? Au una nodejs application inayorun kwenye port fulani?
kiufupi unataka kubadili
kuwa
Katika somo hili tutajifunza namna ya kutumia nginx kama proxy. nginx ni web server maarufu sana, pamoja na umaarufu wake kama web server pia inaweza kutumika kama proxy au load balancer.
location /app1/api inaiambia nginx ifananishe path ya url kama imematch /app1/api basi ipitishe request zote kwenda kwenye upstream server iliyopo kwenye url http://localhost:8080.
Hii URL inaweza kuwa server nyigine kutegemea application yako ipo wapi. Kwa server nyingine URL inaweza kuwa
Kama utahitaji Host kuwa $host
Tofauti ya $host na $proxy_host. $proxy_host ni host uliyoiset kwenye proxy_pass kwa hiyo kama uliset http://localhost:8080 basi $proxy_host itakua localhost.
Tumia $host ili Host iwe sawa na ile ya server_name kwenye server block.
kwa mfano domain.com.
$host inaweza kuwa host ya request au "Host" kwenye request header au server_name kwenye server block.
Kwani usipofanya hivyo application yako itaiona nginx kama ndio client aliyefanya request.
nginx inatumia "Round Robin" algorithm by default kama haujachagua algorithm ya kutumia.
Algorithm zingine za load balancing
hash, ip_hash, least_conn, least_time na random
Ili kuchagua algorithm ya load balancing utaiandika kwenye upstream server list zako
kiufupi unataka kubadili
https://domain.com:8080/api
kuwa
https://domain.com/app1/api
Katika somo hili tutajifunza namna ya kutumia nginx kama proxy. nginx ni web server maarufu sana, pamoja na umaarufu wake kama web server pia inaweza kutumika kama proxy au load balancer.
1. Basic setup
server {
listen 80;
server_name domain.com www.domain.com;
...
location /app1/api {
proxy_pass http://localhost:8080;
}
...
}
location /app1/api inaiambia nginx ifananishe path ya url kama imematch /app1/api basi ipitishe request zote kwenda kwenye upstream server iliyopo kwenye url http://localhost:8080.
Hii URL inaweza kuwa server nyigine kutegemea application yako ipo wapi. Kwa server nyingine URL inaweza kuwa
proxy_pass http://IP_ADDRESS:PORT;
2. HTTP Headers
Kwa kawaida nginx haipitishi http headers zote kwenda kwenye upstream server au application. Vile vile nginx inabadilisha baadhi ya headers, hivyo kama utahitaji ziwe tofauti na nginx ilivyobadili unaweza kuziset wewe mwenyewe. Header zinazobadilishwa na nginx ni "Host" na "Connection". Host inakuwa $proxy_host na Connection inakuwa "close".Kama utahitaji Host kuwa $host
location /app1/api {
proxy_set_header Host $host;
proxy_pass http://localhost:8080;
}
Tofauti ya $host na $proxy_host. $proxy_host ni host uliyoiset kwenye proxy_pass kwa hiyo kama uliset http://localhost:8080 basi $proxy_host itakua localhost.
Tumia $host ili Host iwe sawa na ile ya server_name kwenye server block.
kwa mfano domain.com.
$host inaweza kuwa host ya request au "Host" kwenye request header au server_name kwenye server block.
3. Real IP Address
Kama application yako inahitaji kujua IP Address ya request, itabidi kuiambia nginx ipitishe IP Address hiyo.Kwani usipofanya hivyo application yako itaiona nginx kama ndio client aliyefanya request.
location /app1/api {
#pitisha IP Address ya request
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#weka hii kama unataka kupass protocol ya request, yaani http au https
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8080;
}
4. Cookie
Kama application yako inaset cookie, utahitaji kuiambia nginx ipitisha http header ya Set-Cookie
location /app1/api {
proxy_pass_header Set-Cookie;
proxy_pass http://localhost:8080;
}
5. Load Balancing
Application yako imekuwa maarufu na watu wengi wanaitumia. Server moja imeshakua haitoshi unahitaji kuongeza idadi ya server. Ili kuongeza idadi ya server unahitaji pia kuweka load balancer mbele ya server hizo. load balancer ndio itakayo route traffic kwenda kwenye server fulani kutokana na algorithm uliyochagua.
upstream maarufu_com {
server 192.168.0.1:8000;
server 192.168.0.2:8001;
}
# load balancing
server {
listen 80;
server_name maarufu.com;
access_log logs/maarufu.access.log main;
location / {
proxy_pass http://maarufu_com;
}
}
nginx inatumia "Round Robin" algorithm by default kama haujachagua algorithm ya kutumia.
Algorithm zingine za load balancing
hash, ip_hash, least_conn, least_time na random
Ili kuchagua algorithm ya load balancing utaiandika kwenye upstream server list zako
Comments
Post a Comment