nginx里面的变量判断问题

189 views
Skip to first unread message

Jones Zhong

unread,
Oct 19, 2013, 8:30:40 AM10/19/13
to open...@googlegroups.com
我写了一个配置文件,判断是 if ($upstream_http_content_type ^* "text") 但是却死活不奏效,我把这个变量打印出来看发现是正常(text/css)的。

然后我就怎么也想不通是什么问题了。

然后我又手贱去试了一下  if ($upstream_http_content_type = "")  ,却发现是true!!!!!!

是不是不是所有的nginx变量都可以放到if里面去的啊?请教各位大牛~~感谢~~

Weibin Yao

unread,
Oct 19, 2013, 8:36:50 AM10/19/13
to open...@googlegroups.com
On 2013/10/19 20:30, Jones Zhong wrote:
��д��һ�������ļ����ж��� if ($upstream_http_content_type ^* "text") ����ȴ�����Ч���Ұ����������ӡ��������������(text/css)�ġ�

Ȼ���Ҿ���ôҲ�벻ͨ��ʲô�����ˡ�

Ȼ�������ּ�ȥ����һ��  if ($upstream_http_content_type = "")  ��ȴ������true������������

�Dz��Dz������е�nginx���������Էŵ�if����ȥ�İ�����̸�λ��ţ~~��л~~
Nginx������ĸ�������׶Σ�����ִ�� (http://tengine.taobao.org/book/chapter_02.html#id10)��

Rewrite
Access
Content
Log

if��Ӧ����Rewrite�׶Ρ�$upstream_xxx�����������Contentִ�н׶��õ������Ӧ�����ݲſ���ʹ�ã��������� Rewrite�׶����ò��������ģ�һ��ֻ���ڼ�¼������־��Log�׶β����õõ���

-- 
Weibin Yao
Thanks.

Jones Zhong

unread,
Oct 19, 2013, 8:57:19 AM10/19/13
to open...@googlegroups.com
斌哥。。乱码了。。


在 2013年10月19日星期六UTC+8下午8时36分50秒,Weibin Yao写道:
On 2013/10/19 20:30, Jones Zhong wrote:
锟斤拷写锟斤拷一锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟叫讹拷锟斤拷 if ($upstream_http_content_type ^* "text") 锟斤拷锟斤拷却锟斤拷锟筋不锟斤拷效锟斤拷锟揭帮拷锟斤拷锟斤拷锟斤拷锟斤拷锟接★拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�(text/css)锟侥★拷

然锟斤拷锟揭撅拷锟斤拷么也锟诫不通锟斤拷什么锟斤拷锟斤拷锟剿★拷

然锟斤拷锟斤拷锟斤拷锟街硷拷去锟斤拷锟斤拷一锟斤拷  if ($upstream_http_content_type = "")  锟斤拷却锟斤拷锟斤拷锟斤拷true锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

锟角诧拷锟角诧拷锟斤拷锟斤拷锟叫碉拷nginx锟斤拷锟斤拷锟斤拷锟斤拷锟皆放碉拷if锟斤拷锟斤拷去锟侥帮拷锟斤拷锟斤拷谈锟轿伙拷锟脚~锟斤拷谢~~
Nginx锟斤拷锟斤拷锟斤拷母锟斤拷锟斤拷锟斤拷锟阶段o拷锟斤拷锟斤拷执锟斤拷 (http://tengine.taobao.org/book/chapter_02.html#id10)锟斤拷

Rewrite
Access
Content
Log

if锟斤拷应锟斤拷锟斤拷Rewrite锟阶段★拷$upstream_xxx锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟紺ontent执锟叫阶讹拷锟矫碉拷锟斤拷锟斤拷锟接︼拷锟斤拷锟斤拷莶趴锟斤拷锟绞癸拷茫锟斤拷锟斤拷锟斤拷锟斤拷锟� Rewrite锟阶讹拷锟斤拷锟矫诧拷锟斤拷锟斤拷锟斤拷锟侥o拷一锟斤拷只锟斤拷锟节硷拷录锟斤拷锟斤拷锟斤拷志锟斤拷Log锟阶段诧拷锟斤拷锟矫得碉拷锟斤拷

-- 
Weibin Yao
Thanks.

Weibin Yao

unread,
Oct 19, 2013, 9:44:18 AM10/19/13
to open...@googlegroups.com
On 2013/10/19 20:30, Jones Zhong wrote:
Nginx有请求的各个处理阶段,依次执行 (http://tengine.taobao.org/book/chapter_02.html#id10):

Rewrite
Access
Content
Log

if对应的是Rewrite阶段。$upstream_xxx这个变量是在Content执行阶段拿到后端响应的内容才可以使用,所以它在 Rewrite阶段是拿不到变量的,一般只能在记录访问日志的Log阶段才能用得到。

-- 
Weibin Yao
Thanks.

Jones Zhong

unread,
Oct 19, 2013, 10:36:19 AM10/19/13
to open...@googlegroups.com
恍然大悟!非常感谢。



在 2013年10月19日星期六UTC+8下午9时44分18秒,Weibin Yao写道:

Jones Zhong

unread,
Oct 19, 2013, 8:11:13 PM10/19/13
to open...@googlegroups.com
不好意思,又有一个问题,想再问一下,如果用map的惰性求值

map $upstream_http_content_type $token {
                default       $http_cookie;
                ~*^text/      0;
        }

能否完成这个需求,在content阶段执行判断?
因为我看这个map是在用到时候才执行。


在 2013年10月19日星期六UTC+8下午8时36分50秒,Weibin Yao写道:
On 2013/10/19 20:30, Jones Zhong wrote:

Yichun Zhang (agentzh)

unread,
Oct 19, 2013, 9:49:48 PM10/19/13
to openresty
Hello!

2013/10/19 Jones Zhong:
> 不好意思,又有一个问题,想再问一下,如果用map的惰性求值
>
> map $upstream_http_content_type $token {
> default $http_cookie;
> ~*^text/ 0;
> }
>
> 能否完成这个需求,在content阶段执行判断?
> 因为我看这个map是在用到时候才执行。
>

你在 content 阶段读取你这个 $token 变量,就会在 content 阶段执行映射操作了。这不是很自然的么?

当然,值得提醒的是,$upstream_http_content_type 变量只有在当前请求接收到了后端响应的响应头之后,才会有值。

Regards,
-agentzh

Jones Zhong

unread,
Oct 19, 2013, 10:36:15 PM10/19/13
to open...@googlegroups.com

明白了。。感谢春哥。。


在 2013年10月20日星期日UTC+8上午9时49分48秒,agentzh写道:

Jones Zhong

unread,
Oct 20, 2013, 6:52:39 AM10/20/13
to open...@googlegroups.com
我想再问个问题,还是有关这个问题的。并且找到一个讨论 http://nginx.2469901.n2.nabble.com/Cache-by-mime-type-td7580925.html
后来我使用proxy_no_cache的时候,是可以奏效的。
而使用proxy_cache_bypass的时候,是无效的。
我猜测proxy_cache_bypass在upstream前执行,而proxy_no_cache在upstream后执行。
然而我在使用proxy_no_cache的时候,nginx会警告最好和proxy_cache_bypass一起用。但是这样一来,map是有缓存特性的,会因为先执行的proxy_cache_bypass而固化成一个空值,请问应该如何处理,是无视警告继续单独用proxy_no_cache吗?





在 2013年10月20日星期日UTC+8上午10时36分15秒,Jones Zhong写道:

Yichun Zhang (agentzh)

unread,
Oct 20, 2013, 6:10:26 PM10/20/13
to openresty
Hello!

2013/10/20 Jones Zhong:
> 后来我使用proxy_no_cache的时候,是可以奏效的。
> 而使用proxy_cache_bypass的时候,是无效的。

proxy_cache_bypass 是在 ngx_http_upstream_init_request
函数中检查的(即在给后端发送请求之前)。这条配置指令是用来控制 nginx 是否去检查 cache 里面是否有命中。如果 cache
命中的话,proxy 模块就压根不会去访问后端了。

proxy_no_cache 是在 ngx_http_upstream_send_response
函数中检查的(即在接收到后端的响应头以后)。这条配置指令是用来控制是否把后端的响应写入 cache,所以此时后端已经访问过了。

你的 map 计算是基于后端的响应头,所以你只能使用 proxy_no_cache. 因为在检查 proxy_cache_bypass
的配置时,后端还没有访问呢,自然不会有后端的响应头。

> 我猜测proxy_cache_bypass在upstream前执行,而proxy_no_cache在upstream后执行。

它们其实都是 ngx_http_upstream 内部实现的一部分。这里对于你来说重要的是相对于接收到后端响应头这个时间点的先后。

> 然而我在使用proxy_no_cache的时候,nginx会警告最好和proxy_cache_bypass一起用。

简单忽略掉警告好了。当然,你也可以把 proxy_cache_bypass 设为永真,即

proxy_cache_bypass 1;

这只是会让 proxy 模块对于所有请求都在访问后端之前先检查一下 cache 里是否有命中,无所谓了。

Regards,
-agentzh

Jones Zhong

unread,
Oct 21, 2013, 2:57:51 AM10/21/13
to open...@googlegroups.com
明白了,非常感谢春哥的回复~~

在 2013年10月21日星期一UTC+8上午6时10分26秒,agentzh写道:
Reply all
Reply to author
Forward
0 new messages