我想只为特定的网址使用nginx缓存 网址是/ ajax / airport,必须包含参数?geoloc = 1。
缓存工作正常,我面临的唯一问题是让它适用于包含给定参数的网址。
这是我的nginx site.conf文件:
server {
listen 80;
server_name _;
server_tokens off;
location /ajax/airport.php {
if ($args_geoloc = 1) {
proxy_pass http://127.0.0.1:8080/ajax/airport.php;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache my-cache;
proxy_cache_valid 300s;
#proxy_no_cache $cookie_PHPSESSID;
#proxy_cache_bypass $cookie_PHPSESSID;
proxy_cache_key "$scheme$host$request_uri";
add_header X-Cache $upstream_cache_status;
add_header LEM airport;
}
}
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header LEM all;
}
}
server {
listen 8080;
.. usual location handeling ...
我得到的错误:
nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/sites-enabled/site.com.conf:8
感谢您的帮助!
答案 0 :(得分:1)
只需使用proxy_no_cache和proxy_cache_bypass代替if
,使用map指令测试$arg_geoloc
(不是$args_geoloc
)的值。< / p>
map $arg_geoloc $bypass {
default 1;
1 0;
}
server {
...
location /ajax/airport.php {
...
proxy_no_cache $bypass;
proxy_cache_bypass $bypass;
...
# No need to add /ajax/airport.php in proxy_pass
proxy_pass http://127.0.0.1:8080;
}
...
}
Nginx还允许使用proxy_no_cache
和proxy_cache_bypass
测试多个参数。如果您需要这样的东西,只需将参数依次放入:
proxy_no_cache $cookie_PHPSESSID $bypass_cache;
proxy_cache_bypass $cookie_PHPSESSID $bypass_cache;