fastcgi-mono-server4 和 nginx docker

fastcgi-mono-server4 and nginx with docker

网上有很多信息告诉我们如何在同一台机器上设置 nginx + fastcgi-mono-server4 以托管 ASP.net(目前我正在使用带有 Websharper 的 F# 应用程序)来自 linux环境。但是docker应该运行只有一个进程。还有解决方案,使用 运行it 或其他工具在同一容器上 运行 nginx 和 mono 服务器。

我让它在同一个容器中工作,但是当我尝试将 Web 应用程序 + fastcgi-mono-server4 移动到另一个容器时,我得到了

 [error] 12#12: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.99.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://172.17.0.40:32775", host: "192.168.99.100:32786"

找到了一个 article 关于类似要求的 php fastcgi 服务器 (php-fpm),它只适用于 linking 容器。但是对于 fastcgi-mono-server4 它没有。

我已经尝试用 fastcgi-mono-server4 + 应用程序文件创建 1 个容器,暴露 VOLUME 应用程序文件,暴露端口 9000,然后是第一个容器中的卷和 link 的 nginx 容器 还尝试 运行 2 个安装了 nginx 和单服务器的相同容器,第一个是 fastcgi_pass 127.0.0.1:9000,第二个是 fastcgi_pass 172.17.0.40:32775 - 来自第一个容器的 ip 和端口.如果访问第一个容器 - 应用程序工作,访问第二个容器 - 与上面相同的错误。

用于运行单声道服务器的命令:

fastcgi-mono-server4 /printlog=True /applications=/:/var/www/websharper/ /socket=tcp:0.0.0.0:9000 /loglevels=All /verbose=True

已安装软件的版本:

Container from mono (debian wheezy with mono:4.0.0)
NGINX_VERSION 1.9.5-1~wheezy
$ mono --version
Mono JIT compiler version 4.0.3 (Stable 4.0.3.20/d6946b4 Tue Aug  4 09:43:57 UTC 2015)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
    TLS:           __thread
    SIGSEGV:       altstack
    Notifications: epoll
    Architecture:  amd64
    Disabled:      none
    Misc:          softdebug
    LLVM:          supported, not enabled.
    GC:            sgen
$ fastcgi-mono-server4 --version
Mono.WebServer2.dll 0.4.0.0

fastcgi-mono-server4 启动的一些日志

9: 1 fastcgi-mono-server [2015-10-01 09:01:17.510407] Debug  : fastcgi-mono-server4
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.530481] Debug  : Uid 0, euid 0, gid 0, egid 0
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.531310] Debug  : Root directory: /
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.532843] Notice : Adding applications '/:/var/www/websharper/'...
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.533478] Notice : Registering application:
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.533757] Notice :     Host:          any
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.534075] Notice :     Port:          any
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.534459] Notice :     Virtual path:  /
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.534770] Notice :     Physical path: /var/www/websharper/
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.543250] Debug  : Parsed tcp:0.0.0.0:9000 as URI tcp:0.0.0.0:9000
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.556826] Debug  : Listening on port: 9000
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.558991] Debug  : Listening on address: 0.0.0.0
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.564079] Debug  : Max connections: 1024
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.564786] Debug  : Max requests: 1024
9: 1 fastcgi-mono-server [2015-10-01 09:01:17.565224] Debug  : Multiplex connections: False
9: 3 fastcgi-mono-server [2015-10-01 09:01:17.568840] Debug  : Server started [callback: Mono.WebServer.FastCgi.ServerProxy]

在 mono:4.0.0 docker 图像之上安装:

apt-get install -y ca-certificates nginx=1.9.5-1~wheezy
apt-get install -qy mono-fastcgi-server4
apt-get install -qy mono-xsp4
apt-get install -qy fsharp

我还要确保应用程序使用 mono 4.5:

sed -ie 's|mono/4.0|mono/4.5|g' /usr/bin/fastcgi-mono-server4 \
&& sed -ie 's|mono/4.0|mono/4.5|g' /usr/bin/xsp4

还有我的nginx.conf

worker_processes  1;
events {
   worker_connections  1024;
}
http {
   include       mime.types;
   default_type  application/octet-stream;
   sendfile        on;
   keepalive_timeout  65;
   server {
       listen       80 default;
       access_log   /dev/stdout;
       error_log    /dev/stdout;
       root         /var/www/websharper;
       index        main.html;
       location / {
           root                /var/www/websharper/;
           fastcgi_pass        172.17.0.40:32775;
           include             /etc/nginx/fastcgi_params;
       }
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
       }
   }
}

最后 fastcgi_params

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
fastcgi_param  PATH_INFO         "";
fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;

更新:

我在消息来源中发现,如果您选择任何 ip...,TcpSocket 正在分配 Loopback ip...这很奇怪...

class TcpSocket : StandardSocket {
    public TcpSocket (System.Net.IPEndPoint localEndPoint)
        : base (System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream,
                System.Net.Sockets.ProtocolType.IP, localEndPoint)
    {
    }

    public TcpSocket (System.Net.IPAddress address, int port)
        : this (new System.Net.IPEndPoint (Equals(address, System.Net.IPAddress.Any) ?
            System.Net.IPAddress.Loopback : address, port))
    {
    }
}

无论如何,我已经尝试直接为单声道服务器使用设置 ip 地址(通过 linking nginx 容器到单声道容器)

fastcgi-mono-server4 /printlog=True /applications=/:/var/www/websharper/ /socket=tcp:$(printenv NGINX_HOST_PORT_80_TCP_ADDR):9000 /loglevels=All /verbose=True

和运行用

装箱子
docker run --name t2 --link t1:nginx_host -it -P vasylpurchel/docker-websharper

其中 t1 是一个装有 nginx 运行ning 的盒子 此测试也失败了:

root@dc540db6577f:/#     8: 1 fastcgi-mono-server [2015-10-02 14:32:05.147774] Debug  : fastcgi-mono-server4
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.154128] Debug  : Uid 0, euid 0, gid 0, egid 0
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.155000] Debug  : Root directory: /
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.156562] Notice : Adding applications '/:/var/www/websharper/'...
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.157182] Notice : Registering application:
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.157457] Notice :     Host:          any
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.157908] Notice :     Port:          any
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.158195] Notice :     Virtual path:  /
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.158489] Notice :     Physical path: /var/www/websharper/
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.169995] Debug  : Parsed tcp:172.17.0.5:9000 as URI tcp:172.17.0.5:9000
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.181117] Debug  : Listening on port: 9000
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.182573] Debug  : Listening on address: 172.17.0.5
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.186638] Debug  : Max connections: 1024
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.187418] Debug  : Max requests: 1024
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.187767] Debug  : Multiplex connections: False
8: 1 fastcgi-mono-server [2015-10-02 14:32:05.192868] Error  : Failed to start server Mono.WebServer.FastCgi.Sockets.    TcpSocket: The requested address is not valid in this context (10049)
Unhandled Exception:
System.Net.Sockets.SocketException: The requested address is not valid in this context
at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end) [0x00000] in <filename unknown>:0
at Mono.WebServer.FastCgi.Sockets.StandardSocket.Listen (Int32 backlog) [0x00000] in <filename unknown>:0
at Mono.WebServer.FastCgi.GenericServer`1[Mono.WebServer.FastCgi.ConnectionProxy].Start (Boolean background, Int32 backlog) [    0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.Net.Sockets.SocketException: The requested address is not valid in this context
at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end) [0x00000] in <filename unknown>:0
at Mono.WebServer.FastCgi.Sockets.StandardSocket.Listen (Int32 backlog) [0x00000] in <filename unknown>:0
at Mono.WebServer.FastCgi.GenericServer`1[Mono.WebServer.FastCgi.ConnectionProxy].Start (Boolean background, Int32 backlog) [0x00000] in <filename unknown>:0

地址 172.17.0.5 是 nginx 运行正在运行的容器的 IP 地址。

fastcgi-mono-server4在使用参数

时对docker容器的监听地址有限制
  • /socket=tcp:9000 只在本地监听
  • /socket=tcp:0.0.0.0:9000 只在本地监听,奇怪但真实

要修复 fastcgi-mono-server4 行为,您必须在套接字参数中使用 docker 容器 IP 地址

  • /socket=tcp:IPContainer:9000
  • /socket=tcp:$(ip -4 addr show eth0| grep -Po 'inet \K[\d.]+'):9000

您需要修复 Docker 容器内的命令

fastcgi-mono-server4 /applications=/:/var/www/websharper/ /socket=tcp:$(ip -4 addr show eth0| grep -Po 'inet \K[\d.]+'):9000

这是Docker文件修复的示例

FROM mono:latest

RUN apt-get update
RUN apt-get install -y mono-fastcgi-server4
RUN rm -rf /var/lib/apt/lists/* /tmp/*

RUN echo "#!/bin/sh\nfastcgi-mono-server4 /applications=/:/var/www/websharper/ /socket=tcp:$(ip -4 addr show eth0| grep -Po 'inet \K[\d.]+'):9000" > /opt/mono-fastcgi
RUN chmod +x /opt/mono-fastcgi

EXPOSE 9000

CMD [ "/opt/mono-fastcgi" ]