How To Set Up HTTP Authentication With Nginx On Ubuntu 12.10
来源:互联网 发布:淘宝上能买药吗 编辑:程序博客网 时间:2024/05/17 23:32
What the Red Means
The lines that the user needs to enter or customize will be in red in this tutorial! The rest should mostly be copy-and-pastable.
About Nginx
Nginx (pronounced as 'engine x') is an HTTP and reverse proxy server, as well as a mail proxy server, written by Igor Sysoe that is flexible and lightweight program when compared to apache. The official nginx documentation is here.
Prerequisites
As a prerequisite, we are assuming that you have gone through the article on how to set up your VPS and also have installed Nginx on it. If not, you can find the article on setting up the VPS in the initial server setup article and you can find more information on installing nginx in our community.
Step 1: Apache Utils
We need htpasswd to create and generate an encrypted for the user using Basic Authentication. Install apache2-utils using the command below.
sudo apt-get install apache2-utils
Step 2: Create User and Password
Create a .htpasswd file under your website directory being served by nginx. The following command would create the file and also add the user and an encrypted password to it.
sudo htpasswd -c /etc/nginx/.htpasswd exampleuser
The tool will prompt you for a password.
New password:Re-type new password:Adding password for user exampleuser
The structure of the htpasswd file would be like this:
login:password
Note that this htpasswd should be accessible by the user-account that is running Nginx.
Step 3: Update Nginx configuration
Your nginx configuration file for the website should be under /etc/nginx/sites-available/. Add the two entries below under for the domain path that you want to secure.
auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd;
The second line is the location of the htpasswd file for your website.
For example, lets say our nginx configuration file is /etc/nginx/sites-available/website_nginx.conf, open the file using vi or any editor of your choice.
sudo vi /etc/nginx/sites-available/website_nginx.conf
Then add the two lines into the following path:
server { listen portnumber; server_name ip_address; location / { root /var/www/mywebsite.com; index index.html index.htm; auth_basic "Restricted"; #For Basic Auth auth_basic_user_file /etc/nginx/.htpasswd; #For Basic Auth }}
Step 4: Reload Nginx
To reflect the changes on our website reload the nginx configuration and try to access the domain that has been secured using Basic Authentication.
$ sudo /etc/init.d/nginx reload* Reloading nginx configuration...
Now try to access your website or the domain path that you have secured and you will notice a browser prompt that asks you to enter the login and password. Enter the details that you used while creating the .htpasswd file. The prompt does not allow you to access the website till you enter the right credentials.
And voila! You have your website domain path secured using Nginx's Basic Authentication.
- How To Set Up HTTP Authentication With Nginx On Ubuntu 12.10
- How to set up Eclipse with Android SDK on Ubuntu linux 9.04/9.10 (updated)
- How to Set Up an IKEv2 VPN Server with StrongSwan on Ubuntu 16.04
- How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 14.04 LTS
- How to set up tomcat with server authentication and client certificate authentication enabled
- How To Quickly Set Up Ubuntu 8.04 loaded with Erlang, Mochiweb and Nginx
- How To Set Up Database Replication In MySQL On Ubuntu
- How to set up IceScrum on Ubuntu 12.04
- How to Set Up vsftpd on Ubuntu 12.04
- How To Set Up an OpenVPN Server on Ubuntu 14.04
- How To Set Up vsftpd on Ubuntu 12.04
- How To Set Up an OpenVPN Server on Ubuntu 14.04
- How-To: Set up SVN with Apache2 in Linux (Ubuntu)
- How to Install HTTP Git Server With Nginx on Ubuntu 16.04
- How To Install Wordpress with nginx on Ubuntu 12.04
- How To Install WordPress with nginx on Ubuntu 14.04
- How to set up Eclipse with the gcc C++ compiler and GoogleTest on Windows7, from scratch
- Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 16.04和记录
- 常用的正则表达式
- ios--tableview加入购物车飞入效果
- iOS开发多线程篇—多线程简单介绍
- Android数据加载及Json解析——原始版
- IOS开发 企业版服务器下载ipa
- How To Set Up HTTP Authentication With Nginx On Ubuntu 12.10
- Phoenix 4.3.0 与 Hbase0.98.10-hadoop2的整合,hbase0.98
- Python之路——Python3 入门教程
- linux jdk降版本
- 个推合作案例
- SNMP协议介绍和操作截图
- 如何在Windows下启动Oracle数据库服务
- Spring MVC 框架搭建和详解
- 浙江大华2012笔试题+答案解析