搜索本产品文档关键词
配置app.conf
所有文档
menu

应用引擎 BAE

配置app.conf

app.conf是BAE提供的部署配置文件,用户通过配置app.conf提供的如下功能,实现对所托管Web应用的高级定制:

  • handlers: 自定义Web服务器规则
  • environment: 环境变量
  • system_packages:安装系统软件包

注意:app.conf须严格遵照YAML语法规范,任何不合规范的配置,如使用中文、Tab或不符合缩进规则等,均会导致部署失败。

handlers

handlers支持如下规则:

  • url与regex_url: 设置路由规则
  • errordoc: 自定义错误页面
  • expire: 设置过期时间
  • mime: 设置某类扩展名对应的文件类型
  • check_exist: 检查文件或目录是否存在

如下为一个简单的app.conf文件handlers示例:

Plain Text
1handlers:
2  - regex_url : /
3    script: index.php
4  - regex_url : ^/index\.html$
5    script : index.php
6
7  - errordoc : 404 /error/404.html
8  - errordoc : 0 /error/default.html
9
10  - expire : .jpg modify 10 years
11  - expire : .swf modify 10 years
12  - expire : .png modify 10 years

https://bce.bdstatic.com/p3m/bce-doc/images/BAE-Pro/操作指南/.*-1638259541495.gif modify 10 years - expire : .JPG modify 10 years - expire : .ico modify 10 years

关于上述规则的解释如下:

url与regex_url

作用

设置路由规则。当用户输入的URL字符串的子串与“url”所匹配的字符串一致时,执行script指定的操作;“regex-url”规则的功能与“url”规则相同,支持标准正则表达式,可与script、status_code及location规则配合使用,推荐使用regex-url规则。

语法

Plain Text
1handlers :
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
1handlers :
2  - url : /
3    script : home.php
4  - url : /index\.html
5    script : home.php

https://bce.bdstatic.com/p3m/bce-doc/images/BAE-Pro/操作指南/.*-1638259541495.gif) static_files : static/$2 - url : /admin/. script : admin.php - 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.htm

示例
解释
- url : /
  script : home.php
指定默认首页为home.php
- url : /index.html
  script : home.php
当访问的URL包含“/index.html”时,指向home.php
https://bce.bdstatic.com/p3m/bce-doc/images/BAE-Pro/操作指南/.*-1638259541495.gif”,其中“xxx”为不包含换行在内的任意字符串
- url : /admin/.*
  script : admin.php
当访问的URL包含“/admin/xxx”时,指向“admin.php”,其中“xxx”为不包含换行在内的任意字符串
- regex_url : ^/[a-z0-9]*.html$
  script : /index.php
当访问的URL匹配“/xxx.html”时,指向“index.php”,其中“xxx”必须为小写字母或者数字。
- regex_url : ^/secure_page$
  status_code : 403
当访问的URL匹配“/secure_page”时,返回403错误。
- regex_url : ^/secure_page$
  status_code : 302
  location : http://example.com/error.html
当访问的URL匹配“/secure_page”时,返回302错误,并转移至http://example.com/error.html

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 : .pdf access 1 months

mime

  • 作用

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

  • 语法

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

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

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    

environment

  • 作用

    用户自定义环境变量。

  • 语法

    Plain Text
    1  environment :
    2    <Variable_Name> : <Variable_Value>
  • 代码示例

    Plain Text
    1  environment :
    2    USER_ENV1 : 1000
    3    USER_ENV2 : "hahaha"

system_packages

  • 作用

    安装系统软件包,bae会对每个软件包执行apt-get install命令。

  • 语法

    Plain Text
    1  system_packages :
    2    - <Package_Name>
    3   
  • 代码示例

    Plain Text
    1  system_packages :
    2     - make
    3     - vim
上一篇
环境管理
下一篇
自定义镜像