Java regular expressions differ from Nginx regular expressions

Source: Internet
Author: User
Tags browser cache
Nginx A large number of regular expressions used to match characters, most commonly in the location{} block, used to match the requested access URL, or some built-in variables for conditional judgment, such as:
Location ~. *\. (JS|CSS)? $ {

                expires 1h;

            }

Students who have studied Java regular expressions may look a bit familiar, and there are some similarities with nginx regular expressions.
To make it easier to master Nginx regular expressions from Java expressions, now look at where the differences are. Nginx A regular expression, you can set whether the case sensitivity

~ case-sensitive (case sensitive) matching success
~* matching case-insensitive success

Plus! An inverse proposition is a sign

!~ Match Case matching failed
!~* case-insensitive match failure

In Java regular expressions, this is how you set the case-sensitive

(i) ABC abc three letters ignore case
A (? i) BC only BC ignore case
A ((? i) b) c only B ignores case

See, (?!) Plays a role that ignores case, scope is the character behind it, or the whole in () brackets.

*

Nginx Regular expression, * means to match any character, can be empty, not limited in length.
In Java, * indicates that the preceding character or subexpression is matched 0 or more times. For example, zo* matches "z" and "Zoo". * is equivalent to {0,}.

Code, you can use Pattern.compile (rexp,pattern.case_insensitive) to indicate that the whole REXP expression ignores case

^ and $

These two Java and Nginx are the same:

^ with what the beginning of the match
$ at what end of the match
For example:

Location ^~/images/{
}

Stops matching any subsequent characters after matching any queries that have been/images/and successfully matching these characters.

Location ~*. (Gif|jpg|jpeg) $ {
}

Matches any request that has a. gif,. jpg, or. jpeg End

=

Nginx to indicate an exact lookup address (similar to a preferred match)
The location of the identifier = is first matched, and if the request URL exactly matches the location, that represents a successful match.

such as location =/
It will only match the URL/request, and if the request is/index.html, it will not match this. Conversely, will go to match the other location regular expression, matches location/index.html,location ~/*.html and so on.
Of course, you can write two location,location =/and location/, so that/index.html will match to the latter, if the site to/a large number of requests, you can use this method to speed up the request response speed.

In Java, = is the match = number, there is no such use and concept like nginx, in contrast to Java is used. To show greed or not greedy strategy.

When. The matching pattern is "not greedy" after any other qualifier (*, + 、?、 {n}, {n,}, {n,m}) followed by the character. The "not greedy" pattern matches the shortest possible string of searches, while the default "greedy" pattern matches the search for the longest string possible.
For example, in the string "Oooo", "o+?" Matches only a single "O", and "o+" matches All "O".

.

In Nginx,. This concept is the same as in Java. , the expression matches any one character.
and. is a match. Character.

In nginx regular expressions, there is also a very important concept. Matches a file or file directory, such as:

-F and!-f are used to determine whether a file exists
-D and!-d used to determine whether a directory exists
-E and!-e used to determine whether a file or directory exists
-X and!-x are used to determine whether a file is executable

For example, redirects when files and directories do not exist:

if (!-e $request _filename) {
Proxy_pass http://127.0.0.1;
}

Finally, take a look at some comprehensive examples.

prevent access to multiple directories
Location ~ ^/(cron|templates)/
{
Deny all;
Break
}

prohibit access to files that start with/data
Location ~ ^/data
{
Deny all;
}

prohibit access to files with the. sh,.flv,.mp3 file suffix name
Location ~. *. (Sh|flv|mp3) $
{
return 403;
}

Set Browser cache time for certain types of files
Location ~. *. (gif|jpg|jpeg|png|bmp|swf) expires30d;location. ∗. (JS|CSS) {expires 30d;} location ~. *. (JS|CSS)
{
Expires 1h;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.