Surge 是一个非常流行的静态网站部署工具,它支持通过配置文件(如 surge.yaml)来定义域名、路径映射、重写规则等功能,以简化部署过程。以下是关于 Surge 规则配置的详细说明

什么是 surge.yaml

surge.yaml 是 Surge 的配置文件,用户可以在其中定义域名、路径映射、重写规则等设置,文件结构如下:

surge:
  domain: example.com
  # 其他规则配置...

rules 配置

rules 是定义域名和路径映射的核心部分,每个规则由 matchreplacing 组成,用于匹配特定路径并进行重写。

rules:
  - match: /about
    replacing: /about.html
  - match: /assets/
    replacing: /assets/
  • match:匹配的路径表达式,可以是简单路径(如 /about)或使用正则表达式。
  • replacing:要替换的路径表达式。

domainprefix

  • domain:指定绑定的域名。

    domain: example.com

    如果需要配置多个子域名,可以使用 subdomains

    subdomains:
      api: example.com
      blog: example.com
  • prefix:指定路径前缀,所有请求都会被前缀匹配。

    prefix: /sub/

urlregex

  • url:可以直接定义路径规则。
  • regex:使用正则表达式匹配路径。
url:
  /admin:
    regex: ^/admin
    replacing: /admin-panel

matchreplacing

matchreplacing 配对用于定义路径重写规则。

rules:
  - match: /user/([-9]+)
    replacing: /user/\1
  • match:路径表达式,支持捕获组。
  • replacing:替换后的路径,使用捕获组进行参数替换。

hostsuffix

  • host:匹配特定子域名。
  • suffix:匹配路径后缀。
rules:
  - host: blog.example.com
    suffix: /blog
    replacing: /latest.html

filefileMatch

匹配文件扩展名或路径,进行重写。

fileMatch:
  - *.html: replacing: .html
  - images/*: replacing: images/

rewritereplace

  • rewrite(如添加水印)。
  • replace:直接替换内容。
rewrite:
  - <html>
    <head>
      <meta charset="utf-8">
    </head>
    <body>
      ${content}
    </body>
    </html>
  </replace>

brotligzip以优化加载速度。

brotli:
  enabled: true
  clevel: 4
gzip:
  enabled: true

proxy

配置反向代理,例如通过 Surge代理内部服务器。

proxy:
  - http://localhost:800

cacheexpires

控制浏览器缓存时间。

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/

Surge 是一个非常流行的静态网站部署工具,它支持通过配置文件(如 surge.yaml)来定义域名、路径映射、重写规则等功能,以简化部署过程。以下是关于 Surge 规则配置的详细说明

扫码添加小飞机VPN网络工具官方微信

扫码添加小飞机VPN网络工具官方微信

400-638-2751
扫码添加小飞机VPN网络工具官方微信

扫码添加小飞机VPN网络工具官方微信

网站地图