我想处理从caching中的所有[GET]到/ api的请求,并处理所有其他的请求,如上一个位置块(没有caching)。 所有使用方法PUT,POST,DELETE的/ api的请求也都没有使用caching。
我在这里看到类似的问题Nginx的代理通过请求方法 ,但仍然不明白如何使用它在我的情况。
提前致谢。
我的configuration:
location / { root /var/www/project/web; # try to serve file directly, fallback to app.php try_files $uri /app.php$is_args$args; } location ~ ^/api { root /var/www/project/web/app.php; fastcgi_send_timeout 600s; fastcgi_read_timeout 600s; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/?.*)$; include fastcgi_params; fastcgi_cache fcgi; fastcgi_cache_valid 200 5m; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; } location ~ ^/(app|app_dev|config)\.php(/|$) { root /var/www/project/web; fastcgi_send_timeout 600s; fastcgi_read_timeout 600s; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/?.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; }
在这里我的问题被解决stackoverflow.com/a/35291314/1517230由聪明的stackoverflow用户:)
location ~ ^/api { root /var/www/project/web/app.php; fastcgi_send_timeout 600s; fastcgi_read_timeout 600s; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/?.*)$; include fastcgi_params; # Don't cache anything by default set $no_cache 1; # Cache GET requests if ($request_method = GET) { set $no_cache 0; } fastcgi_cache_bypass $no_cache; fastcgi_no_cache $no_cache; fastcgi_cache fcgi; fastcgi_cache_valid 200 5m; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; }
要么
map $request_method $api_cache_bypass { default 1; GET 0; } location ~ ^/api { root /var/www/project/web/app.php; fastcgi_send_timeout 600s; fastcgi_read_timeout 600s; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/?.*)$; include fastcgi_params; fastcgi_cache_bypass $api_cache_bypass; fastcgi_no_cache $api_cache_bypass; fastcgi_cache fcgi; fastcgi_cache_valid 200 5m; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; }