我的nginx配置:
location ~(\d*?)-(\d*?).news.html{
try_files $uri $uri/ /controller/news.php?id=$2&count=$3;
}
location ~/(\d*?)-(\d*?).journal.html {
try_files $uri $uri/ /controller/journal.php?id=$1&count=$2;
}
location ~/(\d*?)-(\d*?).event.html{
try_files $uri $uri/ /controller/event.php?id=$1&count=$2;
}
location ~ /news.php$ {
fastcgi_cache my_cache;
fastcgi_cache_key $scheme$host$request_uri$request_method;
#cache for 2 hours
fastcgi_cache_valid 200 2h;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /journal.php$ {
fastcgi_cache my_cache;
fastcgi_cache_key $scheme$host$request_uri$request_method;
#cache for 1 day
fastcgi_cache_valid 200 1d;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /event.php$ {
fastcgi_cache my_cache;
fastcgi_cache_key $scheme$host$request_uri$request_method;
#cache for 5 hours
fastcgi_cache_valid 200 5h;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
我有三个(或更多)locations
,其中html网址重定向到相应的php脚本,且时间不同fastcgi_cache_valid
。
所以我需要总共添加六个location routes
来处理这样的逻辑。但是在底部,其他php脚本需要php location route
而不需要缓存。
但是,所有php location route
都具有几乎相同的属性。如何在所有php location route
之间共享?或者还有其他更短的方法来实现相同的机制吗?
答案 0 :(得分:1)
我猜您至少可以将此代码移动到外部文件
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
并将其替换为......
include /path/to/php4fpm-nginx-fastcgi.conf;
位置解析也看起来可以替换
location ~(\d*?)-(\d*?).news.html{
try_files $uri $uri/ /controller/news.php?id=$2&count=$3;
}
location ~/(\d*?)-(\d*?).journal.html {
try_files $uri $uri/ /controller/journal.php?id=$1&count=$2;
}
location ~/(\d*?)-(\d*?).event.html{
try_files $uri $uri/ /controller/event.php?id=$1&count=$2;
}
带
location ~(\d*?)-(\d*?).(news|journal|event).html{
# note it has changed order for vars.
try_files $uri $uri/ /controller/$3.php?id=$1&count=$2;
}
我会更深入地在条件中使用set $var "value"
,然后重复使用代码,但让它成为你的功课。