|
| 1 | +# Config Template |
| 2 | + |
| 3 | +Nginx UI Template provides out-of-the-box configuration templates for users. In `NgxConfigEditor`, we offer a UI where users can quickly insert configurations from the template into the current configuration file. |
| 4 | +In this document, we will describe the file format and syntax of it. |
| 5 | + |
| 6 | +The configuration templates are stored in `template/block`, and we welcome you to share your own configuration templates by open a [PR](https://github.com/0xJacky/nginx-ui/pulls). |
| 7 | + |
| 8 | +::: tip |
| 9 | +Please note, you need to recompile the backend after modifying or adding new configuration files. |
| 10 | +::: |
| 11 | + |
| 12 | +## File Format |
| 13 | + |
| 14 | +Nginx UI Template file consists of two parts: the file header and the actual Nginx configuration. |
| 15 | + |
| 16 | +Below is a configuration template for hotlink protection, which we will use as a basis to introduce the file format and related syntax of Nginx UI Template. |
| 17 | + |
| 18 | +```nginx configuration |
| 19 | +# Nginx UI Template Start |
| 20 | +name = "Hotlink Protection" |
| 21 | +author = "@0xJacky" |
| 22 | +description = { en = "Hotlink Protection Config Template", zh_CN = "防盗链配置模板"} |
| 23 | +
|
| 24 | +[variables.NoneReferer] |
| 25 | +type = "boolean" |
| 26 | +name = { en = "Allow Referer is None", zh_CN = "允许空 Referer"} |
| 27 | +value = false |
| 28 | +
|
| 29 | +[variables.AllowReferers] |
| 30 | +type = "string" |
| 31 | +name = { en = "Allow Referers", zh_CN = "允许的 Referers"} |
| 32 | +value = "" |
| 33 | +# Nginx UI Template End |
| 34 | +
|
| 35 | +location ~ .*\.(jpg|png|js|css)$ { |
| 36 | + valid_referers {{- if .NoneReferer}} none {{- end}} blocked server_names {{if .AllowReferers}}{{.AllowReferers}}{{- end}}; |
| 37 | + if ($invalid_referer) { |
| 38 | + return 403; |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## File Header |
| 44 | + |
| 45 | +The file header should be placed between `# Nginx UI Template Start` and `# Nginx UI Template End`, and should follow the toml syntax. |
| 46 | + |
| 47 | +The file header includes the following fields: |
| 48 | + |
| 49 | +| Field | Description | Type | Required | |
| 50 | +|:------------------------------:|:---------------------------------------------------------------------:|:---------------------------------------------:|:--------:| |
| 51 | +| `name` | Name of the configuration | string | Yes | |
| 52 | +| `author` | Author | string | Yes | |
| 53 | +| `description` | Desciption, uses a toml dictionary for multi-language support | toml dictionary | Yes | |
| 54 | +| `variables.VariableName.type` | Variable type, currently supports `boolean` and `string` | string | No | |
| 55 | +| `variables.VariableName.name` | Variable display name, is a toml dictionary to support multi-language | toml dictionary | No | |
| 56 | +| `variables.VariableName.value` | Default value of the variable | boolean/string (according to type definition) | No | |
| 57 | + |
| 58 | +示例如下: |
| 59 | + |
| 60 | +```toml |
| 61 | +# Nginx UI Template Start |
| 62 | +name = "Hotlink Protection" |
| 63 | +author = "@0xJacky" |
| 64 | +description = { en = "Hotlink Protection Config Template", zh_CN = "防盗链配置模板"} |
| 65 | + |
| 66 | +[variables.NoneReferer] |
| 67 | +type = "boolean" |
| 68 | +name = { en = "Allow Referer is None", zh_CN = "允许空 Referer"} |
| 69 | +value = false |
| 70 | + |
| 71 | +[variables.AllowReferers] |
| 72 | +type = "string" |
| 73 | +name = { en = "Allow Referers", zh_CN = "允许的 Referers"} |
| 74 | +value = "" |
| 75 | +# Nginx UI Template End |
| 76 | +``` |
| 77 | + |
| 78 | +The name, author, and description will be displayed in the configuration list as a summary. |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +When you click the "View" button, a dialog will appear, as shown below. |
| 83 | + |
| 84 | +<img src="/assets/nginx-ui-template/en/config-ui.png" width="350px" title="Config Modal" /> |
| 85 | + |
| 86 | +The input boxes and switches in the interface correspond to the variable types `boolean` and `string`. |
| 87 | + |
| 88 | +## Nginx Configuration |
| 89 | +The Nginx configuration should be provided after the file header. This part will be parsed using the Go `text/template` library. This library provides powerful template generation capabilities, including conditional judgment, looping, and complex text processing, etc. |
| 90 | +For more information, please check [Go Documentation](https://pkg.go.dev/text/template). |
| 91 | + |
| 92 | +The variables defined in the header can be used in this part, such as `.NoneReferer` and `.AllowReferers`. |
| 93 | +Please note that you need to define the variables in the header in advance before using them in this part. |
| 94 | + |
| 95 | +Here is an example: |
| 96 | + |
| 97 | +```nginx configuration |
| 98 | +location ~ .*\.(jpg|png|js|css)$ { |
| 99 | + valid_referers {{- if .NoneReferer}} none {{- end}} blocked server_names {{if .AllowReferers}}{{.AllowReferers}}{{- end}}; |
| 100 | + if ($invalid_referer) { |
| 101 | + return 403; |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +When users input variable values in the frontend input boxes, the system will automatically generate new configuration content, as shown below: |
| 107 | + |
| 108 | +<img src="/assets/nginx-ui-template/en/config-ui-after-input.png" width="350px" title="Config Modal" /> |
| 109 | + |
| 110 | +In addition to the variables defined in the template header, we also provide macro-defined variables, as shown in the table below: |
| 111 | + |
| 112 | +| Variable Name | Description | |
| 113 | +|:-------------:|:-------------------------:| |
| 114 | +| HTTPPORT | Nginx UI listening port | |
| 115 | +| HTTP01PORT | Port for HTTP01 Challenge | |
| 116 | + |
| 117 | +The variables above can be used directly in the configuration part without definition in the header. |
0 commit comments