search
HomeOperation and MaintenanceNginxNginx access control configuration to restrict access to specified users

Nginx access control configuration, restricting access to specified users

In the web server, access control is an important security measure used to limit the access rights of specific users or IP addresses. As a high-performance web server, Nginx also provides powerful access control functions. This article will introduce how to use Nginx configuration to limit the access permissions of specified users, and provide code examples for reference.

First, we need to prepare a basic Nginx configuration file. Assume that we already have a website and the configuration file path is /etc/nginx/nginx.conf. In this configuration file, we will add the following access control configuration:

http {
  # 其他配置内容...
  
  # 定义一个验证文件,包含允许访问的用户名及密码
  auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
  
  # 定义一个location块,对指定URL路径进行访问控制
  location /private {
    # 开启基于HTTP基本认证的访问控制
    auth_basic "Restricted";
    
    # 指定只对特定用户名进行访问控制
    auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
    
    # 其他配置内容...
  }
}

In the above configuration, we used the auth_basic_user_file directive to define an authentication file that contains the users allowed access name and corresponding password. The path of this verification file is /etc/nginx/conf.d/.htpasswd, we can change it according to actual needs.

Next, we use the location block to perform access control on the specified URL path. In the example, we use /private as the path with restricted access. You can adjust it according to the actual situation. In the location block, we use the auth_basic directive to enable access control based on HTTP basic authentication.

In order to restrict access to only specific users, we use the auth_basic_user_file directive again and specify the path to the verification file. This way, only usernames present in the verification file can access restricted URL paths.

Next, we need to prepare the verification file .htpasswd. This file can be generated using the htpasswd command, which is a tool provided by Apache HTTP Server. Execute the following command in the terminal to generate the verification file:

htpasswd -c /etc/nginx/conf.d/.htpasswd user1

The above command will generate a .htpasswd file under the specified path and set the password for user user1. In order to add more users, you can remove the -c option, as shown below:

htpasswd /etc/nginx/conf.d/.htpasswd user2

After this, you can continue to set passwords for more users as needed.

Finally, we need to restart the Nginx server to make the configuration take effect. Execute the following command in the terminal:

sudo service nginx restart

Now, only users present in the verification file can access the restricted URL path. Other users will not be able to pass authorization, thus increasing the security of the website.

Summary:

This article introduces how to use Nginx configuration to limit the access permissions of specified users. First, we defined the path to the verification file in the Nginx configuration file and enabled access control based on HTTP basic authentication. Then, the URL path to which access is restricted is specified through the location block, and the path to the verification file is specified again to restrict access to only specific users. Finally, we used the htpasswd command to generate the verification file and restarted the Nginx server to make the configuration take effect.

I hope this article will help you understand Nginx access control configuration and learn to restrict the access permissions of specified users. If you have other questions, you can refer to Nginx official documentation or conduct further consultation and research.

The above is the detailed content of Nginx access control configuration to restrict access to specified users. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
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.

Choosing Between NGINX and Apache: The Right Fit for Your NeedsChoosing Between NGINX and Apache: The Right Fit for Your NeedsApr 15, 2025 am 12:04 AM

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

How to start nginxHow to start nginxApr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

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

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

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