介绍
Surge 是荷兰的一家静态网站托管服务提供商,提供免费的静态网页托管服务。与大多数静态网页托管提供商不同的是,Surge 的网页托管服务从注册到删号除了激活账户以外全程使用命令行,几乎不需要图形界面。
在其官网的“价格”页面,只有两种套餐——“免费”套餐和 $30 一个月的“专业”套餐。两者在流量、存储空间、域名绑定方面都是无限的,也都可以使用 surge.sh
后缀的二级域名及开启 https,只是后者可为自己添加的第三方域名设置 SSL 证书,以及支持重定向、跨域设置和密码保护。
使用
安装
因为 Surge 的命令行客户端是用 Node.js 编写的,因此使用前需要先安装 Node.js 和 npm(一般会自带),此处省略 N 字。
安装好 Node.js 和 npm 后,只需在命令行输入 npm install surge -g
即可安装 Surge 。安装成功的话,输出应该是类似这样的:
npm warn deprecated inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm warn deprecated rimraf@2.7.1: Rimraf versions prior to v4 are no longer supported
npm warn deprecated har-validator@5.1.5: this library is no longer supported
npm warn deprecated glob@7.2.3: Glob versions prior to v9 are no longer supported
npm warn deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
npm warn deprecated fstream@1.0.12: This package is no longer supported.
npm warn deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
added 99 packages in 16s
6 packages are looking for funding
run `npm fund` for details
此时执行 surge --version
,输出应该是这样的:
v0.24.6
(node:2569) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
说明安装成功。
登录/注册
运行 surge login
,依次输入你的邮箱地址和密码(不要在意红色的报错信息),不需要注册,Surge 会自动为你创建账户并登录。操作成功的输出类似这样:
Login or create surge account by entering email & password.
email: (node:5877) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
email: me@example.com
password:
Success - Logged in as me@example.com.
如果你是首次登录,请打开你的邮箱,点击激活链接进行激活。
创建网站
准备
将网站的全部文件和文件夹放到一个文件夹里。这里我准备了一个只有 index.html 文件的文件夹,其 HTML 代码非常简单:
<html>
<head>
<meta charset="utf-8" />
<title>测试</title>
</head>
<body>
<h1>这只是一个测试页面</h1>
</body>
</html>
部署网站
进入到文件夹所在位置,运行 surge
。此时,你需要依次输入一些信息。首先是网站目录的路径,由于你已经进入了网站所在目录,因此直接回车即可。
接着是网站的域名,默认会生成一个 surge.sh
二级域名。你可以修改免费二级域名的前缀或者设置成自己的域名。如果你选择后者,你需要先将你的域名 CNAME 解析到 na-west1.surge.sh
。这里,我输入的域名为 yucho-surge-demo.surge.sh
。回车后,等待命令执行完成,网站部署成功的输出类似这样:
(node:11869) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Running as me@example.com (Student)
project: /home/yucho/surge-demo/
domain: yucho-surge-demo.surge.sh
size: 1 files, 169 bytes
upload: [====================] 100%
CDN: [====================] 100%
encryption: [====================] 100%
┌──────────────────────────────────────────────────────────────────────────────┬────────────────────┐
│ Certificate: issuer=C = GB, ST = Greater Manchester, L = Salford, O = Sec… │ Valid │
│ *.surge.sh, surge.sh │ 134 more days │
└──────────────────────────────────────────────────────────────────────────────┴────────────────────┘
┌──────────┬──────────────────────────────────────────────────────────────────────────────┬─────────┐
│ │ ns1.surge.world ns2.surge.world or CNAME… │ │
│ NS │ ns3.surge.world ns4.surge.world geo.surge.world │ │
├──────────┼──────────────────┬───────────────────────┬─────────────────────┬─────────────┼─────────┤
│ HTTP │ sfo.surge.sh │ US, San Francisco │ 138.197.235.123 │ D.Ocean │ ✔ ◍ │
│ HTTP │ lhr.surge.sh │ GB, London │ 46.101.67.123 │ D.Ocean │ ✔ ◍ │
│ HTTP │ yyz.surge.sh │ CA, Toronto │ 159.203.50.177 │ D.Ocean │ ✔ ◍ │
│ HTTP │ jfk.surge.sh │ US, New York │ 159.203.159.100 │ D.Ocean │ ✔ ◍ │
│ HTTP │ ams.surge.sh │ NL, Amsterdam │ 188.166.132.94 │ D.Ocean │ ✔ ◍ │
│ HTTP │ fra.surge.sh │ DE, Frankfurt │ 138.68.112.220 │ D.Ocean │ ✔ ◍ │
│ HTTP │ sgp.surge.sh │ SG, Singapore │ 139.59.195.30 │ D.Ocean │ ✔ ◍ │
│ HTTP │ blr.surge.sh │ IN, Bangalore │ 139.59.50.135 │ D.Ocean │ ✔ ◍ │
│ HTTP │ syd.surge.sh │ AU, Sydney │ 45.76.126.95 │ Vultr │ ✔ ◍ │
│ HTTP │ nrt.surge.sh │ JP, Tokyo │ 172.104.96.133 │ Linode │ ✔ ◍ │
└──────────┴──────────────────┴───────────────────────┴─────────────────────┴─────────────┴─────────┘
Live preview .............................................. 1736075801131-yucho-surge-demo.surge.sh
Production .............................................................. yucho-surge-demo.surge.sh
Success! - Published to yucho-surge-demo.surge.sh
此时就可以访问 http://yucho-surge-demo.surge.sh/ ,看看网页是否显示正常了。
注意:每次修改了网页,都要重新部署!
配置 https
默认情况下,我们添加的网站只有 http ,这导致在国内有些地方可能会打不开,而且也会让浏览器提示“不安全”。启用 https 的方法非常简单:如果你用的是官方的二级域名,只需要在部署的时候在域名前面加上 https://
;如果是自己的域名,就需要用 CloudFlare 等 CDN 来配置 https 。
记住域名和跳过输入目录路径
每次部署都需要我们输入网站目录路径和域名,太麻烦,怎么办?只需要切换到网站目录,将命令换成 surge ./
即可跳过输入目录路径的步骤。同时,在网站目录创建一个名为 CNAME 的文件,用文本编辑器打开它,在其中写入你需要绑定的域名并保存即可。下次部署就不需要再输入域名了。
自定义 404 页面
准备好你想要的 404 页面,放在网站目录中,改名为 404.html
,重新部署即可。
查看和删除已部署网站
运行 surge list
,可查看当前账户部署的全部网站,输出类似这样:
(node:21804) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
yucho-surge-demo.surge.sh 9 minutes ago surge surge.sh Standard
surge-website.yucho.top 3 months ago surge surge.sh Standard
grumpy-voice.surge.sh 7 months ago surge surge.sh Standard
localhost.yucho.top 7 months ago surge surge.sh Standard
要删除某个网站,只需要执行 surge teardown [域名]
。例如我执行 surge teardown yucho-surge-demo.surge.sh
的输出如下:
(node:22058) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Success - yucho-surge-demo.surge.sh has been removed.
退出登录 & 换号
退出登录: surge logout
换号:不需要退出登录,运行 surge login
后输入电子邮箱地址和密码即可。
surge 命令的更多使用说明参见 surge --help
。
参考
官方文档