How to use Nginx to implement HTTP basic authentication
Nginx is a high-performance, reliable web server and reverse proxy server. In addition to its powerful load balancing and caching capabilities, Nginx also supports HTTP basic authentication. HTTP basic authentication is a simple authentication method that verifies the user's identity by adding a username and password in the request header. In this article, we will learn how to implement HTTP basic authentication using Nginx.
1. Set username and password
First, we need to create a password file. We can use the htpasswd command to create this file. htpasswd is a command line tool for managing HTTP basic authentication passwords.
We can use the following command in the terminal to create a password file:
htpasswd -c /etc/nginx/.htpasswd username
Among them, -c means to create a new password file, /etc/nginx/.htpasswd is the path to the password file , username is the username we want to add. After running this command, you will be prompted for your password. After entering the password, htpasswd will hash the password and store it in the password file.
If you want to add additional users to the password file, use the following command:
htpasswd /etc/nginx/.htpasswd username2
This command does not have the -c option because our password file already exists. After entering the command, you will be prompted for your password. Once entered, htpasswd hashes and adds the username and password to a password file.
2. Apply basic authentication in Nginx
Now, we have created a password file containing username and password hashes. Next, we need to apply HTTP basic authentication in Nginx.
We need to add the following directives in the server block or location block:
auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd;
The first directive tells Nginx to perform basic authentication on the request and display "Restricted" in the pop-up login box as Description.
The second directive tells Nginx to use the /usr/share/nginx/.htpasswd file to authenticate the user.
For example, we can add the following code in Nginx's default server block:
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd; }
Now, we have successfully added basic authentication to our Nginx server.
3. Test HTTP basic authentication
Now let’s test the authentication function we just set up. We can use curl or Firefox and other tools to test.
Test using curl:
curl -I http://localhost
The command line will prompt you for your username and password. After entering the correct username and password, you can see the following code included in the HTTP response header:
HTTP/1.1 200 OK Server: nginx/1.14.0 (Ubuntu) ...
Test with Firefox:
Enter the server address http://localhost in Firefox. Firefox will pop up a window with the word "Restricted" and a description, prompting you to enter your username and password. After entering the correct username and password, you will be redirected to your server default web page.
Summary
This article introduces how to use Nginx to implement HTTP basic authentication. HTTP Basic Authentication is a simple but effective authentication method that can be used to protect sensitive resources of a web server. We can easily add HTTP Basic Authentication to our Nginx server by first creating a password file using the htpasswd command and then applying the Basic Authentication directive in the Nginx configuration.
The above is the detailed content of How to use Nginx to implement HTTP basic authentication. For more information, please follow other related articles on the PHP Chinese website!

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

NGINXUnit is an open source application server that supports multiple programming languages and provides functions such as dynamic configuration, zero downtime updates and built-in load balancing. 1. Dynamic configuration: You can modify the configuration without restarting. 2. Multilingual support: compatible with Python, Go, Java, PHP, etc. 3. Zero downtime update: Supports application updates that do not interrupt services. 4. Built-in load balancing: Requests can be distributed to multiple application instances.

NGINXUnit is better than ApacheTomcat, Gunicorn and Node.js built-in HTTP servers, suitable for multilingual projects and dynamic configuration requirements. 1) Supports multiple programming languages, 2) Provides dynamic configuration reloading, 3) Built-in load balancing function, suitable for projects that require high scalability and reliability.

NGINXUnit improves application performance and manageability with its modular architecture and dynamic reconfiguration capabilities. 1) Modular design includes master processes, routers and application processes, supporting efficient management and expansion. 2) Dynamic reconfiguration allows seamless update of configuration at runtime, suitable for CI/CD environments. 3) Multilingual support is implemented through dynamic loading of language runtime, improving development flexibility. 4) High performance is achieved through event-driven models and asynchronous I/O, and remains efficient even under high concurrency. 5) Security is improved by isolating application processes and reducing the mutual influence between applications.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
