我正在配置NginX以支持緩存GET請求+特定的POST請求。 我正在使用symfony框架,因此將URI重寫爲:app.php/...。無法達到POST請求緩存的嵌套位置指令
爲了能夠分開處理GET和POST緩存,我創建了一些嵌套位置指令。
fastcgi_cache_path /nginx_cache_path levels=1:2 keys_zone=one:10m max_size=1g inactive=120m loader_threshold=300 loader_files=150;
server {
listen 80;
server_name example.com www.example.com;
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 444;
}
root /path_to_web_server;
location/{
# try to serve file directly. If not existing change to app.php
try_files $uri /app.php$is_args$args;
}
location ~ ^/images/.*\.(gif|jpg|png)$ {
root /path_to_images_folder;
}
location ~ ^/app\.php(/|$) {
location ~ /post_request_path {
fastcgi_cache_key "$request_uri|$request_body";
fastcgi_cache_valid 5m;
fastcgi_cache_methods POST;
fastcgi_cache one;
fastcgi_cache_use_stale updating error timeout invalid_header http_500 http_503 http_404;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
add_header X-Fastcgi-Cache $upstream_cache_status;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~/{
fastcgi_cache_key $scheme$host$request_uri$request_method;
fastcgi_cache_valid 60m;
fastcgi_cache_methods GET HEAD;
fastcgi_cache one;
fastcgi_cache_use_stale updating error timeout invalid_header http_500 http_503 http_404;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
add_header X-Fastcgi-Cache $upstream_cache_status;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
}
我的問題:POST請求從不緩存。 「location〜/ post_request_path」已經過測試,但從未被NginX選中。
按照nginx的調試日誌文件:
2017/07/11 19:31:00 [debug] 13707#0: *8961 test location: "/"
2017/07/11 19:31:00 [debug] 13707#0: *8961 test location: ~ "^/images/.*\.(gif|jpg|png)$"
2017/07/11 19:31:00 [debug] 13707#0: *8961 test location: ~ "^/app\.php(/|$)"
2017/07/11 19:31:00 [debug] 13707#0: *8961 test location: ~ "\.php$"
2017/07/11 19:31:00 [debug] 13707#0: *8961 using configuration "/"
2017/07/11 19:31:00 [debug] 13707#0: *8961 internal redirect: "/app.php?"
2017/07/11 19:31:00 [debug] 13707#0: *8961 test location: "/"
2017/07/11 19:31:00 [debug] 13707#0: *8961 test location: ~ "^/images/.*\.(gif|jpg|png)$"
2017/07/11 19:31:00 [debug] 13707#0: *8961 test location: ~ "^/app\.php(/|$)"
2017/07/11 19:31:00 [debug] 13707#0: *8961 test location: ~ "/post_request_path"
2017/07/11 19:31:00 [debug] 13707#0: *8961 test location: ~ "/"
2017/07/11 19:31:00 [debug] 13707#0: *8961 using configuration "/"
我很驚訝/ post_request_path因爲我以爲Nginx的算法選擇當數那些被匹配的URI的第一個匹配的正則表達式永遠不會選擇。
我想我失去了一些東西大着這裏嵌套的位置指示......
任何線索?