Check out a free preview of the full Full Stack for Front-End Engineers, v3 course:
The "Nginx Redirection & Gzip" Lesson is part of the full, Full Stack for Front-End Engineers, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Jem discusses redirection with Nginx, and compression with gzip, briefly demonstrates what compression is, and the difference between hashing and compression.

Get Unlimited Access Now

Transcript from the "Nginx Redirection & Gzip" Lesson

>> So I wanna cover redirection. So how do you make something redirect to somewhere else from Nginx? But not too hard, but a useful command. We're gonna talk about Gzip, what it is, how and how compression works. Then we're going to add a subdomain to your application, just in case you need one, it's always helpful to do that, again, it's a little bit tricky.

[00:00:22] But it's not too tricky, but it's not as intuitive as you think. So the first one, if you want to modify your virtual configuration, your virtual server and say, I need a when I go to jumps act out LOL, slash help. It actually just redirects me to the Mozilla Developer documentation.

[00:00:41] You can do that. You don't have to do this right now this just an example of how to do a redirection in Nginx. Let's talk a bit about Gzip. Anybody know what Gzip is, have you heard of it? Well, most things why, not most things, html pages ec cetera, but they follow Gzips, a lot of things are gzipped.

[00:01:13] Gzip is just a compression algorithm that is run on Nginx, that compresses things. The browser, it's one of the few things the browser actually knows how to do. So that way we're not sending these massive files over the wire, we're actually sending compressed files over the wire. And the browser knows how to unpack them, Nginx knows how to pack them up.

[00:01:34] We can adjust our Gzip settings in /@cnginx.conf and you scroll down the Gzip, and you can set the level. Now the temptation is always accept the compression level, which says about 6 by the faults. You want to say, I want to turn it to 9, just as it goes, but remember, that means every single connection coming from this computer, that needs extra CPU power to do that compression.

[00:01:57] So it's got to take those files, and it's got to compress them, it's gonna decompress it the other way as well. So generally 6 is considered the right balance, speed and performance. If you're like, hey, I'm not running a lot and I want as much performance as possible, turn up to 9.

[00:02:11] But in general in production systems, you're not gonna do that. And feel free to play with it in /etc says vi etc/nginx/nginx.conf if you want to mess with Gzip, you don't need to but it's nice to know it's there. But how does compression work? And this is my most simplified example I can think of.

[00:02:36] So compression algorithm is just a way of rewriting a file, essentially, in a much way. So a file is always going to break down into 1s and 0s at the core, that's what we're doing right now, just moving 1s and 0s around. So what if, instead of having this long chain of 1s and 0s, we just describe it a little bit differently?

[00:02:57] So compression algorithm will say, hey, you got this line of code or this file, how about we just described it as it's 4 0s, and then 3 1s, and then 1 0, then 1 1s, then 5 0s? You do that over and over and over again. And it turns out you have this giant gigabyte file, and you can compress it down to a few megabytes just by describing it differently.

[00:03:21] And that's how a compression algorithm works, and there's different ways of describing the files. Now you're saying that's awesome, Jem, what's the difference between compression and hashing because they seem a little bit different or they seem very similar? Well, compression always has a fixed output, and it's going to be as small as possible versus hashing, we don't actually care about the size as much.

[00:03:44] So that's really kind of the difference. And one is designed to be unpacked, like you can't compress something without uncompressing it, otherwise, what's the point? Versus hashing is not necessarily designed to be reversed the other way around. And this is just a random fun fact, because we're front engineers here you can't Gzip a picture, why nots, but you can but it won't do anything, why not?

>> They're compressed.
>> That's right, the images are already compressed. That's why the old days, anybody who clitter on a bitmap knows that a bitmap is 5 megabytes, but a png of that same image can be 30 kilobytes. Because png, I kind of think it's some other, what are some other image formats?

>> JPEG.
>> JPEG.
>> JPEG.
>> Isn't that lossy?
>> JPEG can be lossy, yeah and PNG isn't necessarily, but those are all compression algorithms we're talking about. You remember MP3s? I know I'm dealing with stuff, I can't believe if you all know what MP3 are, but
>> How do they not know what MP look like?

[00:04:47] Did everybody move over to FLAC or something?
>> Some people don't know what MP3, like why would you know anymore? Spotify, and YouTube, and all that.
>> Yeah, YouTube, yes.
>> Yeah, MP3 is what a compression of a WAV file, and they're all from the same group. JPEG is joint picture expert group.

[00:05:05] MP3 is motion picture or that's MPEG but same motion picture expert group, again that we compressed down to MP3. Yeah, these are just compression algorithms, really, really powerful things.