• Home
  • About Us
  • Contact
  • Privacy Policy

LinuxAdmin.io

  • Home
  • Tutorials
    • System Administration
    • Linux Tutorials
    • Web Hosting
    • WordPress
    • Virtualization
    • Troubleshooting
    • OpenStack

Nginx Directory Protection

Nginx Directory Protection

There are a few different ways to protect directories with Nginx. Password protection is typically to keep certain users out or only let certain users in. This is useful in protecting certain content on websites you do not want everyone to be able to access. This guide assumes you already have a running Nginx installation, if you do not, please see How to Install Nginx

Nginx Directory Password Authentication

Nginx can use the same user password protection as Apache using a text file with hashed passwords associated with user names.

If you do not already you will need to install the htpasswd utility

yum install -y httpd-tools

You would then create the appropriate htpasswd file

htpasswd -C /etc/nginx/.htpasswd username

Replacing username with the username you would like to use. It will prompt you to enter the password twice then write out the hashed password to the file you specified. You would then add a location block to protect your directory in the nginx configuration

nano /etc/nginx/nginx.conf

And add the following location in the server {} configuration:

 location /secret {
 auth_basic "Administrator Login";
 auth_basic_user_file /etc/nginx/.htpasswd;
 }

You would then just need to restart Nginx

service nginx restart

And the directory would now be protected. Go ahead and visit the directory you configured and you should be prompted with  password prompt. If you were to enter the wrong password you would be given a 401 (Authorization required) response for not entering valid credentials.

Nginx Directory IP Based protection

Another method is to allow only certain IPs or inversely block certain IP addresses. To allow only certain IP address you will want to edit your sites nginx configuration and add the following:

location ^~ /secret {
 allow 192.168.1.10; 
 deny all; 
}

Replacing 192.168.1.10 with the IP you would like to allow access to the directory.  To deny certain IP addresses you would need to do the following:

location ^~ /secret { 
 deny 192.168.1.10; 
 allow all; 
}

Either of these would then just need restart Nginx to load in the new configuration.

Jun 6, 2017LinuxAdmin.io
0 0 vote
Article Rating
Piwik Analytics on NginxNginx Browser Caching
You Might Also Like
 
Virsh KVM Commands For Management
 
Creating A New Virtual Machine with KVM
Subscribe
Notify of
guest
guest
0 Comments
Inline Feedbacks
View all comments
5 years ago Linux System Administration, Nginx, Security, Troubleshooting, Web Hosting 488
Recent Posts
  • Laravel Installation Guide For CentOS
  • Openstack Services Explanation And Overview
  • OSSEC Intrusion Detection Installation On Centos 7
  • Configure ProFTPd for SFTP on CentOS
  • How To Check And Repair MyISAM Tables In MySQL
Most Commented
Hot Clone Linux Server with Rsync
Hot Clone A CentOS Server With Rsync
14 Comments
ngx_cache_purge module
Install The ngx_cache_purge Module In Nginx
8 Comments
piwik-nginx
Piwik Analytics on Nginx
8 Comments
Tags
linuxcentosLinux Performance Tuningsysadminkvmnetworkingmemcachedanalyticssystemd
About

We love Linux and are dedicated to creating Linux administration tutorials for System Administrators since 2016.

Most Viewed
Default Gateway
How To Configure A Default Gateway on CentOS
63,004 views
Zend Opcache
Setup and Optimize Zend OpCache
46,714 views
Install ffmpeg
FFMpeg Install On CentOS 7
20,912 views
Archives
Email subscription

Sign up for our newsletter to receive the latest news and event postings.

2018 © LinuxAdmin.io
wpDiscuz