一,动静分离
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.buge.com;
# 0
location / {
root html;
index index.html index.htm;
}
# 1
location /buge {
root /usr/local/etc/nginx/;
index index.html;
}
# 2
location /buge22 {
alias /usr/local/etc/nginx/;
index index.html;
}
# 3
location ~* \.(gif|jpg|png|css|js)$ {
root /usr/local/etc/nginx/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# include servers/*;
}
上图配置了三个静态站点,1与2 的区别是,1使用了root指定资源路径,2使用了alias指定资源路径;
资源寻址过程:通过域名,找到匹配的server块-》通过URI根路径匹配location-》若为root,则root值+location值为资源位置;若为alias,则alias值为资源位置,不再拼接location的值。
3是支持通配符的location写法,注意只能用root,不能用alias。若使用alias,Nginx会自动在URI后+/,比如访问www.buge.com/a.png,时,会自动跳转到www.buge.com/a.png/,就找到了location块0的资源,最终404;而使用root时,www.buge.com/a.png,不会出现这个坑!
未完待续。。。