In the previous posts I mentioned the Nginx in addition to a Web server outside the Nginx or a small reverse proxy server We can use nginx some static files such as CSS JS images stored in the client local next request directly from the local read to speed up the page response speed For example, we can use a distribution match to distribute a request for a PHP file to a machine to assign requests for a py file to the B machine
There are a lot of other things that are not detailed here, but all of the above functions require location module of the following I will detail the next location module
Nginx's location module is used to do pattern matching official documents Refer:http://nginx.org/en/docs/http/ngx_http_core_module.html#location
Syntax rules: location [=|~|~*|^~]/uri/{...}
= The beginning indicates an exact match
The beginning of the ^~ indicates 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 and can be matched by the rule ^~/static//aa (note is a space).
~ starts with a case-sensitive regular match
~* begins with a case-insensitive regular match
!~ and!~*, respectively, are case insensitive and case-insensitive mismatches
/Universal match, any request will match to.
Multiple location configuration in the case of matching order for (resources, not actually validated, try to know, do not adhere to, for reference only):
First match =, second match ^~, followed by regular matching in file order, finally to/Universal match. When a match is successful, stop the match and process the request according to the current matching rule. Here I'll introduce a few common usage and configuration
1. Prohibit access such as I do not allow access to the project under the. htaccess file can be set this way
Location ~/\.ht {
deny all;
}
Deny all indicates rejection of all requests
2. Push the command of the request PHP file to PHP-FPM
Location ~ \.php$ {
root /xxx;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Fastcgi_param script_filename $document _root$fastcgi_script_name;
Include fastcgi_params;
}
In location, you can use Nginx built-in variables as described in http://blog.csdn.net/zhangsheng_1992/article/details/51727031
3. Forwarding requests to node servers
Upstream Sample {
server 127.0.0.1:9000;
}
server{
Listen;
server_name www.xxx.com;
Location/{
proxy_redirect off;
Proxy_set_header x-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
Proxy_set_header X-forwarded-proto $scheme;
Proxy_set_header Host $http _host;
Proxy_set_header x-nginx-proxy true;
Proxy_set_header Connection "";
Proxy_http_version 1.1;
Proxy_pass http://sample
}
}
All requests for www.xxx.com will be forwarded to port 9000
4. Reverse proxy static file gif jpg jpeg png bmp swf js CSS
Location ~. *\. (gif|jpg|jpeg|png|bmp|swf|js|css) ${
#使用Web缓存区cache_one
proxy_cache cache_one;
#对不同HTTP状态码缓存设置不同的缓存时间
proxy_cache_valid 304 12h;
Proxy_cache_valid 302 1m;
Proxy_cache_valid any 1m;
#设置Web缓存的Key值, nginx the hash storage cache according to the key value, where the "domain name, URI,
#参数" is combined into a key
proxy_cache_key $host $uri$is_args$args;
}
Before I have an article introduced the next Squid reverse proxy comparison between the different http://blog.csdn.net/zhangsheng_1992/article/details/44979165
5.url Rewrite request/123456/xxxx changed to/xxxx?id=123456
Location/{
Rewrite ^ (. *)/equip (d+). html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
And then I'll have a piece of article dedicated to Nginx rewrite please pay attention to
6. Set the address to view nginx status
Location/nginxstatus {
stub_status on;
Access_log on;
Auth_basic "Nginxstatus";
Auth_basic_user_file conf/htpasswd;
}
7 Anti-Theft Chain
Location ~* \. (gif|jpg|png|swf|flv) $ {
Valid_referers none blocked www.xxx.com xxx.com;
if ($invalid _referer) {
rewrite ^/http://www.xxx.com/retrun.html;
#return 403;
}
Not www.xxx.com's picture request is rejected
In addition to the log format message header forwarding can also be configured in the location module interested to see my previous blog here is not introduced