Home Operation and Maintenance Nginx Nginx access control configuration to restrict access to specified users

Nginx access control configuration to restrict access to specified users

Jul 04, 2023 am 10:37 AM
Access control nginx configuration User restrictions

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;
    
    # 其他配置内容...
  }
}
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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 of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Vue for permission management and access control How to use Vue for permission management and access control Aug 02, 2023 pm 09:01 PM

How to use Vue for permission management and access control In modern web applications, permission management and access control is a critical feature. As a popular JavaScript framework, Vue provides a simple and flexible way to implement permission management and access control. This article will introduce how to use Vue to implement basic permission management and access control functions, and attach code examples. Defining Roles and Permissions Before you begin, you first need to define the roles and permissions in your application. A role is a specific set of permissions, and

Nginx error page configuration, beautify website failure prompts Nginx error page configuration, beautify website failure prompts Jul 04, 2023 pm 01:33 PM

Nginx error page configuration, beautify website fault prompts. During the operation of the website, it is inevitable to encounter server errors or other faults. These problems will cause users to be unable to access the website normally. In order to improve user experience and website image, we can configure Nginx error pages to beautify website failure prompts. This article will introduce how to customize the error page through Nginx's error page configuration function, and provide code examples as a reference. 1. Modify the Nginx configuration file. First, we need to open the Nginx configuration.

How to implement Nginx cross-origin resource sharing (CORS) configuration How to implement Nginx cross-origin resource sharing (CORS) configuration Nov 08, 2023 pm 12:22 PM

How to implement Nginx's cross-domain resource sharing (CORS) configuration requires specific code examples. With the popularity of front-end and back-end separation development, cross-domain resource sharing (CORS) issues have become a common challenge. In web development, due to the browser's same-origin policy restrictions, client-side JavaScript code can only request resources with the same domain name, protocol, and port as the page where it is located. However, in actual development, we often need to request resources from different domain names or different subdomains. At this time, you need to use CO

Use Go language to solve large-scale access control problems Use Go language to solve large-scale access control problems Jun 15, 2023 pm 02:59 PM

With the development of the Internet, access control issues have increasingly become an important topic. In traditional permission management, role authorization or access control lists are generally used to control resources. However, this method is often unable to adapt to large-scale access control needs because it is difficult to flexibly implement access control for different roles and resources. To solve this problem, using Go language to solve large-scale access control problems has become an effective method. Go language is a language for concurrent programming. It has excellent concurrency performance and fast compilation.

How Nginx implements access control configuration based on request source IP How Nginx implements access control configuration based on request source IP Nov 08, 2023 am 10:09 AM

How Nginx implements access control configuration based on the request source IP requires specific code examples. In network application development, protecting the server from malicious attacks is a very important step. Using Nginx as a reverse proxy server, we can configure IP access control to restrict access to specific IP addresses to improve server security. This article will introduce how to implement access control configuration based on request source IP in Nginx and provide specific code examples. First, we need to edit the Nginx configuration file

Access Control Editor cannot be opened in Win10 Access Control Editor cannot be opened in Win10 Jan 03, 2024 pm 10:05 PM

The inability to open the access control editor in win10 is an uncommon problem. This problem usually occurs in external hard drives and USB flash drives. In fact, the solution is very simple. Just open it in safe mode and take a look. Let’s take a look at the details below. tutorial. Win10 cannot open the access control editor 1. In the login interface, hold down shift, click the button, click 2.--, click 3. After restarting, press F5 to try to enter and see if you can enter. Articles related to win10 safe mode>>>How to enter win10 safe mode<<<>>>How to repair the system in win10 safe mode<<<

An in-depth exploration of Nginx's traffic analysis and access control methods An in-depth exploration of Nginx's traffic analysis and access control methods Aug 05, 2023 pm 05:46 PM

An in-depth discussion of Nginx's traffic analysis and access control methods. Nginx is a high-performance open source web server. It is powerful and scalable, so it is widely used in the Internet field. In practical applications, we usually need to analyze Nginx traffic and control access. This article will delve into Nginx's traffic analysis and access control methods and provide corresponding code examples. 1. Nginx traffic analysis Nginx provides many built-in variables that can be used to analyze traffic. Among them, commonly used

How does PHP handle cross-domain requests and access control? How does PHP handle cross-domain requests and access control? Jun 30, 2023 pm 11:04 PM

How does PHP handle cross-domain requests and access control? Abstract: With the development of Internet applications, cross-domain requests and access control have become an important issue in PHP development. This article will introduce methods and techniques on how PHP handles cross-domain requests and access control, aiming to help developers better understand and deal with these issues. What is a cross-domain request? Cross-domain request means that in the browser, a web page in one domain requests to access resources in another domain. Cross-domain requests generally occur in AJAX requests, image/script/css references, etc. Depend on

See all articles