1 00:00:06,170 --> 00:00:09,010 So what exactly is reverse proxy in. 2 00:00:09,020 --> 00:00:15,800 Simply put a reverse proxy act as an intermediary between a client for example a browser and the resource 3 00:00:15,800 --> 00:00:17,120 itself. 4 00:00:17,120 --> 00:00:23,480 For example the most common use case on in generics is one we've already covered BHB when we implement 5 00:00:23,480 --> 00:00:32,240 a be back in using P P F be M engine X. acted as a reverse proxy between the client and BHB fbm. 6 00:00:32,270 --> 00:00:38,810 So in other words interpreting the client's request fetching that request from patch P If p.m. and then 7 00:00:38,810 --> 00:00:40,640 sending it back to the client. 8 00:00:40,670 --> 00:00:47,510 Hence the term reverse with the client assuming all requests are coming to and from that engine X server. 9 00:00:47,510 --> 00:00:50,800 This is of course a very powerful feature for many applications. 10 00:00:50,990 --> 00:00:56,740 But in the context of any generics as a web server it will most often be used to proxy somewhere the 11 00:00:56,780 --> 00:01:02,360 back in service be that patch be no Dodger s Ruby etc.. 12 00:01:02,810 --> 00:01:06,250 I still have that engine Xserve from the previous list and running. 13 00:01:07,720 --> 00:01:09,580 Hello from in Genex. 14 00:01:09,610 --> 00:01:15,940 So to demonstrate the most basic reverse proxy I'll start a new web server using P HP. 15 00:01:15,970 --> 00:01:24,280 Again with that response style which echoes Hello from page B test that's working all 9 9 9 9 which 16 00:01:24,280 --> 00:01:25,470 it is now. 17 00:01:25,480 --> 00:01:29,740 Let's say we want to access this BHB server via our engineered server. 18 00:01:29,980 --> 00:01:38,040 This one on port 8 8 8 8 and we want to do so by accessing the slash P HP path or request you are right. 19 00:01:38,290 --> 00:01:43,570 I'll switch back to the engine XT configuration file we are using create that new location block for 20 00:01:43,570 --> 00:01:50,150 Slash patch P and now two proxy requests on this path to the PDB server. 21 00:01:50,170 --> 00:01:58,030 We can say proxy underscore pass which should look familiar from the Patch be back end Listen HTC be 22 00:01:58,120 --> 00:02:06,280 local post 9 9 9 9 the full You are all of that BHB server with a trailing slash which is important 23 00:02:06,400 --> 00:02:07,990 and will cover in a minute. 24 00:02:08,410 --> 00:02:11,950 I'll just wrap this in quotes to make it a bit more readable. 25 00:02:13,070 --> 00:02:20,330 Reload engine X server with the S flag and reload and now test this reverse proxy by requesting that 26 00:02:20,330 --> 00:02:21,250 slash patch. 27 00:02:21,340 --> 00:02:23,500 Request on our engine X server. 28 00:02:24,500 --> 00:02:26,470 Hello from BHB server. 29 00:02:26,780 --> 00:02:33,120 So a response clearly from I appear to be server but accessed and reverse proxied via a were engine 30 00:02:33,120 --> 00:02:37,350 X server kurla again with out the slash BHP. 31 00:02:37,550 --> 00:02:40,990 And again we're back to getting the engine X response as before. 32 00:02:41,730 --> 00:02:47,720 There is of course no restrictions to this proxy server or servers being on the same system either. 33 00:02:47,730 --> 00:02:50,610 For example I'll create a third location block. 34 00:02:51,490 --> 00:02:53,680 Listening for engine X org. 35 00:02:55,230 --> 00:02:58,130 And proxy this one to engine X dot org. 36 00:02:58,210 --> 00:02:59,710 So a remote site. 37 00:03:01,090 --> 00:03:03,070 Reload the configuration. 38 00:03:04,130 --> 00:03:04,710 Try that. 39 00:03:04,730 --> 00:03:06,430 Engine X or path. 40 00:03:08,960 --> 00:03:11,690 And we get a whole bunch of HDMI L.. 41 00:03:11,880 --> 00:03:14,190 I'll access it via the browser. 42 00:03:17,630 --> 00:03:18,550 Where at load. 43 00:03:18,590 --> 00:03:25,400 As if we're visiting engine X dot org directly so a completely remote website being proxied via our 44 00:03:25,400 --> 00:03:26,140 server. 45 00:03:26,870 --> 00:03:32,750 Now when I mentioned the importance of keeping the trailing slash on the proxy drl the reason for this 46 00:03:32,750 --> 00:03:36,910 is because of how engine X handles proxy paths to demonstrate. 47 00:03:36,920 --> 00:03:44,080 I'll create a new page prescript to pass to appear to be server corlett show underscore request dot 48 00:03:44,130 --> 00:03:45,220 patch B. 49 00:03:46,010 --> 00:03:48,720 Open that new file and in. 50 00:03:48,740 --> 00:03:54,290 I'm simply going to echo out the request you are right as received on the pier to be server. 51 00:03:54,380 --> 00:03:57,960 So in other words what our engineers proxy is requesting. 52 00:04:00,320 --> 00:04:01,730 Server. 53 00:04:02,520 --> 00:04:03,190 Request. 54 00:04:03,210 --> 00:04:04,140 You are I. 55 00:04:06,460 --> 00:04:10,310 Save that and launched a BHB server with a new file. 56 00:04:11,270 --> 00:04:13,190 Show underscore request. 57 00:04:15,310 --> 00:04:15,500 OK. 58 00:04:15,520 --> 00:04:21,320 Now when we call this engine X server the HP server receives slash or root. 59 00:04:21,790 --> 00:04:25,560 So there's no request being passed to the pier every server. 60 00:04:25,570 --> 00:04:32,050 If however I changed the proxy path in the engine X configuration file to not include the trailing slash 61 00:04:32,200 --> 00:04:33,240 like so. 62 00:04:34,930 --> 00:04:36,580 Curl again. 63 00:04:37,360 --> 00:04:41,110 We get slash P B so the entire you are right. 64 00:04:41,140 --> 00:04:44,070 As we requested it from the engine X proxy server. 65 00:04:44,140 --> 00:04:50,960 What this means then is that unless we specify a proxy path engine X assumes the original request path. 66 00:04:51,130 --> 00:04:57,290 Now sure you can create use cases for this but ultimately it's inconsistent and not recommended. 67 00:04:57,370 --> 00:05:00,980 So at that request you arrive to the proxy Dural. 68 00:05:01,090 --> 00:05:07,720 In this case when having the U R L specified as before we can still pass a customer request after the 69 00:05:07,720 --> 00:05:13,290 proxy path say BHB slash some slash you are L.. 70 00:05:14,760 --> 00:05:15,350 What the B. 71 00:05:15,370 --> 00:05:20,030 B server receives is that last part some u are l. 72 00:05:20,140 --> 00:05:23,780 Don't worry too much about the double slashes at the start here. 73 00:05:23,920 --> 00:05:29,980 It's just how engineers handles the u r l substitution but it's normalized automatically and shouldn't 74 00:05:29,980 --> 00:05:31,450 cause any issues. 75 00:05:31,510 --> 00:05:37,140 This then being a logical approach to passing request you rise to a proxy server. 76 00:05:37,210 --> 00:05:43,150 Another important aspect of using engine X as a reverse proxy is passing custom headers to either the 77 00:05:43,150 --> 00:05:50,140 proxy server I appear to be server in this case or to the client on the reverse part of the proxy. 78 00:05:50,220 --> 00:05:55,090 First we can pass headers to the client using the standard add header directive. 79 00:05:55,450 --> 00:06:00,280 I'll name this header proxied with a value of engine x. 80 00:06:00,940 --> 00:06:05,980 Reload our configuration and test by curling that patch piece path. 81 00:06:08,530 --> 00:06:10,190 And we see the header. 82 00:06:10,330 --> 00:06:13,270 Remember however this is being added to the client. 83 00:06:13,300 --> 00:06:18,760 Only on the reverse part of the proxy so to add a header that gets sent to the proxy server. 84 00:06:18,780 --> 00:06:22,740 O appear each piece server we have to change this slightly. 85 00:06:22,760 --> 00:06:31,530 First let's modify that show request file to print out all request headers var dump get all headers. 86 00:06:31,750 --> 00:06:38,380 If you're not familiar with Patch B var dump is just a very verbose way of logging out variable or the 87 00:06:38,380 --> 00:06:41,100 return value of this function in our case. 88 00:06:41,140 --> 00:06:43,110 Restart the peer to p server. 89 00:06:43,930 --> 00:06:44,970 Test that. 90 00:06:45,720 --> 00:06:51,180 We get all our headers but of course not that proxy to header We added as that's going to the client 91 00:06:51,810 --> 00:06:59,850 to add this to the proxy request then we can simply change add header to proxy set header meaning set 92 00:06:59,880 --> 00:07:03,530 this header on the proxy request reload. 93 00:07:04,710 --> 00:07:11,220 Call that again and this time the very first value of this dumped headers array is that proxy to header 94 00:07:11,490 --> 00:07:13,730 with the value in Genex. 95 00:07:13,890 --> 00:07:19,950 That wraps up using engine X as a reverse proxy which as you can see engine X makes very easy in the 96 00:07:19,950 --> 00:07:24,290 next list will apply what we've learnt here to create a basic load balancer.