Detailed explanation of Nginx access restriction configuration

Detailed explanation of Nginx access restriction configuration

What is Nginx access restriction configuration

Nginx access restrictions can be based on two aspects: one is IP-based access control, and the other is user-based trusted login control.

Below we will introduce these two methods one by one

Introduction to IP-based access control:

You can configure IP-based access control to allow certain IPs to access and restrict which IPs cannot access

This is the configuration method that allows access

Configuration syntax: allow address | CIDR | unix | all;

Default configuration: No configuration

Configuration path: http, server, location, limit_except;

This is a configuration that does not allow access

Method configuration syntax: deny address | CIDR | unix | all;

Default configuration: No configuration

Configuration path: http, server, location, limit_except;

Testing based on IP access restrictions

1. Check the local IP address. If it is a public network, go to ip138 network to check. If it is a test, use cmd to check.

2. Add an admin.html file in the /opt/app/code/ directory, which contains a normal admin page with a background color

3. In the /etc/nginx/conf.d/ directory, modify the default.conf file and add the following content


As can be seen from the figure above, a location is added to match admin.html, and the configuration based on IP restriction is set in it, restricting 192.xx.xx.xx from accessing it, while others can access it.

4. Reload nginx

5. Enter the URL in the browser and check the log



6. From the above picture, we can see that the access restriction of a certain IP has been realized. If you only allow a certain IP to access, you only need to change the keywords.

Example:

1. Server global IP limit

#vi nginx.conf
  allow 10.115.0.116; #Allowed IP
  deny all;

2. Site Limitation IP

#vi vhosts.conf
Site global limited IP:
location / {
  index index.html index.htm index.php;
  allow 10.115.0.116;
  deny all;

Site Directory Restrictions

location ^~ /test/ {
  allow 10.115.0.116;
  deny all;

Access control based on logged-in user trust

For example, when we access Apache information, a user password box prompt pops up to perform a pre-access verification.

Configuration syntax: auth_basic string | off;

Default configuration: auth_basic off;

Configuration path: http, server, location, limit_except;

Matches configuration syntax: auth_basic_user_file filePath;

Matches default configuration: No configuration

Match configuration paths: http, server, location, limit_except;

1. You need to add an identity file, auth_conf file, here we use an htpasswd tool

Use the command htpasswd -c ./auth_conf root;

Explanation: htpasswd command -c: MD5 encryption is used by default, ./auth_conf is the specified path and file, root is the username

After entering, you will be prompted to enter your password twice.


2. Modify the default.conf configuration file and modify the content as follows


3. Reload nginx

4. Enter the URL and check the results. You can see that you need to enter your identity information to access it.


The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Implementation of Nginx configuration of multi-port and multi-domain name access
  • Solve the problem of modifying configuration access of tp5 under nginx
  • How to configure nginx to limit the access frequency of the same IP
  • Steps to configure nginx ssl to implement https access (suitable for novices)
  • How to configure Nginx to distinguish between PC or mobile phone access to different domain names
  • Detailed explanation of nginx using ssl module configuration to support HTTPS access
  • How to use nginx to configure access to wgcloud

<<:  Summary of 3 ways to lazy load vue-router

>>:  MySQL NULL data conversion method (must read)

Recommend

Install and use Git and GitHub on Ubuntu Linux

Introduction to Git Git is an open source version...

Detailed explanation of the use of base tag in HTML

In requireJS, there is a property called baseURL....

Analysis and solution of the problem that MySQL instance cannot be started

Table of contents Preface Scenario Analysis Summa...

React+ts realizes secondary linkage effect

This article shares the specific code of React+ts...

Detailed explanation of Linux DMA interface knowledge points

1. Two types of DMA mapping 1.1. Consistent DMA m...

MySQL quick recovery solution based on time point

The reason for writing such an article is that on...

How to optimize MySQL indexes

1. How MySQL uses indexes Indexes are used to qui...

Native JS realizes the special effect of spreading love by mouse sliding

This article shares with you a js special effect ...

Raspberry Pi msmtp and mutt installation and configuration tutorial

1. Install mutt sudo apt-get install mutt 2. Inst...

Summary of @ usage in CSS (with examples and explanations)

An at-rule is a declaration that provides instruc...

The use and difference between JavaScript pseudo-array and array

Pseudo-arrays and arrays In JavaScript, except fo...

Three common ways to embed CSS in HTML documents

The following three methods are commonly used to d...

How complicated is the priority of CSS styles?

Last night, I was looking at an interview question...