Back

nginx rewrite/try_file tips(nginx rewrite/try_files 小提示)

发布时间: 2014-03-15 10:34:00

refer to: http://stackoverflow.com/questions/22032751/how-to-process-dynamic-urls-as-static-pages-using-nginx

最近,一个项目的请求让我们的Rails不足以负担(300 req /seconds是我们的极限),所以我打算把它做成静态化的页面,这样在我的 2核CPU上都可以轻松跑到15000 req/s.  ( recently I am considering migrate our dynamic rails pages to static files served by Nginx which is much powerful than rails- 15k req/s v.s. 300 req/s. )

于是我们的nginx 配置文件是: ( so our nginx config file looks like: )

  server {
    listen       100;
    charset utf-8;
    root /workspace/test_static_files;
    index index.html index.htm;
    location /popup_pages {
      log_subrequest on; 
      try_files /platform-$arg_platform-product-$arg_product.json /default.json;
#  DON'T use like below, nginx could not process underscore filenames mixing up with $arg_parameter_name:
#      try_files /platform_$arg_platform_product_$arg_product.json /default.json?q=$uri;
#      don't use rewrite ...  use try_files instead. 
#      rewrite ^/popup_pages /platform-$arg_platform-product-$arg_product.json;
    }   
  }

几个小tips: (some tips)

1. 不要使用下划线。 因为nginx无法正确判断 $arg_param 中的变量。 要使用横线'-' (use '-' instead of underscore '_' in your static file names)

#      DON'T use like below, nginx could not process underscore filenames mixing up with $arg_parameter_name:
#      try_files /platform_$arg_platform_product_$arg_product.json /default.json?q=$uri;

2. 尽量不要使用rewrite ,因为if is Evil ( If is evil, see: http://wiki.nginx.org/IfIsEvil )

#      Don't use rewrite ...  use try_files instead. 
#      rewrite ^/popup_pages /platform-$arg_platform-product-$arg_product.json;

3. 调试时,可以先使用 rewrite 来调试,调试完毕后,把它改写成 try_files. 调试rewrite 时,时刻关注access.log/error.log (  while debugging , you can use "rewrite". once done,  replace the "rewrite" with "try_files".   and take an eye on "access.log/error.log" )

Back