search
HomeOperation and MaintenanceNginxHow to configure Nginx user authentication page

Preface

Application scenario: Probably the internal website needs to be accessed by external users, and at the same time, visitors cannot be given website account permissions, so restrictions are imposed at the nginx level. For example, in outsourcing projects, internal employees have accounts to operate documents, while outsourced employees do not have internal accounts, but they need to be able to see the documents. Therefore, setting user authentication at the nginx level is the best and simplest option. In most cases, employers will not Open an account with basic access rights for outsourced employees.

Prerequisites for user authentication at the nginx level: a corresponding password creation program is required, such as apache2-utils (debian, ubuntu) or httpd-tools (rhel/centos/oracle linux), different operations Systems are different software.

Create account password file

  • Use command sudo htpasswd -c /etc/apache2/.htpasswd user1 Create the first account, then press the enter key to enter the password. The same command does not have the -c parameter to create the second user and password. The -c parameter is to create a file. There is no need to repeat it in the second and subsequent commands. Create a file.

  • Confirm that the file and account information are generated successfully. Use the command cat /etc/apache2/.htpasswd to view the file content. It should be the account number and encrypted password, such as: user1 :$apr1$/woc1jnp$kah0ssvn5qesmjttn0e9q0 etc.

Configure nginx for http basic user authentication

Use the auth_basic directive to specify the name of the protected area. This name will be displayed in On the account and password pop-up window, use the auth_basic_user_file command to set the .htpasswd path with account and password information. For example, configuration:

location /api {
 auth_basic   "administrator's area";
 auth_basic_user_file /etc/apache2/.htpasswd; 
}

In addition, if a block does not want to inherit the entire authentication system, you can set auth_basic off in the block, that is, user authentication is turned off. For example configuration:

server {
 ...
 auth_basic   "administrator's area";
 auth_basic_user_file conf/htpasswd;

 location /public/ {
  auth_basic off;
 }
}

Combining authentication with access restrictions by ip address

http Basic authentication can be effectively combined with access restrictions by ip address. You can implement at least two scenarios:

  • Users need to be authenticated and have ip access

  • Users need to be authenticated or have ip Access permissions

1. Use the allow and deny directives to allow or restrict access to the specified IP, for example, configure:

location /api {
 #... deny 192.168.1.2;
 allow 192.168.1.1/24;
 allow 127.0.0.1;
 deny all;
}

2, at the 192.168.1.2 address For other networks, only access to 192.168.1.1/24 is granted. Note: allow and deny directives will be applied in the order defined.

Combine restrictions with the satisfy directive via ip and http authentication. If the directive is set to all, access is granted when the client meets these two conditions. If the directive is set to any, access is granted if the client meets at least one condition, for example configuration:

location /api {
 #... satisfy all; 

 deny 192.168.1.2;
 allow 192.168.1.1/24;
 allow 127.0.0.1;
 deny all;

 auth_basic   "administrator's area";
 auth_basic_user_file conf/htpasswd;
}

The above can be organized into a complete example:

http {
 server {
  listen 192.168.1.23:8080;
  root /usr/share/nginx/html;

  location /api {
   api;
   satisfy all;

   deny 192.168.1.2;
   allow 192.168.1.1/24;
   allow 127.0.0.1;
   deny all;

   auth_basic   "administrator's area";
   auth_basic_user_file /etc/apache2/.htpasswd; 
  }
 }
}

The final effect is as shown:

How to configure Nginx user authentication page

The above is the detailed content of How to configure Nginx user authentication page. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX Unit: The Architecture and How It WorksNGINX Unit: The Architecture and How It WorksApr 23, 2025 am 12:18 AM

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.

Using NGINX Unit: Deploying and Managing ApplicationsUsing NGINX Unit: Deploying and Managing ApplicationsApr 22, 2025 am 12:06 AM

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 vs. Apache: A Comparative Analysis of Web ServersNGINX vs. Apache: A Comparative Analysis of Web ServersApr 21, 2025 am 12:08 AM

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.

NGINX Unit's Advantages: Flexibility and PerformanceNGINX Unit's Advantages: Flexibility and PerformanceApr 20, 2025 am 12:07 AM

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 vs. Apache: Performance, Scalability, and EfficiencyNGINX vs. Apache: Performance, Scalability, and EfficiencyApr 19, 2025 am 12:05 AM

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.

The Ultimate Showdown: NGINX vs. ApacheThe Ultimate Showdown: NGINX vs. ApacheApr 18, 2025 am 12:02 AM

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 in Action: Examples and Real-World ApplicationsNGINX in Action: Examples and Real-World ApplicationsApr 17, 2025 am 12:18 AM

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.

NGINX Unit: Supporting Different Programming LanguagesNGINX Unit: Supporting Different Programming LanguagesApr 16, 2025 am 12:15 AM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

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),