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

Summary of JavaScript Timer Types

Table of contents 1.setInterval() 2.setTimeout() ...

MySql 8.0.11-Winxp64 (free installation version) configuration tutorial

1. Unzip the zip package to the installation dire...

Summary of some of my frequently used Linux commands

I worked in operations and maintenance for two ye...

Analysis and solutions to problems encountered in the use of label tags

I used the label tag when I was doing something re...

jenkins+gitlab+nginx deployment of front-end application

Table of contents Related dependency installation...

WeChat applet realizes taking photos and selecting pictures from albums

This article shares the specific code for WeChat ...

CSS3 speeds up and delays transitions

1. Use the speed control function to control the ...

Detailed explanation of several examples of insert and batch statements in MySQL

Table of contents Preface 1.insert ignore into 2....

Detailed explanation of the use of filter properties in CSS

The filter attribute defines the visual effect of...

MySQL 8.0.12 decompression version installation tutorial personal test!

Mysql8.0.12 decompression version installation me...

CSS solution for centering elements with variable width and height

1. Horizontal center Public code: html: <div c...