带有proxy_cache的Nginx ssi在第一次请求后挂起

我正在为我们的dynamic页面使用Nginx的代理caching,并且最近已经集成了ssi。 首页加载工作正常,但一旦页面被caching,另一个请求通过页面刚刚挂起。

日志似乎表明正在做多个子请求(只有一个指令,并在布局),我不知道为什么发生这种情况。 页面在第一次加载时加载正常,caching的版本旋转其轮子并打破。 这是我的configuration。

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=500m inactive=60m; #caching proxy_temp_path /var/tmp; #caching gzip_comp_level 6; gzip_vary on; gzip_min_length 1000; gzip_proxied any; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_buffers 16 8k; upstream staging { server 127.0.0.1:1337; } server { listen 0.0.0.0:80; server_name dev.example.com; access_log /var/log/nginx/dev.example.log; error_log /var/log/nginx/dev.example.error.log debug; log_subrequest on; location ~ ^/(images/|scripts/|styles/|robots.txt|humans.txt|favicon.ico) { #caching root /home/example/app/website/public; access_log off; expires modified +1h; } location / { ssi on; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_cache one; #caching proxy_cache_key sfs$request_uri$scheme; #caching proxy_http_version 1.1; proxy_pass http://staging/; #points to the upstream staging } } 

这是布局中的指令

 <!--# include virtual="/ssi/dynamic-content" --> 

– 编辑 –

我只是注意到,页面布局似乎也是多次渲染。 ssi请求不会返回div以外的标记,我不知道为什么整个布局会被多次插入。

– 编辑2 –

我不知道这是为什么会起作用,但是我可以通过移动位置/ {}块之外的ssi请求来解决这个问题,并将它们自己跳过caching设置并直接发送到服务器。 我的configuration现在看起来像这样。

 proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=500m inactive=60m; #caching proxy_temp_path /var/tmp; #caching gzip_comp_level 6; gzip_vary on; gzip_min_length 1000; gzip_proxied any; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_buffers 16 8k; upstream staging { server 127.0.0.1:1337; } server { listen 0.0.0.0:80; server_name dev.example.com; access_log /var/log/nginx/dev.example.log; error_log /var/log/nginx/dev.example.error.log debug; log_subrequest on; location ~ ^/(images/|scripts/|styles/|robots.txt|humans.txt|favicon.ico) { #caching root /home/example/app/website/public; access_log off; expires modified +1h; } #New proxy block specifically for ssi routes location /ssi { proxy_pass http://staging; } location / { ssi on; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_cache one; #caching proxy_cache_key sfs$request_uri$scheme; #caching proxy_http_version 1.1; proxy_pass http://staging/; #points to the upstream staging } } 

经过更多的调查,问题似乎是页面被直接插入自己的ssi包括。 几乎就好像包含整个页面一样,也包含了整个页面,并继续recursion地包含新的标记。

我认为通过在caching块设置之外移动ssi请求可以缓解这一问题,但我不完全确定原因。

我发现了答案。

在之前的configuration中,我为$request_uri设置了caching。 这意味着nginx将根据传入的请求提交和提取caching。 服务器端包含另一个请求,但由于caching是基于传入的URI,所以它最终取回主页面,从而反复插入本身。

通过使用$uri而不是$request_uri nginx将尊重重写和ssi请求,从而通过适当的命名空间(本例中是应用程序定义的ssi路由)进行caching和读取。

这里是关于nginxvariables的更多信息