apache_nginx_conf / nginx_conf
用于处理Nginx配置文件的类。
1.0.4
2021-06-03 09:14 UTC
Requires
- php: >=7.0
Requires (Dev)
- apach_nginx_conf/nginx_conf: >=1.0.0
Suggests
- Composer: 2.+
- version: latest
This package is not auto-updated.
Last update: 2024-09-20 08:18:03 UTC
README
Nginx_Conf

用于处理Nginx(Nginx)网络服务器配置文件的类。
代码遵循Apache License Version 2.0许可。
要求
- PHP >= 7.0.0, <= 8.1.6;
- Nginx >= 1.9.
注意: 此软件包最初由[Aleksey_Nemiro](https://packagist.org.cn/packages/aleksey.nemiro/nginxconf.php)编写。我仅克隆并修复了composer自动加载问题和php8兼容性问题,然后发布,没有修改原始项目。我并不拥有这个项目及其属性。
如何使用
包含autoload.php文件,并导入类`Nginx\Conf`。
# include autoload.php from composer vendor
require_once 'vendor/autoload.php';
# import class
use Nginx\Conf as NginxConf;
从文件加载配置
# create instance and load config from file
$conf = new NginxConf('/etc/nginx/sites-available/example.org.conf');
# or
# $conf = NginxConf::CreateFromFile('/etc/nginx/sites-available/example.org.conf');
# get values
var_dump($conf['server']);
if ($conf['server']->ContainsChild('listen'))
{
print_r($conf['server']['listen']->ParametersAsString());
}
var_dump($conf['server']['server_name']->ParametersAsString());
var_dump($conf['server']['root']->ParametersAsString());
var_dump($conf['server']['location']);
从字符串加载配置
# config data
$str = 'server {
# server name
server_name example.org;
root /home/example.org/html; # root path
auth_basic "Test server";
auth_basic_user_file /home/example.org/.htpasswd;
# location #1
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
# location #2
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}';
# parse string
$conf = NginxConf::CreateFromString($str);
# get values
var_dump($conf['server']);
var_dump($conf['server']['server_name']->ParametersAsString());
var_dump($conf['server']['root']->ParametersAsString());
# get first location
$location = $conf['server']['location'][0];
var_dump($location);
# get second location
$location2 = $conf['server']['location'][1];
var_dump($location2);
保存到文件
# load from file
$conf = NginxConf::CreateFromFile('/etc/nginx/sites-available/example.org.conf');
# set values
$conf['server']['server_name']->Parameters = array('example.org', 'www.example.org');
$conf['server']['root']->Parameters = array('/home/example.org/www');
# create a new directive
$new_location = NginxConf::CreateDirective('location');
# single name directives
$new_location->AddDirective('index', array('Default.aspx', 'default.aspx'));
$new_location->AddDirective('proxy_pass', array('http://127.0.0.1:8080'));
# directives with same name (group)
$proxy_set_header = NginxConf::CreateGroup('proxy_set_header');
$proxy_set_header->AddDirective(array('X-Real-IP', '$remote_addr'));
$proxy_set_header->AddDirective(array('X-Forwarded-For', '$remote_addr'));
$proxy_set_header->AddDirective(array('Host', '$host'));
# add the proxy_set_header to the new location
$proxy_set_header->AddTo($new_location);
# add the new location to the server directive
$new_location->AddTo($conf['server']);
# save
$conf->Save();
# or save as...
# $conf->Save('newFileName.conf');
从当前实例获取字符串
# load from file
$conf = new NginxConf::CreateFromFile('/etc/nginx/sites-available/example.org.conf');
# set values
$conf['server']['server_name']->Parameters = array('example.org', 'www.example.org');
$conf['server']['root']->Parameters = array('/home/example.org/www');
# create a new directive
$new_location = NginxConf::CreateDirective('location');
# single name directives
$new_location->AddDirective('index', array('Default.aspx', 'default.aspx'));
$new_location->AddDirective('proxy_pass', array('http://127.0.0.1:8080'));
# directives with same name (group)
$proxy_set_header = NginxConf::CreateGroup('proxy_set_header');
$proxy_set_header->AddDirective(array('X-Real-IP', '$remote_addr'));
$proxy_set_header->AddDirective(array('X-Forwarded-For', '$remote_addr'));
$proxy_set_header->AddDirective(array('Host', '$host'));
# add the proxy_set_header to the new location
$proxy_set_header->AddTo($new_location);
# add the new location to the server directive
$new_location->AddTo($conf['server']);
# get as string
$string = $conf->GetAsString();
# show string
var_dump($string);
创建一个新的配置
# create an instance
$conf = new NginxConf();
# create and add server directive
$conf->Add(NginxConf::CreateDirective('server'));
# add directives to server directive
$conf['server']->AddDirective('server_name', array('example.org', 'www.example.org'));
$conf['server']->AddDirective('root', array('/home/example.org/www'));
# create a new location directive
$location = NginxConf::CreateDirective('location');
# add sub-directives
$location->AddDirective('index', array('index.php', 'index.html', 'index.htm'));
# add the location to the server directive
$location->AddTo($conf['server']);
# save
$conf->Save('example.org.conf');
# get as string
$string = $conf->GetAsString();
# show string
var_dump($string);
# or save
# $conf->Save('newFileName.conf');