Building data-driven web apps with Flask and SQLAlchemy Transcripts
Lecture: Running in nginx
0:00 The final thing we have to do
0:01 to get our server up and running
0:03 is we have to configure Nginx.
0:05 When you talk to the server
0:07 when you make a request to it in your browser
0:08 or anyone does
0:10 what you're actually talking to is Nginx.
0:12 It does all the static file serving, the SSL
0:15 translation, all that kind of stuff.
0:16 And then internally it will make a request
0:19 over to uWSGI, get the result
0:21 and stream that back to you.
0:23 So as far as the public is concerned
0:26 Nginx is your web server.
0:27 They don't know Mike Rowinski is doing the magic
0:29 behind the scenes.
0:30 So we still have to set that up.
0:31 Now let's just figure out what happens
0:33 if we request a page over there.
0:35 If I put in the URL here, we get "Welcome to Nginx".
0:37 You see, it's already running, so that's cool
0:40 but it's not running our code.
0:41 So let's make it run our code.
0:43 That brings us back to our little script here.
0:45 We've already got Nginx installed. That's cool.
0:47 But what we need to do is remove that default page
0:49 that says, "Hey, Welcome to Nginx. Good job setting it up".
0:53 So we are going to remove that
0:55 and then the next thing we need to do
0:56 is copy our Nginx configuration
0:58 over to sites enabled.
1:01 Now let's just quickly scan over this.
1:03 Since we are listening on port 80
1:05 we are going to listen for fake_pypi.com
1:09 I'm going to change that actually in a minute
1:10 but we could just hack that into our host
1:12 or what you would really do is put your site
1:15 like training.talkpython.fm by
1:17 or www.google.com
1:19 or whatever your domain is, you put it here.
1:21 And this allows you to have many different websites
1:24 served out of the same Nginx.
1:27 So I have one server
1:28 that has six or seven different Nginx files
1:30 and configurations for different domains.
1:32 And this is how you specify that.
1:34 Don't pass back the version of the server.
1:37 Let's see, we have our static files
1:39 and those are not processed by Python anymore.
1:42 They are now exclusively handled by Nginx.
1:45 So we say when somebody's asked for something /static
1:48 you'll look over here. All right.
1:49 And those are cached for a year. That's good.
1:52 And then it says otherwise go to your application
1:55 and your application, does this local call back
1:58 over to 5000.
2:00 I like to use http. You can also use Unix sockets
2:03 which is a little better performance
2:05 but not a huge amount I don't believe.
2:07 This way, I can actually test if anything is going wrong
2:10 I can test my code.
2:11 With the sockets, it's not so much.
2:13 So I'm going to leave it like this.
2:15 And we're just going to copy this file over
2:17 to where this needs to be configured
2:19 which is this line right there.
2:22 That worked. Good.
2:24 And then we're going to enable Nginx.
2:26 I believe it's already enabled, but just to be sure.
2:29 And we're going to restart it
2:30 so it detects the configuration change.
2:33 All right, super.
2:34 Let's see what happens when I go over here.
2:36 Refresh. Moment of truth. Ta-dah!
2:40 How awesome is that? Cool. Our site is online.
2:43 It has a couple of problems, which we're going to fix.
2:45 Problem number 1: it's not SSL.
2:48 Connect is not secure.
2:49 These days you really kind of want
2:51 to have your connection over https.
2:53 It helps with search engine optimization
2:55 even if you don't care about being truly secure.
2:58 The other one is look here how many
3:01 projects, releases, etc. We have no data.
3:03 So, remember we had that script if you remember
3:05 that far back?
3:08 If we go into pypi.org and we go into bin
3:12 remember here we had this load data?
3:15 So we just say Python load data.
3:17 This will go and actually import all that data
3:20 build it up.
3:22 If we come back here one more time and do a refresh...
3:24 Ta-dah! There's our projects
3:26 and now these show up down here as well.
3:28 Perfect, right?