location / {
}
location ~ .*\.(gif)$ {
}
对于这样的配置文件,开始我一直没弄清楚他的顺序,比如/gif.gif这个文件,我开始以为location /{} 这里是匹配的,所以开始要执行里面的东西,然后到location ~ .*\(gif)$ {}又能匹配,然后又执行里面的代码,其实这种思路是错的。
nginx执行顺序其实很简单,正则表达式优先,然后才是普通的。相当于就是SQL的 ORDER BY 正则表达式的LOCATION,先后顺序.
location /{虽然在前面,不过location ~.*\.gif是正则,所以他匹配 location ~.*\.gif.
下面贴一个我常用的nginx反向代理配置。(前端缓存图片,js和flash文件)
-
- user nobody;
- worker_processes 2;
- events {
- use epoll;
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- server {
- listen 80;
- server_name localhost;
- charset utf-8;
- root caches;
- proxy_set_header Host $host;
- proxy_set_header Client-Ip $remote_addr;
- proxy_connect_timeout 5;
- proxy_read_timeout 60;
- proxy_send_timeout 60;
- location / {
- proxy_pass http://192.168.0.1:8080;
- }
- location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
- proxy_store on;
- proxy_store_access user:rw group:rw all:rw;
- proxy_temp_path caches;
- if (!-e $request_filename) {
- proxy_pass http://192.168.0.1:8080;
- }
- }
- }
- }