geeks-dev / ngxcache
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*
This package is not auto-updated.
Last update: 2024-09-24 03:17:58 UTC
README
安装
将 geeks-dev/ngxcache
添加到 composer.json 的需求中。
{
"require": {
"geeks-dev/ngxcache": "dev-master"
}
}
使用 composer update 更新您的包,或使用 composer install 安装。
一旦安装了 Ngxcache,您需要将服务提供者注册到应用程序中。打开 app/config/app.php
并找到 providers
键。
'providers' => array(
'Geeksdev\Ngxcache\NgxcacheServiceProvider'
)
Ngxcache 还附带了一个门面,它提供了创建集合的静态语法。您可以在 app/config/app.php
文件的 aliases
键中注册门面。
'aliases' => array(
'Ngxcache' => 'Geeksdev\Ngxcache\Facades\Ngxcache'
)
配置 Nginx 服务器
添加 fastcgi_pass_header
(X-Accel-Redirect
,X-Accel-Buffering
,X-Accel-Charset
,X-Accel-Expires
,X-Accel-Limit-Rate
)。
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass_header "X-Accel-Redirect";
fastcgi_pass_header "X-Accel-Buffering";
fastcgi_pass_header "X-Accel-Charset";
fastcgi_pass_header "X-Accel-Expires";
fastcgi_pass_header "X-Accel-Limit-Rate";
fastcgi_cache_key $scheme://$host$request_uri;
.
.
.
配置包
运行此命令。
php artisan config:publish geeks-dev/ngxcache
查看您的 Nginx 配置文件。
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=cache:4m inactive=7d max_size=50m;
编辑 `app/config/packages/geeks-dev/ngxcache/config.php` 请指定代理或 FastCGI 缓存存储的目录
/*
|--------------------------------------------------------------------------
| Nginx Cache Location
|--------------------------------------------------------------------------
'nginx_cache_path' => '/var/run/nginx-cache',
/*
|--------------------------------------------------------------------------
| Nginx Cache Levels
|--------------------------------------------------------------------------
*/
'nginx_levels' => '1:2',
Ngixcache 命令
ngxcache:backtrace Nginx display URL by tracing back all caches. <= 2014/09/25 NEW
ngxcache:purge <URI> Nginx purge single cache. (URL argument is required.)
ngxcache:purge-all Nginx purge cache of all.
ngxcache:rebuild <URI> Nginx cache rebuild. (URL argument is required.)
ngxcache:refresh-all Nginx refresh and build cache of all.
ngxcache:search <URI> Nginx search single cache. (URL argument is required.)
ngxcache:show Nginx display cache of all.
创建 Ngixcache 过滤器
通常不缓存。您可以通过选择要缓存的页面来定义一个过滤器,如下面的示例所示。
filter.php
示例
Route::filter('nginx.cache-enable', function()
{
Ngxcache::enable();
});
Route::filter('nginx.cache-disable', function()
{
Ngxcache::disable();
});
`routes.php`**示例**
Route::group(array('prefix' => '', 'before' => 'nginx.cache-enable'), function(){
Route::get('/', 'HomeController@getIndex');
Route::get('{id}/show', 'HomeController@getShow');
});
Route::group(array('prefix' => '', 'before' => 'nginx.cache-disable'),function(){
Route::post('auth/login', 'AuthController@postIndex');
Route::controller('auth', 'AuthController');
});
Ngixcache 方法
/**
* Search nginx cache of all.
* example:[$result = Ngxcache::items();]
*
* @return result
*/
Ngxcache::items();
/**
* Purge nginx cache of all.
* example:[Ngxcache::purgeall();]
*
* @return result
*/
Ngxcache::purgeall();
/**
* Purge or search Nginx cache.
* example:[Ngxcache::purge($uri);]
*
* @param string $uri
* @param bool $searchmode
* @return result
*/
Ngxcache::purge($uri,$searchmode=false)
(It does not purge is performed only search Search Mode)
/**
* Rebuild Nginx cache.
* example:[Ngxcache::rebuild($uri,true,false);]
*
* @param string $uri
* @param bool $overwrite
* @param bool $usecurl
* @param bool $cached_only
* @return result
*/
Ngxcache::rebuild($uri,$overwrite=false,$usecurl=false,$cached_only=false)
(Second argument will do the forcibly overwritten.
Only if the cache does not exist , the cache is created normally.
Third argument is curl or file_get_contents)
/**
* Backtrace uri from Nginx cache.
* example:[Ngxcache::rebuild($cachePath);]
*
* @param string $cachePath
* @return string $uri
*/
Ngxcache::backtrace($cachePath);
故障排除
生效的 open_basedir 限制。
如果缓存操作不正常,请将 Nginx 缓存目录添加到 php.ini
中的 open_basedir
。
(Example)
open_basedir = .:/usr/share/php:/usr/share/pear:/var/run/nginx-cache
如果缓存在 Homestead 环境中不匹配
有两种方法。
模式 A
在 sudo vim /etc/nginx/sites-available/homestead.app
后运行 vagrant ssh
server {
- listen 80;
+ listen 8000;
运行 exit
命令。
下一步 => 编辑 scripts/homestead.rb
# Configure Port Forwarding To The Box
- config.vm.network "forwarded_port", guest: 80, host: 8000
+ config.vm.network "forwarded_port", guest: 8000, host: 8000
完成后请尝试运行 vagrant reload
。
模式 B
在 sudo vim /etc/nginx/sites-available/homestead.app
后运行 vagrant ssh
server {
listen 80;
+ listen 8000;
完成后请尝试运行 sudo service nginx reload
。