nginx rewrite规则和参考

http://edu.codepub.com/show.php?contentid=25477

 

正则表达式匹配,其中:

  1. * ~ 为区分大小写匹配
  2. * ~* 为不区分大小写匹配
  3. * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配

文件及目录匹配,其中:

  1. * -f和!-f用来判断是否存在文件
  2. * -d和!-d用来判断是否存在目录
  3. * -e和!-e用来判断是否存在文件或目录
  4. * -x和!-x用来判断文件是否可执行

flag标记有:

  1. * last 相当于Apache里的[L]标记,表示完成rewrite
  2. * break 终止匹配, 不再匹配后面的规则
  3. * redirect 返回302临时重定向 地址栏会显示跳转后的地址
  4. * permanent 返回301永久重定向 地址栏会显示跳转后的地址

一些可用的全局变量有,可以用做条件判断(待补全)

  1. $args
  2. $content_length
  3. $content_type
  4. $document_root
  5. $document_uri
  6. $host
  7. $http_user_agent
  8. $http_cookie
  9. $limit_rate
  10. $request_body_file
  11. $request_method
  12. $remote_addr
  13. $remote_port
  14. $remote_user
  15. $request_filename
  16. $request_uri
  17. $query_string
  18. $scheme
  19. $server_protocol
  20. $server_addr
  21. $server_name
  22. $server_port
  23. $uri

结合QeePHP的例子

  1. if (!-d $request_filename) {
  2. rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
  3. rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
  4. break;

多目录转成参数
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2

  1. if ($host ~* (.*).domain.com) {
  2. set $sub_name $1;   
  3. rewrite ^/sort/(d+)/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
  4. }

目录对换
/123456/xxxx -> /xxxx?id=123456

  1. rewrite ^/(d+)/(.+)/ /$2?id=$1 last;

例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:

  1. if ($http_user_agent ~ MSIE) {
  2. rewrite ^(.*)$ /nginx-ie/$1 break;
  3. }

目录自动加“/”

  1. if (-d $request_filename){
  2. rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
  3. }

禁止htaccess

  1. location ~/.ht {
  2.          deny all;
  3.      }

禁止多个目录

  1. location ~ ^/(cron|templates)/ {
  2.          deny all;
  3. break;
  4.      }

禁止以/data开头的文件
可以禁止/data/下多级目录下.log.txt等请求;

  1. location ~ ^/data {
  2.          deny all;
  3.      }

禁止单个目录
不能禁止.log.txt能请求

  1. location /searchword/cron/ {
  2.          deny all;
  3.      }

禁止单个文件

  1. location ~ /data/sql/data.sql {
  2.          deny all;
  3.      }

给favicon.ico和robots.txt设置过期时间;
这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志

  1. location ~(favicon.ico) {
  2.                  log_not_found off;
  3. expires 99d;
  4. break;
  5.      }
  6.  
  7.      location ~(robots.txt) {
  8.                  log_not_found off;
  9. expires 7d;
  10. break;
  11.      }

设定某个文件的过期时间;这里为600秒,并不记录访问日志

  1. location ^~ /html/scripts/loadhead_1.js {
  2.                  access_log   off;
  3.                  root /opt/lampp/htdocs/web;
  4. expires 600;
  5. break;
  6.        }

文件反盗链并设置过期时间
这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
“rewrite ^/ http://leech.c1gstudio.com/leech.gif;”显示一张防盗链图片
“access_log off;”不记录访问日志,减轻压力
“expires 3d”所有文件3天的浏览器缓存

  1. location ~* ^.+.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
  2. valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
  3. if ($invalid_referer) {
  4.     rewrite ^/ http://leech.c1gstudio.com/leech.gif;
  5.     return 412;
  6.     break;
  7. }
  8.                  access_log   off;
  9.                  root /opt/lampp/htdocs/web;
  10. expires 3d;
  11. break;
  12.      }

只充许固定ip访问网站,并加上密码

  1. root  /opt/htdocs/www;
  2. allow   208.97.167.194;
  3. allow   222.33.1.2;
  4. allow   231.152.49.4;
  5. deny    all;
  6. auth_basic “C1G_ADMIN”;
  7. auth_basic_user_file htpasswd;

将多级目录下的文件转成一个文件,增强seo效果
/job-123-456-789.html 指向/job/123/456/789.html

  1. rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+).html$ /job/$1/$2/jobshow_$3.html last;

将根目录下某个文件夹指向2级目录
如/shanghaijob/ 指向 /area/shanghai/
如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/

  1. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

上面例子有个问题是访问/shanghai 时将不会匹配

  1. rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
  2. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

这样/shanghai 也可以访问了,但页面中的相对链接无法使用,
如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。

那我加上自动跳转也是不行咯
(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果

  1. if (-d $request_filename){
  2. rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
  3. }

知道原因后就好办了,让我手动跳转吧

  1. rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;
  2. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

文件和目录不存在的时候重定向:

  1. if (!-e $request_filename) {
  2. proxy_pass http://127.0.0.1;
  3. }

域名跳转

  1. server
  2.      {
  3.              listen       80;
  4.              server_name  jump.c1gstudio.com;
  5.              index index.html index.htm index.php;
  6.              root  /opt/lampp/htdocs/www;
  7.              rewrite ^/ http://www.c1gstudio.com/;
  8.              access_log  off;
  9.      }

多域名转向

  1. server_name  www.c1gstudio.com www.c1gstudio.net;
  2.              index index.html index.htm index.php;
  3.              root  /opt/lampp/htdocs;
  4. if ($host ~ “c1gstudio.net”) {
  5. rewrite ^(.*) http://www.c1gstudio.com$1 permanent;
  6. }

三级域名跳转

  1. if ($http_host ~* “^(.*).i.c1gstudio.com$”) {
  2. rewrite ^(.*) http://top.yingjiesheng.com$1;
  3. break;
  4. }

域名镜向

  1. server
  2.      {
  3.              listen       80;
  4.              server_name  mirror.c1gstudio.com;
  5.              index index.html index.htm index.php;
  6.              root  /opt/lampp/htdocs/www;
  7.              rewrite ^/(.*) http://www.c1gstudio.com/$1 last;
  8.              access_log  off;
  9.      }

某个子目录作镜向

  1. location ^~ /zhaopinhui {
  2.   rewrite ^.+ http://zph.c1gstudio.com/ last;
  3.   break;
  4.      }

discuz ucenter home (uchome) rewrite

  1. rewrite ^/(space|network)-(.+).html$ /$1.php?rewrite=$2 last;
  2. rewrite ^/(space|network).html$ /$1.php last;
  3. rewrite ^/([0-9]+)$ /space.php?uid=$1 last;

discuz 7 rewrite

  1. rewrite ^(.*)/archiver/((fid|tid)-[w-]+.html)$ $1/archiver/index.php?$2 last;
  2. rewrite ^(.*)/forum-([0-9]+)-([0-9]+).html$ $1/forumdisplay.php?fid=$2&page=$3 last;
  3. rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;
  4. rewrite ^(.*)/profile-(username|uid)-(.+).html$ $1/viewpro.php?$2=$3 last;
  5. rewrite ^(.*)/space-(username|uid)-(.+).html$ $1/space.php?$2=$3 last;
  6. rewrite ^(.*)/tag-(.+).html$ $1/tag.php?name=$2 last;

给discuz某版块单独配置域名

  1. server_name  bbs.c1gstudio.com news.c1gstudio.com;
  2.  
  3.      location = / {
  4.         if ($http_host ~ news.c1gstudio.com$) {
  5.   rewrite ^.+ http://news.c1gstudio.com/forum-831-1.html last;
  6.   break;
  7. }
  8.      }

discuz ucenter 头像 rewrite 优化

  1. location ^~ /ucenter {
  2.      location ~ .*.php?$
  3.      {
  4.   #fastcgi_pass  unix:/tmp/php-cgi.sock;
  5.   fastcgi_pass  127.0.0.1:9000;
  6.   fastcgi_index index.php;
  7.   include fcgi.conf;     
  8.      }
  9.  
  10.      location /ucenter/data/avatar {
  11. log_not_found off;
  12. access_log   off;
  13. location ~ /(.*)_big.jpg$ {
  14.     error_page 404 /ucenter/images/noavatar_big.gif;
  15. }
  16. location ~ /(.*)_middle.jpg$ {
  17.     error_page 404 /ucenter/images/noavatar_middle.gif;
  18. }
  19. location ~ /(.*)_small.jpg$ {
  20.     error_page 404 /ucenter/images/noavatar_small.gif;
  21. }
  22. expires 300;
  23. break;
  24.      }
  25.                        }

jspace rewrite

  1. location ~ .*.php?$
  2.              {
  3.                   #fastcgi_pass  unix:/tmp/php-cgi.sock;
  4.                   fastcgi_pass  127.0.0.1:9000;
  5.                   fastcgi_index index.php;
  6.                   include fcgi.conf;     
  7.              }
  8.  
  9.              location ~* ^/index.php/
  10.              {
  11.     rewrite ^/index.php/(.*) /index.php?$1 break;
  12.                   fastcgi_pass  127.0.0.1:9000;
  13.                   fastcgi_index index.php;
  14.                   include fcgi.conf;
  15.              }

wordpress rewrite

  1. location / {
  2.         index index.html index.php;
  3.         if (-f $request_filename/index.html){
  4.             rewrite (.*) $1/index.html break;
  5.         }
  6.         if (-f $request_filename/index.php){
  7.             rewrite (.*) $1/index.php;
  8.         }
  9. if  (!-e $request_filename)
  10. {
  11.     rewrite (.*) /index.php;
  12. }
  13. }

UnixBench 5.1.2 of CheapVPS Basic by Cripperz Prodigy

20$/year VPS from Cripperz Prodigy:

 

The result of UnixBench 5.1.2:    1238.1

========================================================================
   BYTE UNIX Benchmarks (Version 5.1.2)

   System: Localhost: GNU/Linux
   OS: GNU/Linux — 2.6.18-238.12.1.el5.028stab091.1 — #1 SMP Wed Jun 1 13:20:2
5 MSD 2011
   Machine: i686 (unknown)
   Language: en_US.utf8 (charmap=”UTF-8″, collate=”UTF-8″)
   CPU 0: AMD Phenom(tm) II X4 965 Processor (6209.4 bogomips)
          Hyper-Threading, x86-64, MMX, Physical Address Ext, SYSENTER/SYSEXIT,
SYSCALL/SYSRET
   CPU 1: AMD Phenom(tm) II X4 965 Processor (6401.2 bogomips)
          Hyper-Threading, x86-64, MMX, Physical Address Ext, SYSENTER/SYSEXIT,
SYSCALL/SYSRET
   09:49:14 up  1:37,  1 user,  load average: 0.06, 0.04, 0.01; runlevel 2

————————————————————————
Benchmark Run: Sun Jun 19 2011 09:49:14 – 10:17:15
2 CPUs in system; running 1 parallel copy of tests

Dhrystone 2 using register variables       14247935.3 lps   (10.0 s, 7 samples)
Double-Precision Whetstone                     2522.3 MWIPS (9.9 s, 7 samples)
Execl Throughput                               2891.6 lps   (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks        666914.8 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks          212719.0 KBps  (30.1 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks        935761.0 KBps  (30.0 s, 2 samples)
Pipe Throughput                             1546190.6 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                 291698.3 lps   (10.0 s, 7 samples)
Process Creation                               7359.3 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                   4071.1 lpm   (60.0 s, 2 samples)
Shell Scripts (8 concurrent)                    591.7 lpm   (60.0 s, 2 samples)
System Call Overhead                        1473204.0 lps   (10.0 s, 7 samples)

System Benchmarks Index Values               BASELINE       RESULT    INDEX
Dhrystone 2 using register variables         116700.0   14247935.3   1220.9
Double-Precision Whetstone                       55.0       2522.3    458.6
Execl Throughput                                 43.0       2891.6    672.5
File Copy 1024 bufsize 2000 maxblocks          3960.0     666914.8   1684.1
File Copy 256 bufsize 500 maxblocks            1655.0     212719.0   1285.3
File Copy 4096 bufsize 8000 maxblocks          5800.0     935761.0   1613.4
Pipe Throughput                               12440.0    1546190.6   1242.9
Pipe-based Context Switching                   4000.0     291698.3    729.2
Process Creation                                126.0       7359.3    584.1
Shell Scripts (1 concurrent)                     42.4       4071.1    960.2
Shell Scripts (8 concurrent)                      6.0        591.7    986.2
System Call Overhead                          15000.0    1473204.0    982.1
                                                                   ========
System Benchmarks Index Score                                         964.4

————————————————————————
Benchmark Run: Sun Jun 19 2011 10:17:15 – 10:45:29
2 CPUs in system; running 2 parallel copies of tests

Dhrystone 2 using register variables       28325265.0 lps   (10.0 s, 7 samples)
Double-Precision Whetstone                     5068.7 MWIPS (9.7 s, 7 samples)
Execl Throughput                               4946.4 lps   (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks        277714.0 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks          103640.2 KBps  (30.0 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks        409976.7 KBps  (30.0 s, 2 samples)
Pipe Throughput                             2998921.2 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                 574050.8 lps   (10.0 s, 7 samples)
Process Creation                              13024.9 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                   7667.9 lpm   (60.0 s, 2 samples)
Shell Scripts (8 concurrent)                   1012.3 lpm   (60.1 s, 2 samples)
System Call Overhead                        2233174.8 lps   (10.0 s, 7 samples)

System Benchmarks Index Values               BASELINE       RESULT    INDEX
Dhrystone 2 using register variables         116700.0   28325265.0   2427.2
Double-Precision Whetstone                       55.0       5068.7    921.6
Execl Throughput                                 43.0       4946.4   1150.3
File Copy 1024 bufsize 2000 maxblocks          3960.0     277714.0    701.3
File Copy 256 bufsize 500 maxblocks            1655.0     103640.2    626.2
File Copy 4096 bufsize 8000 maxblocks          5800.0     409976.7    706.9
Pipe Throughput                               12440.0    2998921.2   2410.7
Pipe-based Context Switching                   4000.0     574050.8   1435.1
Process Creation                                126.0      13024.9   1033.7
Shell Scripts (1 concurrent)                     42.4       7667.9   1808.5
Shell Scripts (8 concurrent)                      6.0       1012.3   1687.2
System Call Overhead                          15000.0    2233174.8   1488.8
                                                                   ========
System Benchmarks Index Score                                        1238.1

lzheng@Localhost:~/unixbench-5.1.2$

 

 

 

VPS Ubuntu 10.04 config (1)

1. 新增日常管理的用户:   myname

           adduser  myname

2. 将用户加入sudo组:

      gpasswd -a myname sudo

3.  启用sudo功能:   visudo在/etc/sudoers文件里root那行后面添加入新加的管理用户:

      root ALL=(ALL) ALL
     myname ALL=(ALL) ALL

4.  重新以 myname 账户登入, 并测试 sudo 功能

         sudo myname

5. 禁用root账户

         sudo passwd -l root

6.  修改ubuntu的软件更新源:  

        sudo nano /etc/apt/sources.list

          deb     http://us.archive.ubuntu.com/ubuntu lucid main restricted universe multiverse
          deb     http://us.archive.ubuntu.com/ubuntu lucid-security main restricted universe multiverse
          deb     http://us.archive.ubuntu.com/ubuntu lucid-updates main restricted universe multiverse
          deb     http://us.archive.ubuntu.com/ubuntu lucid-proposed main restricted universe multiverse
          deb     http://us.archive.ubuntu.com/ubuntu lucid-backports main restricted universe multiverse
          deb-src http://us.archive.ubuntu.com/ubuntu lucid main restricted universe multiverse
          deb-src http://us.archive.ubuntu.com/ubuntu lucid-security main restricted universe multiverse
          deb-src http://us.archive.ubuntu.com/ubuntu lucid-updates main restricted universe multiverse
          deb-src http://us.archive.ubuntu.com/ubuntu lucid-proposed main restricted universe multiverse
          deb-src http://us.archive.ubuntu.com/ubuntu lucid-backports main restricted universe multiverse

7.  更新系统:

            sudo apt-get update

            sudo apt-get upgrade

8.  安装需要软件:

        sudo apt-get install language-pack-en language-pack-zh

        sudo apt-get install bash-completion

        sudo apt-get install vim ctags vim-doc vim-scripts

        sudo apt-get install screen byobu

        #安装压缩解压缩软件

        sudo apt-get install bzip2 unzip unrar p7zip

#安装基本的语言环境和开发包

       sudo apt-get install perl python python-dev ruby ruby-dev sqlite sqlite3 openssl

 #安装编译环境和开发包

       sudo apt-get install gcc g++ make autoconf automake patch gdb libtool cpp build-essential libc6-dev libncurses-dev expat

#安装第三方类库的开发包

      sudo apt-get install libbz2-dev libexpat1-dev libssl-dev libdb-dev libgmp3-dev   liblzo2-dev

#安装版本控制工具

      sudo apt-get install git-core subversion mercurial

Flowplayer

 

 

Flowplayer is an Open Source video player for the web. Embed the video streams into your web pages. User will get rich media experience by viewing video streams from your site. Player could be extended with Flash plug-ins, JavaScript plug-ins or Streaming plug-ins. Give a new look and feel to the player by extending it.

It could play the video irrespective of location. The video could be stored in any CDN network like Akamai, Amazon Cloudfront or even local web server. It has built-in slow motion and fast forward support. Social sharing is an important need for almost all web sites. Its viral plug-in helps to spread the videos to most popular social media.

It has ability to detect bandwidth speed and adjust the bit-rate transferred. It supports MPG, WMV, MOV and AVI formats.

 

http://flowplayer.org/

http://flowplayer.org/plugins/streaming/rtmp.html

 

http://code.google.com/p/flowplayer-plugins/source/checkout

svn checkout http://flowplayer-plugins.googlecode.com/svn/ flowplayer-plugins-read-only

 

 

Passing Variables from HTML to Flash in Adobe CS4

Suppose we need to pass the currnet URL from HTML to Flash:

 

1.   Add the following  ActionScript into FLA:

# #  myVIDEO is the parameter from HTML,  and we display it on screen

var my_fmt = new TextFormat();
my_fmt.color = 0xFF0000;
my_fmt.underline = true;
this.createTextField(“myTxt”, 1, 320, 10, 200, 20);
myTxt.autoSize = “left”;
myTxt.setTextFormat(my_fmt);

if (myVIDEO == undefined) {
myTxt.text = “Keynote_1.flv”;
} else {
myTxt.text = myVIDEO;
}

 

2.  Generate the SWF and HTML from FLA

 

 

3. Modify the generated HTML to add myVIDEO parameter,

   3.1 ==> Add the following line in the  AC_FL_RunContent block:

       ‘flashvars’,”,

 

   3.2 ==> Add the handle of flashvars in function AC_GetArgs, as following:

     case “flashvars”:
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = “myVIDEO=”+window.location;
        break;

  // Here myVIDEO the variable in AS2, and window.location is the variable in JavaScript indicating the current URL of HTML page