fastcgi-mono-server4和nginx with docker

时间:2015-10-01 11:22:37

标签: nginx mono docker fastcgi fastcgi-mono-server

网上的大量信息告诉我们如何在同一台机器上设置nginx + fastcgi-mono-server4来托管ASP.net(目前我正在使用带有Websharper的F#应用程序)来自linux环境。但是docker应该只运行一个进程。还有解决方案,使用runit或其他工具在同一容器上运行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),它只与链接容器有关。但对于fastcgi-mono-server4,它没有。

我尝试使用fastcgi-mono-server4 +应用程序文件创建1个容器,公开一个VOLUME的应用程序文件,公开端口9000,然后使用第一个容器中的卷来暴露nginx容器并链接到它 还尝试运行安装了nginx和mono服务器的2个相同容器,首先使用fastcgi_pass 127.0.0.1:9000,然后使用fastcgi_pass 172.17.0.40:32775 - 第一个容器中的ip和端口。如果访问第一个容器 - 应用程序工作,访问第二个容器 - 与上面相同的错误。

Comand用于运行单声道服务器:

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 image上安装:

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;

更新

我在源代码中发现TcpSocket正在分配Loopback ip,如果你选择Any 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))
    {
    }
}

无论如何我试图直接使用set ip address for mono server(通过将nginx容器连接到mono容器)

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的框 该测试也失败了:

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的容器的IpAddress。

1 个答案:

答案 0 :(得分:3)

使用参数

时,fastcgi-mono-server4对docker容器有一个限制,带有监听地址
  • / socket = tcp:9000只能在localhost中收听
  • /socket=tcp:0.0.0.0:9000只能在localhost中收听,很奇怪但却是真的

要修复fastcgi-mono-server4的行为,你必须在socket参数中使用docker容器的IP地址

  • /插座= TCP:IPContainer:9000
  • / socket = tcp:$(ip -4 addr show eth0 | grep -Po&#39; inet \ K [\ d。] +&#39;):9000

您需要在Docker容器中修复命令

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

这是Dockerfile修复的一个例子

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" ]