The final effect is similar (the interfaces of different browsers are different):
If the authentication fails, an http error will be reported: 401 authorization required.
To implement such a function, you need to change the server configuration and set the user name and password for login.
First we need to change the nginx server configuration of the website. For ubuntu server, this configuration file is usually located at /etc/nginx/sites-enabled/. For example, I use the default configuration file /etc/nginx/sites-enabled/ here. Default is an example:
Copy the code The code is as follows:
server {
server_name www.fancycedar.info
root /www/fancycedar
# ...
location / {
# Add the following two lines
auth_basic "restricted";
auth_basic_user_file htpasswd;
# ...
}
# ...
}
Next, you need to create the htpasswd file. Here are some details to pay attention to:
The path of htpasswd
is at the same level as nginx.conf Directory is enough. For Ubuntu servers, it is usually under /etc/nginx/.
The content of htpasswd
Each line is for one user, and the format is username:password. But please note that the password here is not plain text, but a string that is encrypted by crypt(3).
You can use a piece of php code to generate the password in htpasswd:
Copy the code The code is as follows:
//Password plain text
$password = 'some password' ;
// Encrypt the password
$password = crypt($password, base64_encode($password));
// Obtain the encrypted password
echo $password;
Then write the string into the htpasswd file:
Copy the code The code is as follows:
username1:xucqmk13tfooe
username2:yxtfb3xwkombm
...
htpasswd permissions
You need to change the permissions of the htpasswd file, execute the following command:
Copy the code The code is as follows:
sudo chown root:www-data htpasswd
sudo chmod 640 htpasswd
are you ready?
When the above preparations are done, we can reload or restart the nginx server:
Copy code The code is as follows:
sudo /etc/init.d/nginx reload
# or
sudo /etc/init.d/nginx restart
The above is the detailed content of How to use htpasswd to password protect the website in Nginx. For more information, please follow other related articles on the PHP Chinese website!

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.

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINXUnit improves application flexibility and performance with its dynamic configuration and high-performance architecture. 1. Dynamic configuration allows the application configuration to be adjusted without restarting the server. 2. High performance is reflected in event-driven and non-blocking architectures and multi-process models, and can efficiently handle concurrent connections and utilize multi-core CPUs.

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINXUnit supports multiple programming languages and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version