symfony2通俗易懂手册

分类:Symfony2

TWIG 模板设计 快速入门手册 中文

http://blog.csdn.net/jiaochangyun/article/details/7180758

写了好几篇关于twig的东西。。居然还没写个快速入门之类的。现在就写

来源 http://twig.sensiolabs.org/doc/templates.html

概要

twig 的模板就是普通的文本文件,也不需要特别的扩展名,.html .htm .twig 都可以。

模板内的 变量 和 表达式 会在运行的时候被解析替换,标签(tags)会来控制模板的逻辑

下面是个最小型的模板,用来说明一些基础的东西

[html]view plaincopyprint?

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <title>My Webpage</title>  
  5.     </head>  
  6.     <body>  
  7.         <ul id=“navigation”>  
  8.         {% for item in navigation %}  
  9.             <li><a href=“{{ item.href }}”>{{ item.caption }}</a></li>  
  10.         {% endfor %}  
  11.         </ul>  
  12.   
  13.         <h1>My Webpage</h1>  
  14.         {{ a_variable }}  
  15.     </body>  
  16. </html>  

里面包含两种符号 {% … %} 和 {{ … }} 第一种用来控制的比如for循环什么的,第二个是用来输出变量和表达式的

ide 支持

很多ide 都对twig进行高亮支持。大伙自己找需要的吧。

变量

程序会传递给模板若干变量,你需要在模板里输出他们。例如输出 $hello

[html]view plaincopyprint?

  1. {{ hello }}  

如果传递给模板的是对象或者数组,你可以使用点 . 来输出对象的属性或者方法,或者数组的成员。或者你可以使用下标的方式。

[html]view plaincopyprint?

  1. {{ foo.bar }}  
  2. {{ foo[‘bar’] }}  

如果你访问的值不存在就会返回null。TWIG有一整套的流程来确认值是否存在。

for.bar会进行以下操作

。。。如果 foo是个数组,就尝试返回bar成员,如果不存在的话,往下继续

。。。如果foo是个对象,会尝试返回bar属性,如果不存在的话,往下继续

。。。会尝试运行bar方法,如果不存在的话,往下继续

。。。会尝试运行getBar方法,如果不存在的话,往下继续

。。。会尝试运行isBar方法,如果不存在的话,返回null

for[‘bar’] 就简单很多了 for必须是个数组,尝试返回bar成员,如果不就返回null

全局变量

TWIG定义了有一些全局变量

  • _self  这个参看macro标签
  • _context 这个就是当前的环境
  • _charset: 当前的字符编码

变量赋值

具体参见set标签

[html]view plaincopyprint?

  1. {% set foo = ‘foo’ %}  
  2. {% set foo = [1, 2] %}  
  3. {% set foo = {‘foo': ‘bar’} %}  

过滤器 Firters

变量可以被过滤器修饰。过滤器和变量用(|)分割开。过滤器也是可以有参数的。过滤器也可以被多重使用。

下面这例子就使用了两个过滤器。

[html]view plaincopyprint?

  1. {{ name|striptags|title }}  

striptas表示去除html标签,title表示每个单词的首字母大写。更多过滤器参见我博客

过滤器也可以用在代码块中,参见 filter标签

[html]view plaincopyprint?

  1. {% filter upper %}  
  2.   This text becomes uppercase  
  3. {% endfilter %}  

函数 Function

这个没什么好说的,会写程序的都知道,TWIG内置了一些函数,参考我的博客

举个例子 返回一个0到3的数组,就使用 range函数

[html]view plaincopyprint?

  1. {% for i in range(0, 3) %}  
  2.     {{ i }},  
  3. {% endfor %}  

流程控制

支持for循环 和 if/elseif/else结构。直接看例子吧,没什么好说的。

[html]view plaincopyprint?

  1. <h1>Members</h1>  
  2. <ul>  
  3.     {% for user in users %}  
  4.         <li>{{ user.username|e }}</li>  
  5.     {% endfor %}  
  6. </ul>  

[html]view plaincopyprint?

  1. {% if users|length > 0 %}  
  2.     <ul>  
  3.         {% for user in users %}  
  4.             <li>{{ user.username|e }}</li>  
  5.         {% endfor %}  
  6.     </ul>  
  7. {% endif %}  

注释

{# … #} 包围的内容会被注释掉,可以是单行 也可以是多行。

载入其他模板

详见include标签(我博客内已经翻译好哦),会返回经过渲染的内容到当前的模板里

[html]view plaincopyprint?

  1. {% include ‘sidebar.html’ %}  

当前模板的变量也会传递到 被include的模板里,在那里面可以直接访问你这个模板的变量。

比如

[html]view plaincopyprint?

  1. {% for box in boxes %}  
  2.     {% include “render_box.html” %}  
  3. {% endfor %}  

在 render_box.html 是可以访问 box变量的
加入其他参数可以使被载入的模板只访问部分变量,或者完全访问不到。参考手册

模板继承

TWIG中最有用到功能就是模板继承,他允许你建立一个“骨骼模板”,然后你用不同到block来覆盖父模板中任意到部分。而且使用起来非常到简单。

我们先定义一个基本骨骼页base.html 他包含许多block块,这些都可以被子模板覆盖。

[html]view plaincopyprint?

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         {% block head %}  
  5.             <link rel=“stylesheet” href=“style.css” />  
  6.             <title>{% block title %}{% endblock %} – My Webpage</title>  
  7.         {% endblock %}  
  8.     </head>  
  9.     <body>  
  10.         <div id=“content”>{% block content %}{% endblock %}</div>  
  11.         <div id=“footer”>  
  12.             {% block footer %}  
  13.                 © Copyright 2011 by <a href=“http://domain.invalid/”>you</a>.  
  14.             {% endblock %}  
  15.         </div>  
  16.     </body>  
  17. </html>  

我们定义了4个block块,分别是 block head, block title, block content, block footer

注意

1、block是可以嵌套的。

2、block可以设置默认值(中间包围的内容),如果子模板里没有覆盖,那就直接显示默认值。比如block footer ,大部分页面你不需要修改(省力),但你需要到时候仍可以方便到修改(灵活)

下面我看下 子模板应该怎么定义。

[html]view plaincopyprint?

  1. {% extends “base.html” %}  
  2.   
  3. {% block title %}Index{% endblock %}  
  4. {% block head %}  
  5.     {{ parent() }}  
  6.     <style type=“text/css”>  
  7.         .important { color: #336699; }  
  8.     </style>  
  9. {% endblock %}  
  10. {% block content %}  
  11.     <h1>Index</h1>  
  12.     <p class=“important”>  
  13.         Welcome on my awesome homepage.  
  14.     </p>  
  15. {% endblock %}  

注意 {% extends “base.html” %} 必须是第一个标签。其中 block footer就没有定义,所以显示父模板中设置的默认值

如果你需要增加一个block的内容,而不是全覆盖,你可以使用 parent函数

[html]view plaincopyprint?

  1. {% block sidebar %}  
  2.     <h3>Table Of Contents</h3>  
  3.     …  
  4.     {{ parent() }}  
  5. {% endblock %}  

extends标签只能有一个,所以你只能有一个父模板,但有种变通到方法来达到重用多个模板到目的,具体参见手册的use标签

HTML转义

主要是帮助转义 尖括号等  <, >,  &,  ”  可以有两种办法。一种是用标签,另一种是使用过滤器。其实TWIG内部就是调用 php 的htmlspecialchars 函数

[html]view plaincopyprint?

  1. {{ user.username|e }}  
  2. {{ user.username|e(‘js’) }}  
  3.   
  4. {% autoescape true %}  
  5.     Everything will be automatically escaped in this block  
  6. {% endautoescape %}  

因为{{是TWIG的操作符,如果你需要输出两个花括号,最简单到办法就是

[html]view plaincopyprint?

  1. {{ ‘{{‘ }}  

还可以使用 raw 标签和raw 过滤器,详细参考手册

[html]view plaincopyprint?

  1. {% raw %}  
  2.     <ul>  
  3.     {% for item in seq %}  
  4.         <li>{{ item }}</li>  
  5.     {% endfor %}  
  6.     </ul>  
  7. {% endraw %}  

macros

宏有点类似于函数,常用于输出一些html标签。

这里有个简单示例,定义了一个输出input标签的宏。

[html]view plaincopyprint?

  1. {% macro input(name, value, type, size) %}  
  2.     <input type=“{{ type|default(‘text’) }}” name=“{{ name }}” value=“{{ value|e }}” size=“{{ size|default(20) }}” />  
  3. {% endmacro %}  

宏参数是没有默认值的,但你可以通过default过滤器来实现。

一般来说宏会定义在其他到页面,然后通过import标签来导入,

[html]view plaincopyprint?

  1. {% import “forms.html” as forms %}  
  2.   
  3. <p>{{ forms.input(‘username’) }}</p>  

你也可以只导入一个文件中部分宏,你还可以再重命名。

[html]view plaincopyprint?

  1. {% from ‘forms.html’ import input as input_field, textarea %}  
  2.   
  3. <dl>  
  4.     <dt>Username</dt>  
  5.     <dd>{{ input_field(‘username’) }}</dd>  
  6.     <dt>Password</dt>  
  7.     <dd>{{ input_field(‘password’, type=‘password’) }}</dd>  
  8. </dl>  
  9. <p>{{ textarea(‘comment’) }}</p>  

上面的代码表示 从forms.html中导入了 input 和 textarea宏,并给input重命名为input_field。

表达式

TWIG允许你在任何地方使用表达式,他的规则和PHP几乎一模一样,就算你不会PHP 仍然会觉得很简单。

最简单的有

字符串:“hello world”  或者 ‘hello world’

数字:42 或者 42.33

数组:[‘a’,’b’,’c’]

哈希:{‘a':’av’, ‘b':’bv’} 其中keys 可以不要引号 也可以是数字 还可以是一个表达式,比如{a:’av’, b:’bv’}  {1:’1v’, 2:’2v’}  {1+2:’12v’}

逻辑: true 或者 false

最后还有null

你可以嵌套定义

[html]view plaincopyprint?

  1. {% set foo = [1, {“foo”: “bar”}] %}  

运算符

包括数字运算+ – * /  %(求余数)  //(整除) **(乘方)

[html]view plaincopyprint?

  1. <p>{{ 2 * 3 }}=6  
  2. <p>{{ 2 * 3 }}=8  

逻辑运算 and or  not

比较运算 > < >= <= == !=

包含运算 in 以下的代码会返回 true

[html]view plaincopyprint?

  1. {{ 1 in [1, 2, 3] }}  
  2. {{ ‘cd’ in ‘abcde’ }}  

测试运算 is 这个不用多说 直接看代码

[html]view plaincopyprint?

  1. {{ name is odd }}  
  2. {% if loop.index is divisibleby(3) %}  
  3. {% if loop.index is not divisibleby(3) %}  
  4. {# is equivalent to #}  
  5. {% if not (loop.index is divisibleby(3)) %}  

其他操作符

.. 建立一个指定开始到结束的数组,他是range函数的缩写,具体参看手册

[html]view plaincopyprint?

  1. <pre name=“code” class=“html”>{% for i in 0..3 %}  
  2.     {{ i }},  
  3. {% endfor %}</pre>  
  4. <pre></pre>  

| 使用一个过滤器

[html]view plaincopyprint?

  1. {# output will be HELLO #}  
  2. {{ “hello”|upper }}  

~ 强制字符串连接

[html]view plaincopyprint?

  1. {{ “Hello ” ~ name ~ “!” }}  

?:  三元操作符

[html]view plaincopyprint?

  1. {{ foo ? ‘yes’ : ‘no’ }}  

. [] 得到一个对象的属性,比如以下是相等的。

[html]view plaincopyprint?

  1. {{ foo.bar }}  
  2. {{ foo[‘bar’] }}  

你还可以在一个字符串内部插入一个表达式,通常这个表达式是变量。 格式是 #{表达式}

[html]view plaincopyprint?

  1. {{ “foo #{bar} baz” }}  
  2. {{ “foo #{1 + 2} baz” }}  

空白控制

和 php一样,在TWIG模板标签之后的第一个换行符会被自动删掉,其余的空白(包括 空格 tab 换行等)都会被原样输出。

使用spaceless标签就可以删除这些HTML标签之间的空白

[html]view plaincopyprint?

  1. {% spaceless %}  
  2.     <div>  
  3.         <strong>foo</strong>  
  4.     </div>  
  5. {% endspaceless %}  
  6.   
  7. {# output will be <div><strong>foo</strong></div> #}  

使用-操作符,可以很方便的删除TWIG标签之前或之后与html标签之间的空白。

[html]view plaincopyprint?

  1. {% set value = ‘no spaces’ %}  
  2. {#- No leading/trailing whitespace -#}  
  3. {%- if true -%}  
  4.     {{- value -}}  
  5. {%- endif -%}  
  6.   
  7. {# output ‘no spaces’ #}  

[html]view plaincopyprint?

  1. {% set value = ‘no spaces’ %}  
  2. <li>    {{- value }}    </li>  
  3.   
  4. {# outputs ‘<li>no spaces    </li>‘ #}  

结束,如果你坚持看到这里,恭喜自己吧,你又多掌握了一些知识,恭喜恭喜

更多0

发布于 2014/05/25分类 Symfony2

让symfony运行在nginx上的完美配置

 

http://hi.baidu.com/chrisyue/blog/item/6f35b9fb7a76ae2e4e4aea5f.html

让symfony运行在nginx上的完美配置

http://wiki.nginx.org/Symfony

http://www.ccvita.com/319.html

 

server {

listen      80; #nginx的端口
server_name www.test.local; #网站网址,这里以localhost为例
root       /var/www/test/web; #根目录路径
chareset    utf-8;
location /opi/ { #如果url匹配”/sf/”
alias /var/www/test/src/Opi; #访问/sf下面的资源所在的目录
}
location ~ ^/.+\.php(/.*)?$ { #如果匹配类似/frontend_dev.php/*这样的
set $script $fastcgi_script_name; #像/frontend_dev.php这样的url,直接把$fastcgi_script_name赋值给$script
if ($document_uri ~ ^(.+\.php)(/.*)) { #如果又匹配/frontend_dev.php/article/1这样的
set $script    $1; #把frontend_dev.php赋值给$script
set $path_info $2; #把/article/1赋值给$path_info,如果用nginx默认的配置,$_SERVER[‘path_info’]会不正确而导致symfony找不到正确的路由
}
fastcgi_pass  127.0.0.1:9000; #php-cgi服务
#下面开始给php的$_SERVER相关变量赋值,不写对都有可能运行不了symfony
include       fastcgi_params; #这里面有很多nginx默认给php的$_SERVER赋的环境变量值,
#如果你想要把这句放后面的话,下面已经赋过值的记得注释掉,否则又被覆盖回原来的错误值
fastcgi_param SCRIPT_FILENAME $document_root$script;
fastcgi_param SCRIPT_NAME     $script;
fastcgi_param PATH_INFO       $path_info;
fastcgi_param DOCUMENT_URI    $document_uri;

}
location / { #当前面的规则都不匹配,就运行这个(剩下的url肯定都匹配/)
if (-f $request_filename) { #如果资源(像css,js这样的文件)和请求的url是匹配的,也就是说能根据url直接找到
expires max; #那就永不过期
break;       #并且不往下执行了
}
#否则得话都给他rewrite到index.php上去
rewrite .* /index.php last;
}

}

 

 

 

 

http://www.ccvita.com/319.html

 

Nginx的Rewrite
经过网上查阅和测试,发现Nginx的Rewrite规则和Apache的Rewite规则差别不是很大,几乎可以直接使用。比如在Apache中这样写规则
rewrite ^/([0-9]{5}).html$ /viewthread.php?tid=$1 last;
而在Nginx中写成这样写是无法启动的,解决的办法是加上两个双引号:
rewrite “^/([0-9]{5}).html$” /viewthread.php?tid=$1 last;
同时将RewriteRule为Rewrite,基本就实现了Nginx的Rewrite规则到Apache的Rewite规则的转换。

Rewrite的Flags
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301

 

官方文档请点击这里,另外如果对于302,301这些状态有疑问的,可以参考《301 Redirect 永久重定向的实现》:http://www.ccvita.com/85.html
如果需要对Nginx配置防盗链的话,可以参考《Nginx的防盗链配置》:http://www.ccvita.com/312.html

Discuz!在Nginx下的Rewrite
需要说明的是,下网上以前一直流传的Rewrite都是有误的。
下面的Rewrite中百分号前面多了个转移字符“\”,这在Apache中是需要的,而在Nginx中则是不需要的。
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last;
正确的应该是
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
这个错误在基本上目前所有使用Nginx作为服务器,并且开启了Rewrite的网站上存在。包括Discuz!官方,目前已经给cnteacher反馈了。

完整正确的Discuz!在Nginx下的Rewrite如下:
rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last;
rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last;
rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
break;

http://blog.csdn.net/cnbird2008/article/details/4409620

 

 

nginx rewrite 参数和例子

 

 

http://www.cnblogs.com/analyzer/articles/1377684.html

]

本位转自:http://blog.c1gstudio.com/archives/434

推荐参考地址:
Mailing list ARChives 官方讨论区
http://marc.info/?l=nginx

Nginx 常见应用技术指南[Nginx Tips]
http://bbs.linuxtone.org/thread-1685-1-1.html

本日志内容来自互联网和平日使用经验,整理一下方便日后参考。

正则表达式匹配,其中:

  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&amp;controller=$1&amp;action=$2&amp;$3 last;
  3. rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&amp;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.              }

 

 

 

 

 

 

posted @ 2016-02-28 20:30  猪啊美  阅读(309)  评论(0编辑  收藏  举报