0

我正在开发一个在牛仔网络服务器上使用氮气网络框架的应用程序。当我通过 http 运行服务器时效果很好。现在在生产中,应用程序必须在 https 上运行。

我已经从默认修改了氮气的etc目录中的cowboy.config文件

% vim: ts=4 sw=4 et ft=erlang
[
{cowboy,[
    {bind_address,"127.0.0.1"},
    {port,80},
    {server_name,nitrogen},       
    {document_root,"./site/static"},
    %% some comments.........
    {static_paths, ["/js/","/images/","/css/","/nitrogen/","/favicon.ico"]}
]}
].  

对这个

% vim: ts=4 sw=4 et ft=erlang
[
{cowboy,[
    {bind_address,"127.0.0.1"},
    {port,443},
    {server_name,nitrogen},
    {cacertfile, "Path/cacert.pem"},
    {certfile, "Path/webservercert.pem"},
    {keyfile, "Path/webserverkey.pem"},
    {password, "webserverkeypassphrase"}
    {document_root,"./site/static"},
    %% some comments.........
    {static_paths, ["/js/","/images/","/css/","/nitrogen/","/favicon.ico"]}
]}
].  

其中Path是我自己使用openSSL生成并签名的 SSL 证书的绝对路径。我将我的站点名称作为 domainname.com 但我首先按照 openSSl 文档创建了一个 CA

我还从默认值修改了在nitro/site/scr中找到的nitrogen_sup.erl文件中的Supervisor 回调

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init([]) ->
%% Start the Process Registry...
application:start(crypto),
application:start(nprocreg),
application:start(ranch),

%% Start Cowboy...
application:start(cowboy),
{ok, BindAddress} = application:get_env(cowboy, bind_address),
{ok, Port} = application:get_env(cowboy, port),
{ok, ServerName} = application:get_env(cowboy, server_name),
{ok, DocRoot} = application:get_env(cowboy, document_root),
{ok, StaticPaths} = application:get_env(cowboy, static_paths),  

io:format("Starting Cowboy Server (~s) on ~s:~p, root: '~s'~n",
          [ServerName, BindAddress, Port, DocRoot]),

Dispatch = init_dispatch(DocRoot, StaticPaths),

{ok, _} = cowboy:start_http(http, 100,     
        [
            {port, Port}               
        ], [
    {env, [{dispatch, Dispatch}]},
    {max_keepalive, 50}
]),

{ok, { {one_for_one, 5, 10}, []} }.

到下面这个

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init([]) ->
   %% Start the Process Registry...

   application:start(crypto),    
   application:start(nprocreg),
   application:start(ranch),   

   %% Start Cowboy...
   application:start(cowboy),
   {ok, BindAddress} = application:get_env(cowboy, bind_address),
   {ok, Port} = application:get_env(cowboy, port),
   {ok, ServerName} = application:get_env(cowboy, server_name),
   {ok, DocRoot} = application:get_env(cowboy, document_root),
   {ok, StaticPaths} = application:get_env(cowboy, static_paths),
   {ok, CAcertfile} = application:get_env(cowboy, cacertfile),
   {ok, Certfile} = application:get_env(cowboy, certfile),
   {ok, Keyfile} = application:get_env(cowboy, keyfile),
   {ok, Password} = application:get_env(cowboy, password),

   io:format("Starting Cowboy Server (~s) on ~s:~p, root: '~s'~n",
          [ServerName, BindAddress, Port, DocRoot]),

   Dispatch = init_dispatch(DocRoot, StaticPaths),
   {ok, _} = cowboy:start_https(https, 100,
     [
        {port, Port},
        {cacertfile, CAcertfile}, 
        {certfile, Certfile},
        {keyfile,  Keyfile},
        {password, Password}            
    ], [         
    {env, [{dispatch, Dispatch}]},
    {max_keepalive, 50}
]),

{ok, { {one_for_one, 5, 10}, []} }.

使用 sync:go() 文件编译并重新加载。但是我关闭了氮气并再次启动。

在 shell 中,我使用 curl 实用程序来测试服务器是否正在监听

$ curl --cacert Absolute_path/cacert.pem -i https://domainname.com

响应是正确的,因为索引页面上的内容显示在 shell 中

但是,当我使用 Firefox 浏览器时,它会抛出一个安全警告,我承认除了我知道它的原因之外,我会永久添加到异常中。当我再次尝试获取页面时,浏览器会抛出此错误。

Secure Connection Failed

The key does not support the requested operation.

(Error code: sec_error_invalid_key)

.The page you are trying to view cannot be shown because the authenticity of the received data could not be verified.
.Please contact the website owners to inform them of this problem. Alternatively, use the command found in the help menu to report this broken site.

当我检查氮气控制台时,如果发现此错误报告

(nitrogen@127.0.0.1)4> user@user:~/nitrogen/rel/nitrogen$ 
user@user:~/nitrogen/rel/nitrogen$ sudo ./bin/nitrogen console
Exec: /home/user/nitrogen/rel/nitrogen/erts-5.10.4/bin/erlexec  -boot       /home/user/nitrogen/rel/nitrogen/releases/2.2.2/nitrogen -mode interactive  -config /home/user/nitrogen/rel/nitrogen/etc/app.config -config /home/user/nitrogen/rel/nitrogen/etc/cowboy.config -config /home/user/nitrogen/rel/nitrogen/etc/sync.config -args_file /home/dotshule/nitrogen/rel/nitrogen/etc/vm.args -- console
Root: /home/dotshule/nitrogen/rel/nitrogen
Erlang R16B03 (erts-5.10.4) [source] [smp:2:2] [async-threads:5] [hipe] [kernel-poll:true]

Eshell V5.10.4  (abort with ^G)
(nitrogen@127.0.0.1)1> Starting Cowboy Server (nitrogen) on 127.0.0.1:443, root: './site/static'

=ERROR REPORT==== 20-Feb-2014::14:51:12 ===
SSL: certify: tls_connection.erl:375:Fatal error: unknown ca

现在我不明白的是服务器是拒绝我的证书还是我跳过了一个步骤,或者一两个步骤出错了,或者问题出在我自己创建的 CA(根证书cacert.pem)或问题上在 openSSL 上!

我现在开始怀疑如果我生成我的CSR并将其发送到受信任的 CA,例如 symantec、digcert、thawte、geotrust 等。生成的证书也可能无法正常工作。

我需要你的帮助来解决这个关于牛仔网络服务器的氮气 https 问题。谢谢你到目前为止的所有帮助....

4

2 回答 2

1

我不确定为什么牛仔会抛出那个特定的错误(tls_connection.erl实际上是 Erlang 的一部分,而不是 Cowboy 或 Nitrogen)。

也就是说,当谈到使用 Nitrogen 运行 SSL 时,我通常只是向用户推荐使用 nginx 作为 Nitrogen 前面的反向代理,并且在 Nitrogen 站点http://nitrogenproject.com/doc上有 nginx 配置示例/config.html(向下滚动到仅 SSL 示例)。

我意识到这并不完全理想,因此,或者,我会看看 nginx 或 apache 是否能够成功地为具有相同密钥/证书组合的示例页面提供服务。显然,“未知 ca”错误是说 Erlang 不喜欢它是自签名证书这一事实。因此,您可以尝试使用其他已签名的证书/密钥,或者在 StartSSL.com 上免费生成一个真实的证书/密钥,然后查看错误是否继续出现。

同样,这些都不是可靠的答案,但它们应该可以帮助您指出一些方向来帮助您解决问题。

就个人而言,我在 nginx 后面运行我所有的 Nitrogen 实例,并让 nginx 处理 SSL 和负载平衡。

于 2014-02-24T23:40:21.507 回答
0

我的方法确实有效,我已经使用 SYMANTEC 和 VERISIGN SSL 证书成功地对其进行了测试,这可能是在 Cowboy Web 服务器上安装 SSL 证书的正式方式。

于 2014-03-11T08:54:30.290 回答