• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
tohilary
博客园    首页    新随笔    联系   管理    订阅  订阅
nginx try_files 语法

try_files 是nginx0.6.36 后新增一个功能,用于搜索指定目录下的N个文件,如果找不到fileN,则调用fallback中指定的位置来处理请求。个人认为,作为nginx核心的内容,可以部分替代烦琐的rewrite功能,笔者把它用在wp super cache的rewrite重写中,也取得了不错的效果。

 

try_files

语法: try_files file1 [file2 ... filen] fallback

默认值: 无

作用域: location

这个指令的作用就是可接收多个路径作为参数,当前一个路径的资源无法找到,则自动查找下一个路径,如当请求的静态资源不存在,就将请求fallback指定位置到后台服务器上进行动态处理。

简单的例子:

location / {
try_files index.html index.htm @fallback;
}

location @fallback {
root /var/www/error;
index index.html;
}

笔者的博客刚刚建立起来,wp super cache是不可或缺的一个插件,但默认的supercache是针对apache的.htaccess的,在nginx下的
配置如下:

server {
    set $cache /wp-content/cache/supercache/$host;
        #wp-super-cache的路径
    listen 80;
    server_name _;
    location / {
        root /home/html/s0001/domains/$host;
        index index.php index.html;
                #直接调用gzip压缩后的html静态文件
        add_header Content-Type "text/html; charset=UTF-8";
        add_header Content-Encoding "gzip";
        try_files $cache/$uri/index.html.gz @proxy;
 
    }
        #所有静态文件都由nginx处理,并用gzip压缩输出
    location ~* \.(jpg|jpeg|png|gif|css|js|swf|mp3|avi|flv|xml|zip|rar)$ {
        expires 30d;
        gzip on;
        gzip_types  text/plain application/x-javascript text/css application/xml;
        root /home/html/s0001/domains/$host;
    }
        #找不到的文件都交给后端的apache处理了
    location @proxy {
        index  index.php index.htm index.html;
        root   /home/html/$user/domains/$host;
            proxy_pass   http://127.0.0.1:8080;
            proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

这样一个wordpress博客就配置完成了,是不是很简单呢^_^~

posted on 2012-08-24 11:31  tohilary  阅读(2879)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3