关于nginx lua中的if和rewrite中的if使用问题

305 views
Skip to first unread message

杨进

unread,
Jul 16, 2013, 9:38:12 PM7/16/13
to open...@googlegroups.com
nginx lua中可以使用if then else语句,它能在location外使用吗?还有在nginx中如果只使用lua的if语句而不使用rewrite模块中的if语句,这样在nginx配置中可以吗?是不是要用到lua中的if只有在nginx配置调用lua的语句的时候才能用到?


Yichun Zhang (agentzh)

unread,
Jul 17, 2013, 12:54:23 AM7/17/13
to openresty
Hello!

2013/7/16 杨进:
> nginx lua中可以使用if then
> else语句,它能在location外使用吗?

这是什么意思?Lua 的 if 语句只能在 Lua 代码中使用,和 nginx 的 location 没有关系。或者你是指下面这种用法?

server {
rewrite_by_lua 'if ... then ... else ... end';
location ... { ... }
}

你能表述得更具体一些么?

> 还有在nginx中如果只使用lua的if语句而不使用rewrite模块中的if语句,这样在nginx配置中可以吗?

为什么不可以?这是推荐的做法。因为 ngx_rewrite 模块的 if 有很多使用上的陷讲,见这里:

http://wiki.nginx.org/IfIsEvil

> 是不是要用到lua中的if只有在nginx配置调用lua的语句的时候才能用到?
>

是的,当然。

nginx.conf 配置文件有自己语法,独立于 Lua 语言。推荐阅读我的 nginx 连载教程:

http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

Regards,
-agentzh

杨进

unread,
Jul 17, 2013, 2:12:38 AM7/17/13
to open...@googlegroups.com
您好!
      是这样的,在配置nginx泛域名的时候,对域名的日志是否记录进行设置,如果是img的二级域名就不做记录,其他的域名的日志就记录下来
location / {
if ($domain ~ ^img$){
access_log off;
}
access_log /home/Www/logs/$http_host.log access;
proxy_pass http://CAHospital;
}
这样写在location里是可以的,写在location外面就不行了,就想lua能支持if then语句,可不可以实现这个功能?请指点!谢谢!




>
>Regards,
>-agentzh
>
>-- 
>-- 
>邮件来自列表“openresty”,专用于技术讨论!
>订阅: 请发空白邮件到 openresty...@googlegroups.com
>发言: 请发邮件到 open...@googlegroups.com
>退订: 请发邮件至 openresty+...@googlegroups.com
>归档: http://groups.google.com/group/openresty
>官网: http://openresty.org/
>仓库: https://github.com/agentzh/ngx_openresty
>教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
>
>


Yichun Zhang (agentzh)

unread,
Jul 17, 2013, 2:59:00 PM7/17/13
to openresty
Hello!

2013/7/16 杨进:
> 是这样的,在配置nginx泛域名的时候,对域名的日志是否记录进行设置,如果是img的二级域名就不做记录,其他的域名的日志就记录下来
> location / {
> if ($domain ~ ^img$){
> access_log off;
> }
> access_log /home/Www/logs/$http_host.log access;
> proxy_pass http://CAHospital;
> }
> 这样写在location里是可以的,写在location外面就不行了,就想lua能支持if then语句,可不可以实现这个功能?请指点!谢谢!
>

由于 access_log 配置指令并不支持 nginx 变量,所以你只能通过配置多个 location 来实现(无论你是否使用
Lua)。你上面那个使用 if 指令的例子其实也是在 if 的语句块里隐式创建了一个匿名嵌套的 location.

Regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages