什么是 surge.yaml?
surge.yaml 是 Surge 的配置文件,用户可以在其中定义域名、路径映射、重写规则等设置,文件结构如下:
surge: domain: example.com # 其他规则配置...
rules 配置
rules 是定义域名和路径映射的核心部分,每个规则由 match 和 replacing 组成,用于匹配特定路径并进行重写。
rules:
- match: /about
replacing: /about.html
- match: /assets/
replacing: /assets/
match:匹配的路径表达式,可以是简单路径(如/about)或使用正则表达式。replacing:要替换的路径表达式。
domain 和 prefix
-
domain:指定绑定的域名。domain: example.com
如果需要配置多个子域名,可以使用
subdomains:subdomains: api: example.com blog: example.com
-
prefix:指定路径前缀,所有请求都会被前缀匹配。prefix: /sub/
url 和 regex
url:可以直接定义路径规则。regex:使用正则表达式匹配路径。
url:
/admin:
regex: ^/admin
replacing: /admin-panel
match 和 replacing
match 和 replacing 配对用于定义路径重写规则。
rules:
- match: /user/([-9]+)
replacing: /user/\1
match:路径表达式,支持捕获组。replacing:替换后的路径,使用捕获组进行参数替换。
host 和 suffix
host:匹配特定子域名。suffix:匹配路径后缀。
rules:
- host: blog.example.com
suffix: /blog
replacing: /latest.html
file 和 fileMatch
匹配文件扩展名或路径,进行重写。
fileMatch: - *.html: replacing: .html - images/*: replacing: images/
rewrite 和 replace
rewrite(如添加水印)。replace:直接替换内容。
rewrite:
- <html>
<head>
<meta charset="utf-8">
</head>
<body>
${content}
</body>
</html>
</replace>
brotli 和 gzip以优化加载速度。
brotli: enabled: true clevel: 4 gzip: enabled: true
proxy
配置反向代理,例如通过 Surge代理内部服务器。
proxy: - http://localhost:800
cache 和 expires
控制浏览器缓存时间。
cache: enabled: true expires: 1h
redirect
重定向到其他路径或域名。
redirect: /old-path: /new-path example.com: https://www.example.com/
示例配置
surge:
domain: example.com
rules:
- match: /about
replacing: /about.html
- match: /images
replacing: /images/
subdomains:
api: example.com
blog: example.com
proxy:
- http://localhost:300
cache:
enabled: true
expires: 1h
redirect:
/old-path: /new-path
example.com: https://www.example.com/








