Browser compatibility is the biggest headache for front-end estimation, especially for IE browsers. The front-end usually creates a page to prompt users to upgrade their browsers or display simple static pages. Then we need O & M to configure nginx rewrite rules.
Paste the configuration instance directly here
Server {
Listen 80;
Server_name xxx.xxx.com;
Root/www;
If ($ http_user_agent ~ * "MSIE [6-9]. [0-9]") {
Rewrite/ie.html break;
}
}
Explain the above configuration
$ Http_user_agent client agent information (this is the browser ID. If you have enabled the access log, you can check it. The ID of each browser may be different .)
~ * Regular expressions are used and case-insensitive.
MSIE [6, 7]. [0-9] MSIE-IE browser identifier, which matches IE browser in versions 6 to 9, such as 6.1, 7.0, 8.2 and so on ···
Rewrite/ie6.html returns the specified static page as long as it matches
Break stops executing the current ngx_http_rewrite_module instruction set.
If you need to verify whether it is effective, here is a convenient tool IETester, you can simulate any version of Internet Explorer to test
Nginx global variables
$ Args # This variable is equal to the parameter in the request line.
$ Content_length # Content-length field in the request header.
$ Content_type # Content-Type field in the request header.
$ Document_root # the value specified by the current request in the root command.
$ Host # Request host header field. Otherwise, it is the server name.
$ Http_user_agent # client agent information
$ Http_cookie # client cookie information
$ Limit_rate # this variable can limit the connection rate.
$ Request_body_file # temporary file name of the client request body information.
$ Request_method # The action requested by the client, usually GET or POST.
$ Remote_addr # IP address of the client.
$ Remote_port # The client port.
$ Remote_user # Username verified by Auth Basic Module.
$ Request_filename # the file path of the current request, which is generated by the root or alias command and URI request.
$ Query_string # is the same as $ args.
$ Scheme # HTTP methods (such as http and https ).
$ Server_protocol # the protocol used by the request, usually HTTP/1.0 or HTTP/1.1.
$ Server_addr # server address, which can be determined after a system call.
$ Server_name # server name.
$ Server_port # the port number of the request to the server.
$ Request_uri # The original URI containing the request parameters, excluding the host name, for example, "/foo/bar. php? Arg = baz ".
$ Uri # The current URI without the request parameters. $ uri does not contain the host name, for example, "/foo/bar.html ".
$ Document_uri # is the same as $ uri.
Nginx rewrite basics and instances
Nginx rewrite regular expression match
Case-sensitive matching
~ Case-sensitive matching
~ * Case-insensitive match
!~ And !~ * Case-insensitive and case-insensitive
File and directory matching
-F and! -F is used to determine whether a file exists.
-D and! -D is used to determine whether a directory exists.
-E and! -E is used to determine whether a file or directory exists.
-X and! -X is used to determine whether a file is executable.
Flag
Last is equivalent to the [L] Mark in Apache, indicating that rewrite is completed.
The break terminates the match and does not match the subsequent rules.
Redirect returns 302. The address bar of the temporary redirection will display the address after the jump.
Permanent returns the 301 permanent redirect address bar and the redirected address is displayed.
Several use cases of logcation:
1) location/{}: match any query because all requests start. However, regular expression rules are preferentially matched with queries.
2) location =/{}: only match/
3) location ~ * \. (Gif | jpg | jpeg) $
{
Rewrite \. (gif | jpg) $/logo.png;
}: Location is case-insensitive. It matches any file ending with gif, jpg, and jpeg.
Several instances:
Convert multiple directories to parameters
Requirement: abc.domian.com/sort/2 => abc.domian.com/index.php? Act = sort & name = abc & id = 2
Rule configuration:
If ($ host ~ * (. *) \. Domain \. com ){
Set $ sub_name $1;
Rewrite ^/sort \/(\ d + )\/? $/Index. php? Act = sort & cid = $ sub_name & id = $1 last;
}
Directory swap
Requirement:/123456/xxxx->/xxxx? Id = 123456
Rule configuration:
Rewrite ^/(\ d +)/(. +) // $2? Id = $1 last;
Another automatic rewrite optimized for the browser, where the directory after rewrite may exist;
For example, set nginx to redirect to the/nginx-ie directory when you use ie.
The rules are as follows:
If ($ http_user_agent ~ MSIE ){
Rewrite ^ (. *) $/nginx-ie/$1 break;
}
The directory is automatically added with "/". This function is generally automatically completed by the browser.
If (-d $ request_filename ){
Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
}
The following may be irrelevant to the generalized rewrite.
Disable htaccess
Location ~ /\. Ht {
Deny all;
}
Prohibit multiple directories
Location ~ ^/(Cron | templates )/{
Deny all; break;
}
Prohibit files starting with/data. Prohibit/data/multi-level Directory. Log.txt and other requests.
Location ~ ^/Data {
Deny all;
}
Prohibit a single file
Location ~ /Data/SQL/data. SQL {
Deny all;
}
Set the expiration time for favicon.icoand robots.txt. Here, favicon.icois 99 days, and Robots.txt is 7 days, and no 404 error logs are recorded.
Location ~ (Favicon. ico ){
Log_not_found off;
Expires 99d;
Break;
}
Location ~ (Robots.txt ){
Log_not_found off;
Expires 7d;
Break;
}
Set the expiration time of the browser cache for an object. This parameter is set to 600 seconds and no access logs are recorded.
Location ^ ~ /Html/scripts/loadhead_1.js {
Access_log off;
Expires 600;
Break;
}
Nginx can also customize the expiration time of a type of files. For details, refer to the following code:
Location ~ * \. (Js | css | jpg | jpeg | gif | png | swf) $ {
If (-f $ request_filename ){
Expires 1 h;
Break;
}
}
// Set the expiration time of files such as js, css, jpg, jpeg, gif, png, and swf to one hour.
Anti-Leech settings:
Anti-Leech: If your website is a download site, the download process should first go through your home page to find the download address before downloading. To prevent some users from directly accessing the download address, we can use anti-Leech as follows:
Location ~ * \. (Gif | jpg | swf) $ {
Valid_referers none blocked start.igrow.cn sta.igrow.cn;
If ($ invalid_referer ){
Rewrite ^/http: // $ host/logo.png;
}
}
File anti-leeching and set the Expiration Time -- <the leeching request will also open the image of your site, so set the cache time. This image will not be requested and downloaded every time the leeching occurs.>
Location ~ * ^. + \. (Jpg | jpeg | gif | png | swf | rar | zip | css | js) $ {
Valid_referers none blocked * .jjonline.cn * .jjonline.com.cn * .lanwei.org * .jjonline.org localhost 42.121.107.189;
If ($ invalid_referer ){
Rewrite ^/yun_qi_img/forbid.gif;
Return 417;
Break;
}
Access_log off;
Break;
}
Note:
Here, return 417 is a custom http status code. The default value is 403. It is convenient to find the correct URL for leeching through the nginx log file.
"Rewrite ^/yun_qi_img/forbid.gif;" shows an anti-Leech image.
"Access_log off;" does not record access logs, reducing pressure
"Expires 3d" browser cache for all files for 3 days
Only a fixed ip address is allowed to access the website and a password is added. This is a good choice for applications with permission authentication.
Location \{
Allow 22.27.164.25; # allowed ipd
Deny all;
Auth_basic "KEY"; # authentication settings
Auth_basic_user_file htpasswd;
}
Note: There are also various changes in the location application. The method here is for the root directory.
Redirection when files and directories do not exist
If (! -E $ request_filename ){
# Proxy_pass http: // 127.0.0.1; # jump to the proxy ip address, which has a web server monitored
Rewrite ^/http://www.jjonline.cn/none.html; # jump to this page
# Return 404; the callback directly returns the code, and then searches for the rootdesignated 404.html file.
}
Domain jump
Server {
Listen 80;
Server_name jump.jjonline.cn; # Multi-level domain name to be redirected
Index index.html index.htm index. php; # Name of the entry index file
Root/var/www/public_html/; # root directory of the site
Rewrite ^/http://www.jjonline.cn /;
# Rewrite to this address. Function: enter jump.jjonline.cn in the browser and press Enter. No prompt will be sent to www.jjonline.cn.
Access_log off;
}
Multi-domain redirection
Server {
Listen 80;
Server_name www.jjonline.cn www.jjonline.org;
Index index.html index.htm index. php;
Root/var/www/public_html /;
If ($ host ~ "Jjonline \. org "){
Rewrite ^ (. *) http://www.jjonline.cn $1 permanent;
}
}
Third-level domain jump
If ($ http_host ~ * "^ (. *) \. I \. jjonline \. cn $ "){
Rewrite ^ (. *) http://demo.jjonline.cn $1;
Break;
}
Domain name mirror
Server {
Listen 80;
Server_name 2.16.jjonline.cn;
Index index.html index.htm index. php;
Root/var/www/public_html;
Rewrite ^/(. *) http://www.jjonline.cn/#1 last;
Access_log off;
}
A subdirectory is used as the mirror. The example here is the demo subdirectory.
Location ^ ~ /Demo {
Rewrite ^. + http://demo.jjonline.cn/last;
Break;
}
The rewrite method of the emlog system is as follows:
Location ~ {
If (! -E $ request_filename ){
Rewrite ^/(. +) $/index. php last;
}
}