网络上的很多信息都告诉我们如何在同一台机器上设置 nginx + fastcgi-mono-server4 以从 linux 环境托管 ASP.net(目前我正在使用带有 Websharper 的 F# 应用程序)。但是 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"
找到一篇关于类似要求的文章,但适用于 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 和端口。如果访问第一个容器 - 应用程序工作,访问第二个容器 - 与上述相同的错误。
用于运行单声道服务器的命令:
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;
更新:
如果您选择 Any 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 地址(通过将 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 的盒子这个测试也失败了:
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。