Blocking Access in Nginx0:00
This episode's question comes from Mohamed through email, and he asks how can I restrict my site, or a subsection of my site, from the outside world? So, basically what he wants is, maybe he wants to push the site to production, but he's not ready to make it public. Okay, lock it down using basic HTTP auth. Now, in this case, you can see, for the lesson, I've created a subdomain specifically for Laracast: demo.laracast.com, and this is live right now. So, let's say I want to restrict it from the outside world, people like you. Okay, well, I will ssh into my server, and I will edit my /etc/nginx/sites-available/demo.laracast.com. So, if we want to be quick, we could just deny it from everyone, deny all, and put that in my server block. So, now if I escape out, and then I say sudo service nginx reload, that will reload the config file. If I now switch back to Chrome, and I give that a refresh, I can't access that, and nobody can.
Allowlisting Specific IPs0:51
So, now if I escape out, and then I say sudo service nginx reload, that will reload the config file. If I now switch back to Chrome, and I give that a refresh, I can't access that, and nobody can. So, let's go back in, and then say I do want to allow my IP address, and I'll paste that in. Okay, one more time, refresh nginx, back to Chrome, and now I can access it, but no other IP address in the world is allowed. That's a good option. But let's say you don't want to keep adding IP addresses for each person on your team. That can get kind of annoying. Okay, another alternative is HTTP basic authentication, like this. Let's get back in there. We're going to remove these two sections, and we want basic auth.
Enabling Basic Auth1:35
Let's get back in there. We're going to remove these two sections, and we want basic auth. Now, the title that will display in the little modal will be, I don't know, admin only. Next, we have to present a user file, which is just a flat credentials file, very simple stuff. Auth basic user file, and we'll put that within the /etc/nginx/.htpasswd. How about that? And we'll create that in just a minute. Okay, so now we're saying we do want basic authentication. The title will be admin only, and then the credentials file is located at this path. So let's create that file now.
Creating htpasswd File2:07
The title will be admin only, and then the credentials file is located at this path. So let's create that file now. Now, it's possible that you have this tool already installed on your system, but possibly not. If that's the case, just do this, sudo apt-get install apache2-utils, and that's all you need to do. That'll take care of it. Okay, so now this is a little utility for generating usernames and encrypting passwords. So we'll say htpasswd, and we're going to create a new file within that exact path that we specified in the config file. Now, while I'm doing this, though, I'm going to add a username. We'll just set it to admin. Now, the password will just be pass, and we're all set to go.
Adding Users and Testing2:47
We'll just set it to admin. Now, the password will just be pass, and we're all set to go. So now, if I cat that, you'll see this is very, very simple stuff. The username, colon, and then the encrypted password. So, for example, if I want to add another one, I could say same path, and this will be myself. And the password, once again, will be pass. Okay, now if we cat it, you'll see a new line there, which means only these two usernames are able to log in. So now the only remaining step is refresh the nginx file, because we changed it a couple minutes ago. And we're all set. So let's try this out again.
