搜索本产品文档关键词
通过app.conf修改BCH环境配置
所有文档
menu

云虚拟主机 BCH

通过app.conf修改BCH环境配置

app.conf配置文件

app.conf是BCH提供的部署配置文件,您通过配置app.conf可以实现主机环境的自定义。

注意:

  • app.conf在网站文件主目录/webroot下。
  • app.conf须严格遵照YAML语法规范,任何不合规范的配置,如使用中文、Tab或不符合缩进规则等,均会导致发布失败。

你可通过在app.conf文件中配置handlers规则来实现环境的高级配置:

  • url/regex_url:设置路由规则(包括默认首页顺序),与script规则配合使用。
  • UrlRewrite:对于不存在的路径,根据正则表达式进行重定向。
  • errordoc: 自定义错误页面
  • expire:设置过期时间
  • mime: 设置某类扩展名对应的文件类型
  • check_exist:检查文件或目录是否存在
  • ipblacklist与ipwhitelist: 设置黑白名单

通过url/regex_urlrewrite_not_exist配合使用,能够实现绝大多数伪静态功能。

url/regex_url设置路由规则

url与regex_url

  • 作用

    设置路由规则,包括默认首页顺序。其中,url设定的正则表达式所匹配字符串是用户输入URL的子串(“/”作为特例,需精确匹配),与script规则配合使用;而regex_url设定的正则表达式须与用户输入URL完全匹配,可与script、status_code及location规则配合使用。

  • 语法

    Plain Text
    1  handlers :
    2    - url : <Regex>
    3      script : <File_Path>
    4    - url : <Regex>
    5      static_files : <File_Path>
    6    - regex_url : <Regex>
    7      script : <File_Path>
    8    - regex_url : <Regex>
    9      status_code : {301 | 302 | 403 | 404}
    10      [location : <Redirection_Page>]

    注意:

    [location: <Redirection_Page>]仅能在status_code设为301或302时使用。

  • 代码示例

    Plain Text
    1   # 设置默认首页顺序示例
    2   hanlders :
    3      - url : /
    4        script : index.php
    5      - url : /
    6        script : index.html
    7        
    8    # url示例
    9    handlers :
    10      - url : /
    11        script : home.php
    12      - url : /index\.html
    13        script : home.php
    14      - url : /(aaa)/(.*\.gif)  
    15        static_files : static/$2
    16      - url : /admin/.* 
    17        script : admin.php
    18
    19    # regex_url示例
    20      - regex_url : ^/[a-z0-9]\.html$
    21        script : /index.php       
    22      - regex_url : ^/secure_page$
    23        status_code : 403
    24      - regex_url : ^/secure_page$
    25        status_code : 302
    26        location : http://example.com/error.html
    27        
    28     # 任何request都返回503示例
    29     handlers :
    30      - url : /(.*)
    31        script : maintaince.php
    32        
    33        maintaince.php
    34        <?php
    35         header("HTTP/1.1 503 Service Unavailable");
    36        ?>
    37        this service is down for maintaince, please contact admin.    

伪静态UrlRewrite

rewrite_not_exist

  • 作用

    用户请求的<Regex>路径不存在的时候,根据<Regex>进行正则匹配,然后替换为<New_File_Path>

  • 语法

    Plain Text
    1  handlers :
    2    - rewrite_not_exist: <Regex>
    3      script : <New_File_Path>  
  • 代码示例

    Plain Text
    1# example 1
    2handlers :
    3  - rewrite_not_exist: (.*)
    4    script : /index.php/$1   

自定义错误页

errordoc

  • 作用

    自定义错误页面,设定Web服务器在处理用户请求发生错误时所返回的错误页面。

  • 语法

    Plain Text
    1  handlers :
    2    - errordoc : <Error_Code> <Error_Response_Page>    

    注意:

    <<Error_Code>为“0”时表示任意错误。

  • 代码示例

    Plain Text
    1  handlers :
    2    - errordoc : 403 /error/403.html
    3    - errordoc : 404 /error/404.html
    4    - errordoc : 0 /error/default.html

设置过期时间

expire

  • 作用

    设置过期时间,指导浏览器对其进行缓存和失效操作。

  • 语法

    Plain Text
    1  handlers :
    2    - expire : <File_Extension> {access | modify} <Number> {years | months | weeks | days | hours | minutes | seconds} 
  • 代码示例

    Plain Text
    1  handlers :
    2    - expire : .jpg modify 10 years
    3    - expire : .swf modify 10 years
    4    - expire : .png modify 10 years
    5    - expire : .gif modify 10 years
    6    - expire : .JPG modify 10 years
    7    - expire : .ico modify 10 years

MIME文件类型定义

mime

  • 作用

    设置某类扩展名对应的文件类型。

  • 语法

    Plain Text
    1  handlers :
    2    - mime : <File_Extension> <Content_Type>
  • 代码示例

    Plain Text
    1  handlers :
    2    - mime: .txt text/plain
    3    

检查文件和目录是否存在

check_exist

  • 作用

    检查文件和目录是否存在,并根据判断结果进行处理,可与script、status_code、location规则配合完成使用。

  • 语法

    Plain Text
    1  handlers :
    2    # 格式一
    3    - check_exist : {file_exist | dir_exist | not_exist}
    4      script : <File_Path>  
    5    # 格式二 
    6    - check_exist : {file_exist | dir_exist | not_exist}
    7      status_code : {301 | 302 | 403 | 404}
    8      [location : <Redirection_Page>]

    注意:

    [location: <redirection_page>]仅能在status_code设为301或302时使用。

  • 代码示例

    Plain Text
    1# example 1
    2handlers :
    3  - check_exist : not_exist
    4         script : /index.php   
    5
    6# example 2
    7handlers :
    8  - check_exist : not_exist
    9    status_code: 403
    10
    11# example 3
    12handlers :
    13  - check_exist : not_exist
    14    status_code : 302
    15       location : http://example.com/error.html    

设置IP 黑名单/白名单

ipblacklist与ipwhitelist

  • 作用

    • 黑名单:设置禁止通过的用户,黑名单以外的用户均能通过。
    • 白名单:设置能通过的用户,白名单以外的用户均不能通过。
  • 语法

    Plain Text
    1  handlers :
    2    - ipblacklist : [<Regex_URL_Pattern>] <IP_Pattern>
    3    - ipwhitelist : [<Regex_URL_Pattern>] <IP_Pattern>

    注意:

    • [<Regex_URL_Pattern>]省略时表示所有URL地址。
    • <IP_Pattern>可以使用IP地址、IP地址/掩码、正则表达式,之间用逗号隔开。
  • 代码示例

    Plain Text
    1  # 设置黑名单
    2  handlers :
    3    - ipblacklist : 192.168.1.* 
    4    - ipblacklist : 192.168.1.0/24
    5    - ipblacklist : /critical\.html 192.168.0.*
    6
    7  # 设置白名单
    8  handlers :
    9    - ipwhitelist : 192.168.1.0/24
    10    - ipwhitelist : 192.168.0.10,192.168.1.*,192.168.2.0/24  

完整示例

该示例包含设置默认首页,404处理,URL Rewrite、重定向和过期处理。

Plain Text
1    handlers:
2  
3    # 设置默认首页
4	  - url : /
5      script : home.php
6
7	  # URL Rewrite,所有的图片都访问其他地址
8        - regex_url: /picture/(.*\.gif)
9	    static_files: static/$1
10	
11	  # URL Rewrite,所有的html访问都转换为php访问
12	  -regex_url:^/([a-z0-9]*)\.html$
13	   script: /process.php?$1
14	
15	  # 重定向访问处理
16	  -regex_url:^/permission_page$
17	   status_code: 302
18	   location: http://example.com/error.html
19	
20	  # 处理404错误
21	  - errordoc : 404 /error/404.html
22	  - errordoc : 403 /error/permission.html
23
24	  # 过期处理
25	  - expire : .jpg modify 10 years
26      - expire : .swf modify 10 years
27      - expire : .png modify 10 years
28      - expire : .gif modify 10 years
29      - expire : .JPG modify 10 years
30      - expire : .ico modify 10 years
31
32      # mime 设置默认首页
33      - mime: .txt text/plain
34	  - mime: .json application/json

通过app.conf实现伪静态示例

通过url/regex_urlrewrite_not_exist配合使用,能够实现绝大多数伪静态功能。本节将以Wordpress和discuz为例,给出典型的app.conf配置示例。

wordpress

如果wordpress设置为“朴素”方式,则app.conf不需要进行其他处理。如果为其他方式,则需要在app.conf中加入以下规则:

Plain Text
1- rewrite_not_exist: (.*)
2  script: /index.php/$1

完整的app.conf文件内容如下:

Plain Text
1handlers:
2    - url : /
3    script : /index.php
4    script : /index.html
5
6    - rewrite_not_exist: (.*)
7    script: /index.php/$1
8
9    - errordoc : 404 /error/404.html
10
11    - expire : .jpg modify 10 years
12    - expire : .swf modify 10 years
13    - expire : .png modify 10 years
14    - expire : .gif modify 10 years
15    - expire : .JPG modify 10 years
16    - expire : .ico modify 10 years

discuz

如果用户部署的是discuz,建议在app.conf中加入以下规则:

Plain Text
1- regex_url: ^/topic-(.+)\.html$
2  script: /portal.php?mod=topic&topic=$1&%1
3- regex_url: ^/article-([0-9]+)-([0-9]+)\.html$
4  script: /portal.php?mod=view&aid=$1&page=$2&%1
5- regex_url: ^/forum-(\w+)-([0-9]+)\.html$
6  script: /forum.php?mod=forumdisplay&fid=$1&page=$2&%1
7- regex_url: ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$
8  script: /forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
9- regex_url: ^/group-([0-9]+)-([0-9]+)\.html$
10  script: /forum.php?mod=group&fid=$1&page=$2&%1
11- regex_url: ^/space-(username|uid)-(.+)\.html$
12  script: /home.php?mod=space&$1=$2&%1
13- regex_url: ^/blog-([0-9]+)-([0-9]+)\.html$
14  script: /home.php?mod=space&uid=$1&do=blog&id=$2&%1
15- regex_url: ^/archiver/(fid|tid)-([0-9]+)\.html$
16  script: /archiver/index.php?action=$1&value=$2&%1
17- regex_url: ^/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$
18  script: /plugin.php?id=$1:$2&%1

完整的app.conf内容如下:

Plain Text
1handlers:
2    - url : /
3    script : /index.php
4    script : /index.html
5
6    - regex_url: ^/topic-(.+)\.html$
7    script: /portal.php?mod=topic&topic=$1&%1
8    - regex_url: ^/article-([0-9]+)-([0-9]+)\.html$
9    script: /portal.php?mod=view&aid=$1&page=$2&%1
10    - regex_url: ^/forum-(\w+)-([0-9]+)\.html$
11    script: /forum.php?mod=forumdisplay&fid=$1&page=$2&%1
12    - regex_url: ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$
13    script: /forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
14    - regex_url: ^/group-([0-9]+)-([0-9]+)\.html$
15    script: /forum.php?mod=group&fid=$1&page=$2&%1
16    - regex_url: ^/space-(username|uid)-(.+)\.html$
17    script: /home.php?mod=space&$1=$2&%1
18    - regex_url: ^/blog-([0-9]+)-([0-9]+)\.html$
19    script: /home.php?mod=space&uid=$1&do=blog&id=$2&%1
20    - regex_url: ^/archiver/(fid|tid)-([0-9]+)\.html$
21    script: /archiver/index.php?action=$1&value=$2&%1
22    - regex_url: ^/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$
23    script: /plugin.php?id=$1:$2&%1
24
25    - errordoc : 404 /error/404.html
26
27    - expire : .jpg modify 10 years
28    - expire : .swf modify 10 years
29    - expire : .png modify 10 years
30    - expire : .gif modify 10 years
31    - expire : .JPG modify 10 years
32    - expire : .ico modify 10 years
上一篇
上传网站文件
下一篇
Nginx环境高级配置