Click “Programmer internal matters” and select “Set star Mark”.
Insist on learning, good daily delivery!
Silly force, led
We’re running out of people at the front. Write the page yourself
Handsome than
Lead me in back-end development
Silly force, led
The o&M guy quit, you do your own Nginx cluster environment
Handsome than
Oh, my God. I’m a back-end developer
1. Regular expression matching
-
~ is case sensitive
-
~* is case insensitive
-
! ~ and! ~* indicates case sensitive mismatch and case insensitive mismatch respectively
Two, file and directory matching
-
The -f and! -f is used to check whether files exist
-
– d and! -d is used to check whether a directory exists
-
– e and! -e checks whether files or directories exist
-
– x and y! -x determines whether the file is executable
3. The final argument to the rewrite directive is the flag flag, which has
-
“Last” is equivalent to the [L] tag in Apache, rewriting.
-
Break After the match of this rule is complete, the match is terminated.
-
Redirect Returns 302 temporary redirect. The browser address displays the redirect URL.
-
Permanent Returns the 301 permanent redirect, and 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 rewrite the server on which it resides. 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
1rewrite "/photo/([0-9]{2})([0-9]{2})([0-9]{2})" 23rewrite "/path/to/photo/$1/$1$2/$1$2$3.png" ;
Copy the code
NginxRewrite rules
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
1location ~ .*\.(sh|bash)? $2{3 return 403; 4}
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:
1if( $host ~* www\.(.*) )2{3 set $host_without_www $1; 4 rewrite ^(.*)$ http://$host_without_www$1permanent; 5}
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.
1 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
1if( ! -e $request_filename )2{3 rewrite ^/(.*)$ index.htmllast; 4}
Copy the code
/123456/ XXXX ====> / XXXX? id=123456
1 rewrite ^/(\d+)/(.+)/ /$2? id=$1 last;
Copy the code
3. If the client uses Internet Explorer, redirect the client to/IE
1if( $http_user_agent ~ MSIE)2{3 rewrite ^(.*)$ /ie/$1 break; 4}
Copy the code
4. Do not access multiple directories
1location ~ ^/(cron|templates)/2{3 deny all; 4 break; 5}
Copy the code
5. Do not access files starting with /data
1location ~ ^/data2{3 deny all; 4}
Copy the code
6. Do not access files whose file name extensions start with. Sh,. FLV, or
1location ~ .*\.(sh|flv|mp3)$2{3 return 403; 4}
Copy the code
7. Set the browser cache time for certain types of files
1location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$2{3 expires 30d; 4}5location ~ .*\.(js|css)$6{7 expires 1h; 8}
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
1location ~(favicon.ico) { 2 log_not_found off; 3 expires 99d; 4 break; 5} 6location ~(robots.txt) { 7 log_not_found off; 8 expires 7d; 9 break; 10}
Copy the code
9. Set the expiration time of a file. This is 600 seconds and no access log is logged
1location ^~ /html/scripts/loadhead_1.js {2 access_log off; 3 root /opt/lampp/htdocs/web; 4 expires 600; 5 break; 6}
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
1rewrite ^/ http: //img.linuxidc.net/leech.gif; // display an anti-theft image 2access_log off; // Do not record the access log, Reduce stress 3 expires 3 d / / all documents 3 days of the browser cache 4 5 location ~ * ^. + \. (JPG | jpeg | | PNG GIF | SWF | rar | zip | | js, CSS) ${6 valid_referers none Blocked *. *. Linuxidc.com linuxidc.net localhost 208.97.167.194; 7if ($invalid_referer) { 8 rewrite ^/ http://img.linuxidc.net/leech.gif; 9 return 412; 10 break; 11}12access_log off; 13root /opt/lampp/htdocs/web; 14expires 3d; 15break; 16}
Copy the code
11. Allow only fixed IP addresses to access the site and add passwords
1root /opt/htdocs/www; 2 allow 208.97.167.194; 3 allow 222.33.1.2; 4 allow 231.152.49.4; 5deny all; 6 auth_basic C1G_ADMIN; 7auth_basic_user_file htpasswd;
Copy the code
12, multi-level directory files into a file, enhance seo effect
1 / job - 123-456-789. The HTML point/job / 123/456/789. Html23rewrite ^ / 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:
1if (! -e $request_filename) {2 proxy_pass http://127.0.0.1; 3}
Copy the code
14. Point a folder in the root directory to the level 2 directory
/shanghaijob/ point /area/ Shanghai / 2 The browser address bar show is rewrite/location/Shanghai / 3 ^ / ([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; 6rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2last; Shanghia /list_1.html will be changed to /list_1.html, so it is not accessible. Rewrite (-d $request_filename) rewrite (-d $request_filename) So there is no effect of 11 if (-d $request_filename) {12 rewrite ^ / (. *) $http://$host/$1$2/permanent; ([^ /]) Rewrite ^/([0-9a-z]+) rewrite ^/([0-9a-z]+)job$/$1job/permanent; 16rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2last;
Copy the code
15, domain name jump
1server{23 listen 80; 4 server_name jump.linuxidc.com; 5 index index.html index.htm index.php; 6 root /opt/lampp/htdocs/www; 7 rewrite ^/ http://www.linuxidc.com/; 8 access_log off; 9}
Copy the code
16, multi-domain turn
1server_name www.linuxidc.comwww.linuxidc.net; 2index index.html index.htm index.php; 3root /opt/lampp/htdocs; 4if ($host ~ "linuxidc\.net") {5 rewrite ^(.*) http://www.linuxidc.com$1permanent; 6}
Copy the code
Nginx global variables
The 1arg_PARAMETER # variable contains the PARAMETER value, if any, in the GET request. 2args # This variable is equal to the parameters in the request line (GET request), e.g. Foo =123&bar=blahblah; 3binary_remote_addr # Binary customer address. 4body_bytes_sent # Number of body bytes sent in response. This data is accurate even if the connection is broken. 5content_length # Content-length field in the request header. 6content_type # Content-type field in the request header. 7cookie_COOKIE #cookie the value of the cookie variable 8document_root # The value specified in the root directive is currently requested. 9document_uri # Same as uri. 10host # Request host header field, otherwise server name. 11hostname #Set to themachine's hostname as returned by gethostname12http_HEADER13is_args # If there is an args parameter, this variable is equal to "?" , otherwise equal to "", null. 14HTTP_user_agent # Client agent info 15HTTP_cookie # client cookie info 16limit_rate # This variable can limit connection rate. 17query_string # same as args. 18request_body_file # Temporary file name for the body information requested by the client. 19request_method # The action requested by the client, usually GET or POST. 20remote_addr # IP address of the client. 21REMOTE_port # Specifies the client port. 22Remote_user # The user name that has been authenticated by the Auth Basic Module. 23request_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. 24request_method #GET or POST25request_filename # The file path of the current request, generated by the root or alias directive and the URI request. 26request_uri # Contains the original URI of the request parameters, excluding the host name, e.g. "/foo/bar.php? Arg = baz ". Cannot be modified. 27scheme #HTTP methods (e.g. HTTP, HTTPS). 28server_protocol # The protocol used for the request, usually HTTP/1.0 or HTTP/1.1. 29SERVER_ADDR # Server address, which can be determined after a system call. 30server_name # Indicates the server name. 31server_port # Port number for the request to reach the server.
Copy the code
Apache and Nginx rules
1Apache RewriteCond corresponds to Nginx if2Apache RewriteRule corresponds to Nginx rewrite3Apache [R] corresponds to Nginx redirect4Apache [P] corresponds to Nginx last5Apache [R,L] corresponds to Nginx redirect6Apache [P,L] corresponds to Nginx last7Apache [PT,L] corresponds to Nginx last
Copy the code
For example: allow the specified domain name to access this site, all other domain names are to www.linuxidc.net
1Apache:2RewriteCond %{HTTP_HOST} ! ^ (. *?) \.aaa\.com$[NC]3RewriteCond %{HTTP_HOST} ! ^localhost$ 4RewriteCond %{HTTP_HOST}! ^ 192\168 \. 0 \. (. *?) $5RewriteRule ^/(.*)$ http://www.linuxidc.net[R,L]
Copy the code
Examples of Nginx filtering:
1 the if ($host ~ * ^ (. *) \. Aaa\.com $) 2 {3 set $allowHost '1'; 4} 5if($host ~* ^localhost) 6{7 set $allowHost '1'; 8} 9if( $host ~* ^192\.168\.1\.(.*?) $)10{11 set $allowHost '1'; 12}13if( $allowHost ! ~ '1') {14 15 rewrite ^ / (. *) $http://www.linuxidc.netredirect; 16}
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!
I was forced to learn JAVA crawlers in order not to copy and paste
Reeling off nine distributed ID generation methods, the interviewer gets a little confused
How about the score sheet? You need to know these things
Face recognition function based on Java
What about distributed ids? Tinyid threw it to him
9 kinds of distributed ID generation of the United States (Leaf) combat
Get some geek paid lessons, SHH, for free. Concern public number reply [geek] to get
Technology/interview/teasing
There’s everything a programmer can do
Long press scan code can be concerned