上云无忧 > 文档中心 > 通过app.conf修改百度智能云云虚拟主机环境配置
云虚拟主机
通过app.conf修改百度智能云云虚拟主机环境配置

文档简介:
app.conf配置文件: app.conf是BCH提供的部署配置文件,您通过配置app.conf可以实现主机环境的自定义。 注意: app.conf在网站文件主目录/webroot下。 app.conf须严格遵照YAML语法规范,任何不合规范的配置,如使用中文、Tab或不符合缩进规则等,均会导致发布失败。 【百度智能云】商务安全主机
*此产品及展示信息均由百度智能云官方提供。免费试用 咨询热线:400-826-7010,为您提供专业的售前咨询,让您快速了解云产品,助您轻松上云! 微信咨询
  免费试用、价格特惠

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_url和rewrite_not_exist配合使用,能够实现绝大多数伪静态功能。

url/regex_url设置路由规则

url与regex_url

  • 作用

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

  • 语法

    handlers :
        - url :script :- url :static_files :- regex_url :script :- regex_url :status_code : {301 | 302 | 403 | 404}
          [location :]

    注意:

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

  • 代码示例

    # 设置默认首页顺序示例
       hanlders :
          - url : /
            script : index.php
          - url : /
            script : index.html
            
        # url示例
        handlers :
          - url : /
            script : home.php
          - url : /index\.html
            script : home.php
          - url : /(aaa)/(.*\.gif)  
            static_files : static/$2
          - url : /admin/.* 
            script : admin.php
    
        # regex_url示例
          - regex_url : ^/[a-z0-9]\.html$
            script : /index.php       
          - regex_url : ^/secure_page$
            status_code : 403
          - regex_url : ^/secure_page$
            status_code : 302
            location : http://example.com/error.html
            
         # 任何request都返回503示例
         handlers :
          - url : /(.*)
            script : maintaince.php
            
            maintaince.php
            
            this service is down for maintaince, please contact admin.

伪静态UrlRewrite

rewrite_not_exist

  • 作用

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

  • 语法

    handlers :
        - rewrite_not_exist:script :
  • 代码示例

    # example 1
    handlers :
      - rewrite_not_exist: (.*)
        script : /index.php/$1

自定义错误页

errordoc

  • 作用

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

  • 语法

    handlers :
        - errordoc :

    注意:

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

  • 代码示例

    handlers :
        - errordoc : 403 /error/403.html
        - errordoc : 404 /error/404.html
        - errordoc : 0 /error/default.html

设置过期时间

expire

  • 作用

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

  • 语法

    handlers :
        - expire :{access | modify}{years | months | weeks | days | hours | minutes | seconds}
  • 代码示例

    handlers :
        - expire : .jpg modify 10 years
        - expire : .swf modify 10 years
        - expire : .png modify 10 years
        - expire : .gif modify 10 years
        - expire : .JPG modify 10 years
        - expire : .ico modify 10 years

MIME文件类型定义

mime

  • 作用

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

  • 语法

    handlers :
        - mime :
  • 代码示例

    handlers :
        - mime: .txt text/plain

检查文件和目录是否存在

check_exist

  • 作用

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

  • 语法

    handlers :
        # 格式一
        - check_exist : {file_exist | dir_exist | not_exist}
          script :# 格式二 
        - check_exist : {file_exist | dir_exist | not_exist}
          status_code : {301 | 302 | 403 | 404}
          [location :]

    注意:

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

  • 代码示例

    # example 1
    handlers :
      - check_exist : not_exist
             script : /index.php   
    
    # example 2
    handlers :
      - check_exist : not_exist
        status_code: 403
    
    # example 3
    handlers :
      - check_exist : not_exist
        status_code : 302
           location : http://example.com/error.html

设置IP 黑名单/白名单

ipblacklist与ipwhitelist

  • 作用

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

    handlers :
        - ipblacklist : []- ipwhitelist : []

    注意:

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

    # 设置黑名单
      handlers :
        - ipblacklist : 192.168.1.* 
        - ipblacklist : 192.168.1.0/24
        - ipblacklist : /critical\.html 192.168.0.*
    
      # 设置白名单
      handlers :
        - ipwhitelist : 192.168.1.0/24
        - ipwhitelist : 192.168.0.10,192.168.1.*,192.168.2.0/24

完整示例

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

handlers:
  
    # 设置默认首页
	  - url : /
      script : home.php

	  # URL Rewrite,所有的图片都访问其他地址
        - regex_url: /picture/(.*\.gif)
	    static_files: static/$1
	
	  # URL Rewrite,所有的html访问都转换为php访问
	  -regex_url:^/([a-z0-9]*)\.html$
	   script: /process.php?$1
	
	  # 重定向访问处理
	  -regex_url:^/permission_page$
	   status_code: 302
	   location: http://example.com/error.html
	
	  # 处理404错误
	  - errordoc : 404 /error/404.html
	  - errordoc : 403 /error/permission.html

	  # 过期处理
	  - expire : .jpg modify 10 years
      - expire : .swf modify 10 years
      - expire : .png modify 10 years
      - expire : .gif modify 10 years
      - expire : .JPG modify 10 years
      - expire : .ico modify 10 years

      # mime 设置默认首页
      - mime: .txt text/plain
	  - mime: .json application/json

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

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

wordpress

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

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

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

handlers:
    - url : /
    script : /index.php
    script : /index.html

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

    - errordoc : 404 /error/404.html

    - expire : .jpg modify 10 years
    - expire : .swf modify 10 years
    - expire : .png modify 10 years
    - expire : .gif modify 10 years
    - expire : .JPG modify 10 years
    - expire : .ico modify 10 years

discuz

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

- regex_url: ^/topic-(.+)\.html$
  script: /portal.php?mod=topic&topic=$1&%1
- regex_url: ^/article-([0-9]+)-([0-9]+)\.html$
  script: /portal.php?mod=view&aid=$1&page=$2&%1
- regex_url: ^/forum-(\w+)-([0-9]+)\.html$
  script: /forum.php?mod=forumdisplay&fid=$1&page=$2&%1
- regex_url: ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$
  script: /forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
- regex_url: ^/group-([0-9]+)-([0-9]+)\.html$
  script: /forum.php?mod=group&fid=$1&page=$2&%1
- regex_url: ^/space-(username|uid)-(.+)\.html$
  script: /home.php?mod=space&$1=$2&%1
- regex_url: ^/blog-([0-9]+)-([0-9]+)\.html$
  script: /home.php?mod=space&uid=$1&do=blog&id=$2&%1
- regex_url: ^/archiver/(fid|tid)-([0-9]+)\.html$
  script: /archiver/index.php?action=$1&value=$2&%1
- regex_url: ^/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$
  script: /plugin.php?id=$1:$2&%1

完整的app.conf内容如下:

handlers:
    - url : /
    script : /index.php
    script : /index.html

    - regex_url: ^/topic-(.+)\.html$
    script: /portal.php?mod=topic&topic=$1&%1
    - regex_url: ^/article-([0-9]+)-([0-9]+)\.html$
    script: /portal.php?mod=view&aid=$1&page=$2&%1
    - regex_url: ^/forum-(\w+)-([0-9]+)\.html$
    script: /forum.php?mod=forumdisplay&fid=$1&page=$2&%1
    - regex_url: ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$
    script: /forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
    - regex_url: ^/group-([0-9]+)-([0-9]+)\.html$
    script: /forum.php?mod=group&fid=$1&page=$2&%1
    - regex_url: ^/space-(username|uid)-(.+)\.html$
    script: /home.php?mod=space&$1=$2&%1
    - regex_url: ^/blog-([0-9]+)-([0-9]+)\.html$
    script: /home.php?mod=space&uid=$1&do=blog&id=$2&%1
    - regex_url: ^/archiver/(fid|tid)-([0-9]+)\.html$
    script: /archiver/index.php?action=$1&value=$2&%1
    - regex_url: ^/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$
    script: /plugin.php?id=$1:$2&%1

    - errordoc : 404 /error/404.html

    - expire : .jpg modify 10 years
    - expire : .swf modify 10 years
    - expire : .png modify 10 years
    - expire : .gif modify 10 years
    - expire : .JPG modify 10 years
    - expire : .ico modify 10 years
相似文档
  • Nginx配置文件加载流程: 系统启动后将自动加载bcloud_nginx_gen.conf和bcloud_nginx_user.conf配置文件: bcloud_nginx_gen.conf文件是由app.conf文件转换的。 【百度智能云】商务安全主机
  • 自定义php.ini: 用户可以在webroot目录下增加一个php.ini文件,将需要改变的值写入,重载后即可生效。其中error_log和extension_dir的值请不要修改,否则会导致BCH的一些服务无法生效。 【百度智能云】商务安全主机
  • 本文介绍百度智能云“BCH使用WordPress快速建站”的方法(含操作指导视频),您可以参考视频中的关键步骤指导,使用智能云产品或是自助解决问题,请点击此处观看视频《BCH搭建WordPress以及配置伪静态》。 【百度智能云】商务安全主机
  • 本文介绍百度智能云“BCH使用Discuz搭建论坛”的方法(含操作指导视频),您可以参考视频中的关键步骤指导,使用智能云产品或是自助解决问题,请点击此处观看视频《BCH使用Discuz搭建论坛》。 【百度智能云】商务安全主机
  • 微擎是一款免费开源的公众号管理与小程序开发系统,基于目前最流行的WEB2.0架构(php+mysql),支持在线升级和安装模块及模板,拥有良好的开发框架、成熟稳定的技术解决方案、活跃的第三方开发者及开发团队,依托微擎开放的生态系统,提供丰富的扩展功能。 【百度智能云】商务安全主机
官方微信
联系客服
400-826-7010
7x24小时客服热线
分享
  • QQ好友
  • QQ空间
  • 微信
  • 微博
返回顶部