Even though my search ability is not low, I still struggle to quickly find many things.
.htaccess | nginx.conf | Description |
---|---|---|
%{REQUEST_SCHEME} | $scheme | http /https |
%{HTTP_HOST} | $host | domain / ip |
%{REQUEST_URI} | $request_uri | part after host/ |
%{REQUEST_FILENAME} | $request_filename | seems to be the same as uri |
%{QUERY_STRING} | $args | query string (part after ? ) |
%{HTTP_USER_AGENT} | $http_user_agent | client UA |
%{REMOTE_ADDR} | $remote_addr | visitor's IP |
$scheme://$host/
or%{REQUEST_SCHEME}://%{HTTP_HOST}/
can directly concatenate the current URL.
mod_rewrite Reference_Apache Chinese Documentation:
https://www.apachehttpd.com/mod/mod_rewrite.html
Htaccess - THE Ultimate .htaccess tutorial with 100's of Examples:
https://www.askapache.com/htaccess/#Htaccess_Variables
I. Regular expression matching, where:
~
is for case-sensitive matching;~*
is for case-insensitive matching;!~
and!~*
are for case-sensitive and case-insensitive non-matching, respectively;
II. File and directory matching, where:
-f
and!-f
are used to check if a file exists;-d
and!-d
are used to check if a directory exists;-e
and!-e
are used to check if a file or directory exists;-x
and!-x
are used to check if a file is executable;
III. The last parameter of the rewrite directive is the flag, which can be:
last
initiates a new request for the rewritten URI, the final effect depends on whether the new request can match another rule; "similar to[L]
flag in Apache"break
the current directive specifies the final URI;redirect
returns a 302 temporary redirect, the browser address will display the redirected URL;permanent
returns a 301 permanent redirect, the browser address will display the redirected URL;
In 2022, I just learned that you can't directly use "and" or "or" operations in Nginx configuration?
# Used to view the visiting IP, for testing only
location ~* ^/ip/[\d-]+ {
# default_type cannot be used inside if
default_type text/plain;
return 200 "${remote_addr}";
}
# Match a certain path
# if ($uri ~* ^/SomePath) {
# ………………
# }
# -----------------
# Default is 0
set $pass 0;
# Set $pass to 1 if any condition is met;
# equivalent to "or" operation;
# Check UA
if ($http_user_agent ~* Edg/\d+) {
set $pass 1;
}
# Check IP
if ($remote_addr ~* 172.*) {
set $pass 1;
}
# Deny access if not equal to 1
if ($pass != 1) {
return 403;
}
# -----------------
set $flag 0;
# All conditions must be met;
# equivalent to "and" operation;
# Check UA
if ($http_user_agent ~* Edg/\d+) {
set $flag "${flag}1";
}
# Check IP
if ($remote_addr ~* 172.*) {
set $flag "${flag}1";
}
# Deny access if the result does not match
if ($flag != "011") {
return 403;
}
Nginx implementation to only allow search engines or specified IP addresses to access:
# Frozen Ice and Floating Water (wdssmq)
location / {
set $pass 0;
# Check UA
if ($http_user_agent ~* (Baiduspider|YisouSpider|360Spider|HaosouSpider|bingbot|Sosospider|Sosoimagespider|Bytespider|Sogou web spider|Sogou inst spider|Sogou News Spider|Sogou Pic Spider|Sogou Video Spider|Googlebot|Googlebot-Image|AdsBot-Google-Mobile)) {
set $pass 1;
}
# Check IP
if ($remote_addr ~* (123.123.123.123|192.168.0.\d+)) {
set $pass 1;
}
# Deny access if not equal to 1
if ($pass != 1) {
return 403;
}
# Other rules for pseudo-static and so on
}