Caching 具有参数的特定url的Niginx位置和缓存
我只想将nginx缓存用于特定的url url为/ajax/airport,必须包含参数?geoloc=1 缓存工作正常,我面临的唯一问题是让它在包含给定参数的url上工作 这是我的nginx site.conf文件:Caching 具有参数的特定url的Niginx位置和缓存,caching,nginx,Caching,Nginx,我只想将nginx缓存用于特定的url url为/ajax/airport,必须包含参数?geoloc=1 缓存工作正常,我面临的唯一问题是让它在包含给定参数的url上工作 这是我的nginx site.conf文件: server { listen 80; server_name _; server_tokens off; location /ajax/airport.php {
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
谢谢你的帮助 只需使用指令测试$arg\u geoloc
(而不是$args\u geoloc
)的值,而不是if
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\u no\u cache
和proxy\u cache\u bypass
测试多个参数。如果您需要类似的功能,只需将参数一个接一个地放置:
proxy_no_cache $cookie_PHPSESSID $bypass_cache;
proxy_cache_bypass $cookie_PHPSESSID $bypass_cache;
谢谢你的回答。工作起来很有魅力!实际上,我想知道为什么我们需要这两台服务器(一台在:80上,另一台在:8080上)。难道不能使用一台服务器(:80)并在那里添加“/ajax/airport”位置吗?代理缓存工作是否需要代理权限?Thanks@LEM01我没有注意到您将代理服务器传递给另一个Nginx服务器。这确实很奇怪。是的,如果您想使用代理模块缓存某些内容,则需要代理传递,但您可以直接将代理传递到后端。好的,如果需要代理传递,我必须像以前一样设置2台服务器,对吗?事实上,我刚刚发现fastcgi_缓存似乎更适合我的要求,因为我不需要路由到另一台服务器。