custom domain - ltoddy/blog GitHub Wiki
你需要把caddy
(web服务器)下载到本地。caddy
的使用非常简单,即使没有配置文件也可以直接运行。
它的配置文件一般默认名为Caddyfile
。
某些时候,你的web服务可能需要你的url的信息。 比如,你做了一个图片服务器,用户上传图片,你将图片的url反馈给用户。 这个时候就需要获取自己的域名信息了。
比如你的域名是 hello.world
,用户上传了一个照片后,你返回给用户一个url: hello.world/images/xxx.jpg
。
那么,如果在本地测试这个功能呢。
先创建一个简单的http server,因为nodejs标准库里已经为我们提供了http模块,所以用node最方便。
目录:
.
├── Caddyfile
└── server.js
server.js
const http = require("http");
const server = http.createServer((req, res) => {
res.end(req.headers.host);
});
server.listen(3000);
此时运行node server.js
,打开浏览器访问localhost:3000
,会看到:localhost:3000
。
让我们来更改我们的hosts
文件,hosts
文件干什么用的:Hosts - The static table lookup for host name(主机名查询静态表)
。
我的系统是Ubuntu
,hosts
文件所在位置为/etc/hosts
,在文件中加一行:
127.0.0.1 hello.world
然后创建一个名为Caddyfile
的文件,
Caddyfile
hello.world {
proxy / localhost:3000
tls off
}
打开两个终端,第一个终端运行 caddy
, 第二个终端运行 node server.js
。
此时在浏览器访问 hello.world:3000
, 就会看到我们自定义域名的信息了。
就这么简单。