Sorted out some Java architecture, interview materials (micro services, clusters, distributed, middleware, etc.), partners in need can pay attention to the public number [programmer internal point], no routine to get

  • Reeling off nine distributed ID generation methods, the interviewer gets a little confused

  • How about the score sheet? You can play him off like this

  • 30,000 words summary, the essence of Mysql optimization

  • Face recognition function based on Java

  • 9 kinds of distributed ID generation of the United States (Leaf) combat


1. Regular expression matching

  1. ~For case sensitive matching
  2. ~ *Is case insensitive matching
  3. ! ~! ~ *Are case sensitive mismatch and case insensitive mismatch respectively

Two, file and directory matching

  1. The -f and! -f is used to check whether files exist

  2. – d and! -d is used to check whether a directory exists

  3. – e and! -e checks whether files or directories exist

  4. – x and y! -x determines whether the file is executable

3. The final argument to the rewrite directive is the flag flag, which has

  1. lastThe equivalent ofapacheThe [L] mark inside representsrewrite.
  2. breakAfter the matching of this rule is complete, the matching is terminated and subsequent rules are not matched.
  3. redirectReturn 302 temporary redirect, browser address will display the URL after jump.
  4. permanentReturns the 301 permanent redirect. The browser address displays the URL after the redirect.

Use last and break to implement URI rewriting, leaving the browser address bar unchanged.

The alias directive must use the last flag. When using the proxy_pass directive, the break flag is required. The Last tag, after the rewrite rule has been executed, will be applied to server{…… The} tag reinitiates the request, and the break flag terminates the match after the rule is matched.

For example: if we will be similar to the URL/photo / 123456 redirect to/path/to/photo / 12/1234/123456. The PNG

rewrite "/photo/([0-9]{2})([0-9]{2})([0-9]{2})" 

rewrite "/path/to/photo/$1/$1$2/$1$2$3.png" ;
Copy the code

Four,NginxRewriteRule dependent instruction

1. The break command

Use environment: Server, location, if

The purpose of this directive is to complete the current rule set and not deal with the rewrite directive.

2. The if command

Context: Server, location

This directive checks if a condition is met, and if so, executes the statement inside the braces. The If instruction does not support nested, does not support multiple conditions && and | | processing.

3. Return instruction

Grammar: returncode

Use environment: Server, location, if

This directive is used to end the execution of the rule and return the status code to the client.

Example: If the accessed URL ends in “.sh” or “.bash”, the 403 status code is returned

location ~ .*\.(sh|bash)? $ { return 403; }Copy the code

Rewrite directive

Syntax: rewriteregex replacement flag

Use environment: Server, location, if

This directive redirects urIs based on expressions or modifies strings. Instructions are executed according to the order in the configuration file. Note that rewrite expressions are valid only for relative paths. If you want to pair host names, you should use the if statement as shown in the following example:


if( $host ~* www\.(.*) )
{
   set $host_without_www $1;
   rewrite ^(.*)$  http://$host_without_www$1permanent;
}
Copy the code

5. Set instruction

Syntax: setvariable value; Default value: None Context: Server, location, if

This directive defines a variable and assigns a value to it. Variable values can be text, variables, and combinations of text variables.


   set$varname "hello world";
Copy the code

6. Uninitialized_variable_warn directive

Grammar: uninitialized_variable_warnon | off

Context: HTTP, server, location, if

This command is used to turn on and off warnings for uninitialized variables. The default value is on.

Five. Rewrite rule writing example of Nginx

1. If the accessed file or directory does not exist, redirect to an HTML file

if( ! -e $request_filename ) { rewrite ^/(.*)$ index.htmllast; }Copy the code

/123456/ XXXX ====> / XXXX? id=123456

rewrite ^/(\d+)/(.+)/ /$2? id=$1 last;Copy the code

3. If the client uses Internet Explorer, redirect the client to/IE


if( $http_user_agent  ~ MSIE)
{
    rewrite ^(.*)$ /ie/$1 break;
}
Copy the code

4. Do not access multiple directories


location ~ ^/(cron|templates)/
{
    deny all;
    break;
}
Copy the code

5. Do not access files starting with /data


location ~ ^/data
{
    deny all;
}
Copy the code

6. Do not access files whose file name extensions start with. Sh,. FLV, or


location ~ .*\.(sh|flv|mp3)$
{
    return 403;
}
Copy the code

7. Set the browser cache time for certain types of files


location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
    expires 30d;
}
location ~ .*\.(js|css)$
{
    expires 1h;
}
Copy the code

8. Set expiration times for favicon.ico and robots.txt

Here favicon.ico is 99 days,robots.txt is 7 days and no 404 error log is recorded


location ~(favicon.ico) {
   log_not_found off;
   expires 99d;
   break;
}
location ~(robots.txt) {
   log_not_found off;
   expires 7d;
   break;
}
Copy the code

9. Set the expiration time of a file. This is 600 seconds and no access log is logged


location ^~ /html/scripts/loadhead_1.js {
    access_log  off;
    root /opt/lampp/htdocs/web;
    expires 600;
    break;
}
Copy the code

10, file anti-chain and set the expiration time

Return412 is a custom HTTP status code, default is 403, easy to find the correct link request

rewrite ^/ http: //img.linuxidc.net/leech.gif; Access_log off; // Do not record the access log, Stress expires 3 d / / all documents 3 days browser cache location ~ * ^. + \. (JPG | jpeg | | PNG GIF | SWF | rar | zip | | js, CSS) ${valid_referers none blocked *. *. Linuxidc.com linuxidc.net localhost 208.97.167.194; if ($invalid_referer) { rewrite ^/ http://img.linuxidc.net/leech.gif; return 412; break; } access_log off; root /opt/lampp/htdocs/web; expires 3d; break; }Copy the code

11. Allow only fixed IP addresses to access the site and add passwords

root /opt/htdocs/www; Allow 208.97.167.194; Allow 222.33.1.2; Allow 231.152.49.4; deny all; Auth_basic C1G_ADMIN; auth_basic_user_file htpasswd;Copy the code

12, multi-level directory files into a file, enhance seo effect

/ job - 123-456-789. The HTML point/job / 123/456/789 HTML rewrite ^ / job - ([0-9] +) - ([0-9] +) - ([0-9] +) \. HTML $/ job / $1 / $2 / jobshow_ $3. HTML last;Copy the code

Redirection when files and directories do not exist:


if (!-e $request_filename) {
    proxy_pass http://127.0.0.1;
}
Copy the code

14. Point a folder in the root directory to the level 2 directory

/shanghaijob/ point /area/ Shanghai/if you change last to permanent, Show is the browser address bar/location/Shanghai/rewrite ^ / ([0-9 a-z] +) job/(. *) $/ area / $1 / $2 last; Rewrite ^/([0-9a-z]+)job$/area/$1/ last; rewrite ^/([0-9a-z]+)job$/area/$1/ last; rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2last; Shanghia /list_1.html will change to /list_1.html./list_1.html will change to /list_1.html. Rewrite (-d $request_filename) rewrite (-d $request_filename) So there is no effect if (-d $request_filename) {rewrite ^ / (. *) $http://$host/$1$2/permanent; ([^ /]) Rewrite ^/([0-9a-z]+) rewrite ^/([0-9a-z]+)job$/$1job/permanent; rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2last;Copy the code

15, domain name jump


server{

  listen      80;
  server_name  jump.linuxidc.com;
  index index.html index.htm index.php;
  root  /opt/lampp/htdocs/www;
  rewrite ^/ http://www.linuxidc.com/;
  access_log  off;
}

Copy the code

16, multi-domain turn


server_name  www.linuxidc.comwww.linuxidc.net;
index index.html index.htm index.php;
root  /opt/lampp/htdocs;
if ($host ~ "linuxidc\.net") {
    rewrite ^(.*) http://www.linuxidc.com$1permanent;
}
Copy the code

Nginx global variables

Arg_PARAMETER # this variable contains the value of PARAMETER, if any, in the GET request. Args # This variable is equal to the parameters in the request line (GET request), e.g. Foo =123&bar=blahblah; Binary_remote_addr # Binary customer address. Body_bytes_sent # Number of body bytes sent in response. This data is accurate even if the connection is broken. Content_length # Content-Length field in the request header. Content_type # Content-Type field in the request header. Cookie_COOKIE # The value of the cookie variable document_root # The value specified in the root directive is currently requested. Document_uri # is the same as uri. Host # Request host header field, otherwise server name. Hostname #Set to themachine's hostname as returned by gethostName http_HEADER is_args # , otherwise equal to "", null. Limit_rate # This variable can limit the connection rate. Query_string # same as args. Request_body_file # Temporary file name for the body information requested by the client. Request_method # The action requested by the client, usually GET or POST. Remote_addr # IP address of the client. Remote_port # Port of the client. Remote_user # The user name that has been authenticated by the Auth Basic Module. Request_completion # If the request ends, set it to OK. Empty if the request is not finished or if the request is not the last in the request chain. Request_method #GET or POST request_filename # The file path of the current request, generated by the root or alias directive and the URI request. Request_uri # contains the original URI of the request parameters, excluding the host name, e.g. "/foo/bar.php? Arg = baz ". Cannot be modified. Scheme #HTTP methods (e.g. HTTP, HTTPS). Server_protocol # The protocol used in the request, usually HTTP/1.0 or HTTP/1.1. Server_addr # Server address, which can be determined after a system call. Server_name # Indicates the server name. Server_port # Port number for the request to reach the server.Copy the code

#### 7. Apache and Nginx rules

Apache RewriteCond corresponds to Nginx's if Apache RewriteRule corresponds to Nginx's rewrite [R] corresponds to Nginx's redirect Apache [P] corresponds to Nginx's last Apache [R,L] corresponds to Nginx redirect Apache [P,L] corresponds to Nginx last Apache [PT,L] corresponds to Nginx lastCopy the code

For example: allow the specified domain name to access this site, all other domain names are to www.linuxidc.net

Apache: RewriteCond %{HTTP_HOST} ! ^ (. *?) \.aaa\.com$[NC] RewriteCond %{HTTP_HOST} ! ^localhost$ RewriteCond %{HTTP_HOST}! ^ 192\168 \. 0 \. (. *?) $ RewriteRule ^/(.*)$ http://www.linuxidc.net[R,L]Copy the code

Examples of Nginx filtering:

If ($host ~ * ^ (. *) \. Aaa\.com $) {set $allowHost '1'. } if($host ~* ^localhost) {set $allowHost '1'; } if( $host ~* ^192\.168\.1\.(.*?) $) {set $allowHost '1'; } if( $allowHost ! ~ '1') {rewrite ^ / (. *) $http://www.linuxidc.netredirect; }Copy the code

conclusion

Back-end development is one of the most close to the full stack of a career, the front end is not enough to write the page JS on top of the back end, there is no operation and maintenance does not matter to the back end to maintain the server, in short, a good back-end is to cover everything.


So much for today, if this article is a little help to you, I hope you can get a thumbs up 👍 oh

Your approval is my motivation to write!

Small benefits:

Hundreds of technical e-books, SHH ~, free for your friends. Please reply to the official account [666] for self-collection

Sorted out some Java architecture, interview materials, partners in need can pay attention to the public number [programmer internal points]