1、server下配置的每个location,都需要有自己的一套代理配置
即要么加入:
root 某个目录
要么加入
proxy_pass 某个地址;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
否则如果该location被请求匹配,nginx会先读取该location下的root或者proxy_pass,如果没有,则取server下全局的root配置
如果还没有,则使用默认配置(nginx安装目录下的html目录)
如果多个location中使用了同一个配置,可以把配置抽为一个conf文件,之后再include *.conf把配置引进来
官方root说明:http://nginx.org/en/docs/http/ngx_http_core_module.html#root
Syntax:root
path
;
Default:
root html;
Context:http
, server
, location
, if in location
可用在http,server,location和if中
官方proxy_pass说明:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
Syntax: | proxy_pass |
---|---|
Default: | — |
Context: | location , if in location , limit_except |
只能用在location和if中
于是就导致了:要想使用expired令浏览器使用缓存机制,则必须要在静态文件的匹配中
要么把root配置到对应的网站文件夹下,要么配置到proxy_pass
配置好之后,成功让浏览器接受到304响应,嘿嘿,下面是配置文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
# ‘$status $body_bytes_sent "$http_referer" ‘
# ‘"$http_user_agent" "$http_x_forwarded_for"‘;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt)$
{
#缓存时长
expires 7d;
}
#JS和CSS缓存时间设置
location ~ .*\.(js|css)?$
{
expires 1h;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache‘s document root
# concurs with nginx‘s one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 80;
server_name angular.ailiailia.com;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
}
# location /status {
# stub_status on;
# access_log off;
# }
location /.(gif|jpg|jpeg|png|bmp|swf)$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8000;
expires 30d;
}
location /.(js|css)?$ {
expires 12h;
}
}
server {
listen 80;
server_name node.ailiailia.com;
location / {
proxy_pass http://localhost:3000/;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
}
location /.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location /.(js|css)?$ {
expires 12h;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
2、nginx的匹配优先级:
不同于防火墙,前面的规则优先
nginx类似于css,权重高的优先
但是也不全是:
一、 location 的匹配符
1.等于匹配符:=
等于匹配符就是等号,特点可以概括为两点:
精确匹配
不支持正则表达式
2.空匹配符
空匹配符的特点是:
匹配以指定模式开始的 URI
不支持正则表达式
3.正则匹配符:~
正则匹配符是可以使用正则表达式的匹配符。不过这里要强调的是,一般来说~是指:
区分大小写的正则匹配
而~*表示:
不区分大小写的正则匹配
但是对于一些对大小写不敏感的操作系统,这两者没有区别。另外一个就是^~,其表示以指定模式开始的正则匹配。
4.内部访问符:@
一般用于错误页面等,这个暂不讨论。
二、匹配符优先级
1.=
2.空匹配符,满足精确匹配时
3.^~
4.~或~*
5.空匹配符,满足以指定模式开始时的匹配时
(location =) > (location 完整路径 >) >(location ^~ 路径) >(location ~* 正则) >(location 路径)
只要匹配到,其它的都会忽略,然后返回到改匹配。
同级匹配符按从上到下的优先级排列
location 匹配的原型是这样的:location [=|~|~*|^~|@] /uri/ { … }
“=”是精确匹配
“@”是命名的location ,在正常的location 匹配中不会使用,仅仅在内部跳转中才会使用到。
“~”是区分大小写的匹配
“~*”是不区分大小写的匹配
“^~”表示中止正则匹配(这个平时没太注意)
在一个请求中,匹配的顺序是这样的。先使用所有location 来匹配URI的开始部分,最精确匹配的(形象点说,就是即配置字符数最多的)为最后匹配结果;然后进行正则表达式的匹配,按照配置文件中的顺序来进行匹配,如果有一个匹配成功,则结束正则匹配,且最后匹配结果为此location ,否则,最后结果为先前最精确匹配的的那个location 。
之前有提到过”^~”,它配置在非正则匹配中,表示,如果最精确匹配的loction为此location ,则立即返回该location 作为结果,而不进行下一步的正则匹配,这样,就此可以不必要进入到正则匹配当中,以加快匹配速度。
还有”=”,它是最精确的匹配,而且优先级最高。最先进行带”=”的匹配,如果匹配成功,立马返回。
最后总结下匹配的过程,有四步:
1. 带”=”前缀的先进行匹配,如果找到了,中止查找。
2. 所有其它location 进行非正则的匹配,找到最精确匹配的那个,如果匹配到带”^~”前缀的,则中止查找。
3. 正则查找,按照我们配置文件中配置的location 顺序进行查找。
4. 如果正则查找匹配成功,则使用此正则匹配的location ,否则,使用第二步查找的结果。
这里要特别说明下”=”与”^~”的区别:
“=”在匹配时,则匹配带”=”的location 。而”^~”,则会匹配所有非”=”的非正则location ,只有在确认它是最精确匹配的location 后,才生效。
location 匹配的优先级(与location在配置文件中的顺序无关)
= 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
最后匹配理带有"~"和"~*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。
location 优先级官方文档
例如
location = / { # 只匹配"/". [ configuration A ] } location / { # 匹配任何请求,因为所有请求都是以"/"开始 # 但是更长字符匹配或者正则表达式匹配会优先匹配 [ configuration B ] } location ^~ /images/ { # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location [ configuration C ] } location ~* .(gif|jpg|jpeg)$ { # 匹配以 gif, jpg, or jpeg结尾的请求. # 但是所有 /images/ 目录的请求将由 [Configuration C]处理. [ configuration D ] }
请求URI例子:
@location 例子
error_page 404 = @fetch;
location @fetch(
proxy_pass http://fetch;
)
原文:http://www.cnblogs.com/guangshan/p/4853775.html