Static resource and dynamic access separation of Nginx entry

Source: Internet
Author: User
Tags tomcat
Absrtact: In practice, we need to give the static resources to Nginx processing, and some Web server requests are given to Tomcat, this can be done in Nginx configuration

In the previous article, we configured the

Location/{
proxy_pass http://localhost:8080;
}

This is the fact that all of Nginx's requests are forwarded to

http://localhost:8080

If we need to forward the Web service request, and other static resources (such as JPG, CSS, etc.) do not forward directly by NIGX processing, then the efficiency should be improved a lot. So this is actually the location of the configuration. The following on the online review of the information about this configuration to do some sorting, we can practice their own

--------------------------------------------------------------------------------------------------------------- --------------

Syntax rules: location [=|~|~*|^~]/uri/{...}

= Start with exact match

^~ begins by indicating that the URI begins with a regular string and is understood to match the URL path. Nginx does not encode the URL, so the request is/STATIC/20%/AA, which can be matched to the rule ^~/static//aa (note is a space).

~ Start indicates a case-sensitive regular match

~* start indicates a case-insensitive regular match

!~ and!~* are case-insensitive and match-insensitive.

/generic match, any request will be matched to.

In the case of multiple location configurations, the matching order is:

First match =, next match ^~, followed by the regular match in the order of the file, and finally to the/General match. When a match succeeds, the match is stopped and the request is processed according to the current matching rule.


example, there are the following matching rules:

Location =/{
#规则A
} location
=/login {
#规则B
} location
^~/static/{
#规则C
}
  location ~ \.    (GIF|JPG|PNG|JS|CSS) $ {
#规则D
} location
~* \.png$ {
#规则E
} location
!~ \.xhtml$ {
#规则F
} location
!~* \.xhtml$ {
#规则G
} location
/{
#规则H
}

The resulting effect is as follows:

Access root directory/, e.g. http://localhost/will match rule a

Access Http://localhost/login will match rule b,http://localhost/register then match rule h

Access http://localhost/static/a.html will match rule C

Accessing Http://localhost/a.gif, Http://localhost/b.jpg will match rule D and rule e, but rule D order takes precedence and rule e does not work, and http://localhost/static/c.png First match to rule C

Access to Http://localhost/a.PNG matches rule e without matching rule d because rule e is not case sensitive.

Access http://localhost/a.xhtml does not match rule f and rule g,http://localhost/a.xhtml does not match rule G because it is case insensitive. Rule f, rule g belongs to the exclusion method, conforms to the matching rule but does not match, so think about where the actual application will be used.

Access to the http://localhost/category/id/1111 is finally matched to the rule H, because the above rules do not match, this time should be nginx forwarding request to the backend application server, such as fastcgi (PHP), Tomcat (JSP), Nginx as the direction proxy server exists.

So in actual use, the individual feels that there are at least three matching rule definitions, as follows:

#直接匹配网站根, through the domain name to visit the site home more frequently, using this will speed up processing, the official website said.
#这里是直接转发给后端应用服务器了, can also be a static home
# First required rule location
=/{
proxy_pass http://tomcat:8080/index
}
# The second required rule is to handle the static file request, which is nginx as the strength of the HTTP server
# There are two configuration modes, directory matching or suffix matching, either one or use location
^~/static/{
root/ webroot/static/;
}
Location ~* \. (Gif|jpg|jpeg|png|css|js|ico) $ {
root/webroot/res/;
}
#第三个规则就是通用规则, used to forward the dynamic request to the backend application server
#非静态文件请求就默认是动态请求, according to
the actual grasp #毕竟目前的一些框架的流行, with the. php,.jsp suffix is rare
. Location/{
Proxy_pass http://tomcat:8080/
}

Third, rewrite grammar

last– basically use this flag.

break– abort Rewirte, do not continue to match

redirect– returns the HTTP status of a temporary REDIRECT 302

permanent– returns the HTTP status of permanent redirection 301

1. The following expressions can be used to judge:

-F and!-f to determine if a file exists

-D and!-d to determine if a directory exists

-E and!-e to determine if a file or directory exists

-X and!-x to determine if a file is executable

2. The following is a global variable that can be used as a judge

Example: http://localhost:88/test1/test2/test.php

$host: localhost

$server _port:88

$request _uri:http://localhost:88/test1/test2/test.php

$document _uri:/test1/test2/test.php

$document _root:d:\nginx/html

$request _filename:d:\nginx/html/test1/test2/test.php

Iv. redirect Grammar

server {
listen;
server_name start.igrow.cn;
Index index.html index.php;
root html;
if ($http _host!~ "^star\.igrow\.cn$&quot {
rewrite ^ (. *) http://star.igrow.cn$1 redirect;
}
}

Five, anti-theft chain

Location ~* \.    (gif|jpg|swf) $ {
Valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid _referer) {
rewrite ^/http://$host/logo.png;
}
}

Vi. setting the expiration time based on the file type

Location ~* \.        (js|css|jpg|jpeg|gif|png|swf) $ {
if (-F $request _filename) {
expires 1h;
break;
}
}

Vii. prohibit access to a directory

Location ~* \.    (txt|doc) ${
root/data/www/wwwroot/linuxtone/test;
Deny all;
}

Some of the available global variables:

 $args $content _length $content _type $ Document_root $document _uri $host $http _user_agent $http _cookie $limit _rate $request _body_file $request _method $remote _addr $remote _port $remote _user $request _filename $request _uri $query _string $scheme $server _protocol $server _addr $ server_name $server _port $uri 

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.