Skip to content

Commit d3184ff

Browse files
committed
docs: added docs of the syntax of config template
1 parent b1b6e8f commit d3184ff

File tree

16 files changed

+568
-255
lines changed

16 files changed

+568
-255
lines changed

docs/.vitepress/config/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const enConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
2626
items: [
2727
{text: 'Build', link: '/guide/build'},
2828
{text: 'Project Structure', link: '/guide/project-structure'},
29+
{text: 'Config Template', link: '/guide/nginx-ui-template'},
2930
{text: 'Contributing', link: '/guide/contributing'}
3031
]
3132
},

docs/.vitepress/config/zh_CN.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const zhCNConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
3131
items: [
3232
{text: '构建', link: '/zh_CN/guide/build'},
3333
{text: '项目结构', link: '/zh_CN/guide/project-structure'},
34+
{text: '配置模板', link: '/zh_CN/guide/nginx-ui-template'},
3435
{text: '贡献代码', link: '/zh_CN/guide/contributing'}
3536
]
3637
},

docs/.vitepress/theme/styles/custom.less

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,16 @@
121121
.VPNavBar {
122122
white-space: nowrap;
123123
}
124+
125+
img {
126+
margin: 0 auto;
127+
}
128+
129+
.vp-doc table {
130+
display: table;
131+
margin: 10px auto;
132+
}
133+
134+
.VPImage .logo {
135+
margin-left: 0;
136+
}

docs/guide/nginx-ui-template.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
![Config template list](/assets/nginx-ui-template/zh_CN/config-template-list.png)
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.
15.2 KB
Loading
32.7 KB
Loading
30.3 KB
Loading
12.3 KB
Loading
31.7 KB
Loading
29.3 KB
Loading

0 commit comments

Comments
 (0)