Erlang:如何创建一个从'erl_script_alias'到根url的别名?

jei2mxaa  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(149)

下面是我拥有的 inets 配置文件:

[{port, 443}, 
 {server_name, "example.com"},
 {server_root, "./root/"},
 {document_root, "./htdocs/"},
 {socket_type, {essl, [{certfile, "/etc/letsencrypt/live/example.com/cert.pem"}, {keyfile, "/etc/letsencrypt/live/example.com/privkey.pem"}]}},
 {directory_index, ["index.html"]},
 {erl_script_alias, {"/erl",[functions]}},
 {erl_script_nocache, true},
 {script_alias, {"/cgi-bin/", "/home/example/site/cgi-bin/"}},
 {script_nocache,true}
].

使用此配置文件,我可以访问:

https://example.com/cgi-bin/something.cgi

and

https://example.com/erl/functions/function

我已经知道将**{script_alias,{"/cgi-bin/"...更改为{script_alias,{"/"...可以从https://example.com/访问cgi脚本,但如何使用erl_script_alias**获得相同的行为?例如:访问https://example.com/和访问/erl/functions/function

50few1ms

50few1ms1#

Answering my own question:

The solution that I found was to create a module to deal with "GET" requests of "/".

-module(mod_index).
-export([do/1]).
-include_lib("inets/include/httpd.hrl").

do(ModData) ->
    root_url(ModData#mod.method, ModData#mod.request_uri, ModData#mod.data).

root_url("GET", "/", _) ->
    {proceed, [{response, {200,"Content-Type: text/html\r\n\r\nSomething"}}]};

root_url(_, _, OldData) ->
    {proceed, OldData}.

And adding the module into the configuration file with the default modules:

[{port, 443}, 
 {server_name, "example.com"},
 {server_root, "./root/"},
 {document_root, "./htdocs/"},
 {socket_type, {essl, [{certfile, "/etc/letsencrypt/live/example.com/cert.pem"}, {keyfile, "/etc/letsencrypt/live/example.com/privkey.pem"}]}},
 {directory_index, ["index.html"]},
 {erl_script_alias, {"/erl",[functions]}},
 {erl_script_nocache, true},
 {script_alias, {"/cgi-bin/", "/home/example/site/cgi-bin/"}},
 {script_nocache,true},
 {modules, [mod_index, mod_alias, mod_auth, mod_esi, mod_actions, mod_cgi, mod_dir, mod_get, mod_head, mod_log, mod_disk_log]}
].

As the documentation says, the modules' order matters.

相关问题