nginx:我怎样才能设置proxy_ *指令只为匹配的URI?

我已经在这个几个小时了,我找不出一个干净的解决scheme。

基本上,我有一个nginx代理设置,这工作得很好,但我想手动处理几个url。 具体来说,有2-3个位置,我想设置proxy_ignore_headers Set-Cookie来强制nginxcaching它们(nginx不caching响应与Set-Cookie按http://wiki.nginx.org/ HttpProxyModule#proxy_ignore_headers )。

所以对于这些位置,我想要做的就是设置proxy_ignore_headers Set-Cookie;

我已经尝试了所有我能想到的设置和复制每个configuration值之外的任何东西,但没有任何工作。

我试过了:

  • 嵌套的位置指令,希望与我的文件相匹配的内部位置只是设置这个值,并inheritance其余的,但事实并非如此 – 它似乎忽略任何设置在外部位置,最值得注意的是proxy_pass和我最终404)。
  • 在$ request_uri匹配的if块中指定proxy_cache_valid指令,但是nginx抱怨不允许(这里不允许使用“ proxy_cache_valid ”指令)。
  • 在if块中指定一个等于“Set-Cookie”的variables,然后尝试将proxy_cache_valid设置为该variables,但是nginx不允许这种情况下的variables并抛出。

它应该是如此简单 – 修改/附加一个指令的一些请求,但我还没有能够让nginx做到这一点。

我在这里错过了什么?

是否至less有一种方法来包装可重复使用的块中的通用指令,并添加自己的唯一位后,有多个位置块引用它?

谢谢。

仅供参考,主要位置/块包含在下面,以及针对特定URI的失败的proxy_ignore_headers指令。

 location / { # Setup var defaults set $no_cache ""; # If non GET/HEAD, don't cache & mark user as uncacheable for 1 second via cookie if ($request_method !~ ^(GET|HEAD)$) { set $no_cache "1"; } if ($http_user_agent ~* '(iphone|ipod|ipad|aspen|incognito|webmate|android|dream|cupcake|froyo|blackberry|webos|s8000|bada)') { set $mobile_request '1'; set $no_cache "1"; } # feed crawlers, don't want these to get stuck with a cached version, especially if it caches a 302 back to themselves (infinite loop) if ($http_user_agent ~* '(FeedBurner|FeedValidator|MediafedMetrics)') { set $no_cache "1"; } # Drop no cache cookie if need be # (for some reason, add_header fails if included in prior if-block) if ($no_cache = "1") { add_header Set-Cookie "_mcnc=1; Max-Age=17; Path=/"; add_header X-Microcachable "0"; } # Bypass cache if no-cache cookie is set, these are absolutely critical for WordPress installations that don't use JS comments if ($http_cookie ~* "(_mcnc|comment_author_|wordpress_(?!test_cookie)|wp-postpass_)") { set $no_cache "1"; } if ($request_uri ~* wpsf-(img|js)\.php) { proxy_ignore_headers Set-Cookie; } # Bypass cache if flag is set proxy_no_cache $no_cache; proxy_cache_bypass $no_cache; # under no circumstances should there ever be a retry of a POST request, or any other request for that matter proxy_next_upstream off; proxy_read_timeout 86400s; # Point nginx to the real app/web server proxy_pass http://localhost; # Set cache zone proxy_cache microcache; # Set cache key to include identifying components proxy_cache_key $scheme$host$request_method$request_uri$mobile_request; # Only cache valid HTTP 200 responses for this long proxy_cache_valid 200 15s; #proxy_cache_min_uses 3; # Serve from cache if currently refreshing proxy_cache_use_stale updating timeout; # Send appropriate headers through proxy_set_header Host $host; # no need for this proxy_set_header X-Real-IP $remote_addr; # no need for this proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # Set files larger than 1M to stream rather than cache proxy_max_temp_file_size 1M; access_log /var/log/nginx/androidpolice-microcache.log custom; } 

在尝试了10个不同的东西,并与http://fennb.com的 Fenn Bailey进行了咨询之后,我不得不求助于包含解决scheme,这似乎是避免代码重复和实际工作的唯一方法。

我将位置/ {}中的所有指令移至其自己的文件中,并执行以下操作:

 location / { include /etc/nginx/vhosts/androidpolice-root.conf; } location ~* wpsf-(img|js)\.php { include /etc/nginx/vhosts/androidpolice-root.conf; proxy_ignore_headers "Set-Cookie" "Cache-Control"; } 

在这一点上,对于if和条件,nginx的灵活性让我感到非常震惊 – 这些事情应该是如此微不足道,但事实并非如此。

顺便说一下,我正在使用最新的nginx 1.1.17。