1. URL rewrite (Rewrite)
1. What is URL rewriting
URL
Rewrite refers to the requested userURL
Convert to anotherURL
The process. This is very useful in many scenarios, such as:
- Put the old one
URL
Redirect to newURL
。 - Beautify
URL
, make it more friendly. - Implement dynamics
URL
To staticURL
Conversion.
2. Rewrite command
Nginx
Providedrewrite
Instructions to implementURL
Rewrite. Its basic syntax is as follows:
rewrite regex replacement [flag];
-
regex
: Regular expression, used to match the requested URL. -
replacement
: ReplacedURL
。 -
flag
: Optional parameter to control the behavior of rewrites.
3. Example, URL replacement
Suppose we have an old oneURL /
, now want to redirect it to the new oneURL /
, can be found inNginx
Add the following configuration to the configuration file:
server { listen 80; server_name ; location / { rewrite ^/$ / permanent; } }
-
^/$
: Match/
。 -
/
: Replace with newURL
。 -
permanent
: Return 301 permanent redirect status code.
4. Example, beautify URL
Suppose we have a dynamicURL /?id=123
, now I want to beautify it into/product/123
, can be configured like this:
server { listen 80; server_name ; location /product { rewrite ^/product/(\d+)$ /?id=$1 last; } }
-
^/product/(\d+)$
: Match/product/123
SuchURL
, and capture 123 as$1
。 -
/?id=$1
: Will be captured$1
Passed as a parameter to。
-
last
: Stop processing the current rewrite rule and continue to match otherlocation
piece.
2. IP access control
1. What is IP access control
IP
Access control refers to the client-basedIP address
to restrict or allow access to certain resources. This is very useful in many scenarios, such as:
- Limit some
IP
Access sensitive resources. - Allow specific
IP
Access the management backend.
2. IP access control command
Nginx
Providedallow
anddeny
Instructions to implementIP
Access control. Their basic syntax is as follows:
allow IPAddress or network segment; deny IPAddress or network segment;
3. Example: Restricting specific IP access
Suppose we want to limit onlyIP location
Address:192.168.1.100
The client can access/admin
Directory, otherIP address
All access is denied, so you can configure it like this
server { listen 80; server_name ; location /admin { allow 192.168.1.100; deny all; } }
-
allow 192.168.1.100
:allowIP address
for192.168.1.100
client access. -
deny all
: Reject all otherIP address
visit.
4. Example: Allow access to specific network segments
Suppose we want to allowIP network segment
for192.168.1.0/24
Client access/internal directory
,otherIP address
Access is denied, so you can configure it like this:
server { listen 80; server_name ; location /internal { allow 192.168.1.0/24; deny all; } }
-
allow 192.168.1.0/24
:allowIP network segment
for192.168.1.0/24
client access. -
deny all
: Reject all otherIP address
visit.
3. Basic Authentication
1. What is basic certification
Basic authentication is a simple authentication mechanism that requires users to provide a username and password to access protected resources. Although the security of basic authentication is not as good as other advanced authentication mechanisms (such asOAuth
), but it is still very useful in some scenarios.
2. Basic authentication in Nginx
Nginx
Providedauth_basic
andauth_basic_user_file
Instructions to implement basic authentication. Their basic syntax is as follows:
auth_basic "Prompt message"; auth_basic_user_file Password file path;
3. Example: Protect Directory
Suppose we want to protect/secret directory
, the user is required to enter a username and password to access. You can configure it like this:
server { listen 80; server_name ; location /secret { auth_basic "Restricted Area"; auth_basic_user_file /etc/nginx/.htpasswd; } }
-
auth_basic "Restricted Area"
: Set the prompt message to “Restricted Area
”。 -
auth_basic_user_file /etc/nginx/.htpasswd
: Specify the password file path as/etc/nginx/.htpasswd
。
How to create .htpasswd file:
Nginx
use.htpasswd file
To store username and password. Can be usedhtpasswd
Tools to create and update this file.
htpasswd -c /etc/nginx/.htpasswd username
-
-c
: Create a new password file (overrides if the file already exists). -
/etc/nginx/.htpasswd
: Password file path. -
username
:username.
After executing the command, the system will prompt you to enter your password. After entering the password,.htpasswd file
Will be generated.
Add multiple users:
If you want to add multiple users, you can usehtpasswd
Command-b
Options:
htpasswd -b /etc/nginx/.htpasswd user1 password1 htpasswd -b /etc/nginx/.htpasswd user2 password2
-
-b
: Specify the password directly in the command line.
4. Summary
You've mastered itNginx
Three important functions in:URL rewrite
、IP Access Control
and basic certification. These features can help you better manage and protect yourWeb Services
. Hope these examples and explanations can help you better understand and use themNginx
。
The above is the detailed explanation of Nginx's URL rewriting and IP access control. For more information about Nginx's URL rewriting and IP access control, please follow my other related articles!