Nginx

Mono 通過 nginx 上的 FastCGI

  • April 15, 2012

我正在瀏覽http://www.mono-project.com/FastCGI_Nginx並且無法讓它工作。FastCGI 伺服器似乎正在執行。以下來自錯誤日誌:

上游發送了意外的 FastCGI 記錄:3 在從上游讀取響應標頭時,客戶端:192.168.1.125,伺服器:arch,請求:“GET /Default.aspx HTTP/1.1”,上游:“fastcgi://127.0.0.1:9000” ,主持人:“拱門”

用於啟動伺服器的命令(我嘗試過 server2 和 server4,使用簡單的 .NET 2.0 或 .NET 4.0 項目):

fastcgi-mono-server2 /applications=arch:/:/var/www/test/public/ /socket=tcp:127.0.0.1:9000 /stopable=True

nginx配置:

server
{
   listen 80;
   server_name arch;

   access_log /var/www/test/log/access.log;
   error_log /var/www/test/log/error.log;

   location /
   {
       root /var/www/test/public;
       index index.html index.htm default.aspx Default.aspx;
       fastcgi_index Default.aspx;
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_param  PATH_INFO          "";
       fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
   }
}

使用 xsp4 工作正常,我可以瀏覽該站點。

我啟用了 FastCGI 日誌記錄,這是輸出:

[2012-04-15 23:51:18Z] Debug   Accepting an incoming connection.
[2012-04-15 23:51:18Z] Notice  Beginning to receive records on connection.
[2012-04-15 23:51:18Z] Debug   Record received. (Type: BeginRequest, ID: 1, Length: 8)
[2012-04-15 23:51:18Z] Debug   Record received. (Type: Params, ID: 1, Length: 386)
[2012-04-15 23:51:18Z] Debug   Record received. (Type: Params, ID: 1, Length: 0)
[2012-04-15 23:51:18Z] Debug   Read parameter. (PATH_INFO = )
[2012-04-15 23:51:18Z] Debug   Read parameter. (SCRIPT_FILENAME = /var/www/test/public/Home)
[2012-04-15 23:51:18Z] Debug   Read parameter. (HTTP_HOST = arch)
[2012-04-15 23:51:18Z] Debug   Read parameter. (HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0)
[2012-04-15 23:51:18Z] Debug   Read parameter. (HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8)
[2012-04-15 23:51:18Z] Debug   Read parameter. (HTTP_ACCEPT_LANGUAGE = en-gb,en;q=0.5)
[2012-04-15 23:51:18Z] Debug   Read parameter. (HTTP_ACCEPT_ENCODING = gzip, deflate)
[2012-04-15 23:51:18Z] Debug   Read parameter. (HTTP_CONNECTION = keep-alive)
[2012-04-15 23:51:18Z] Debug   Read parameter. (HTTP_COOKIE = ASP.NET_SessionId=2C3D702C9B0F23F69B80820B)
[2012-04-15 23:51:18Z] Error   Failed to process connection. Reason: Argument cannot be null.
Parameter name: s
[2012-04-15 23:51:18Z] Debug   Record sent. (Type: EndRequest, ID: 1, Length: 8)
[2012-04-15 23:51:18Z] Debug   The FastCGI connection has been closed.

需要兩個額外的 FastCGI 參數。

fastcgi_param SERVER_PORT "80";
fastcgi_param SCRIPT_NAME $fastcgi_script_name;

感謝來自#mono IRC 頻道的 KnyghtMare 的測試腳本。通過排除,找到了 nginx conf 所需的參數。使用說明:您可能需要更改最後一行的 IP 和埠號,以及HTTP_HOST.

SERVER_SOFTWARE="lighttpd/1.4.26" \
SERVER_NAME="127.0.0.1" \
GATEWAY_INTERFACE="CGI/1.1" \
SERVER_PORT="80" \
SERVER_ADDR="127.0.0.1" \
REMOTE_PORT="28886" \
REMOTE_ADDR="127.0.0.1" \
SCRIPT_NAME="/" \
PATH_INFO="" \
SCRIPT_FILENAME="/var/www/" \
DOCUMENT_ROOT="/var/www/" \
REQUEST_URI="/" \
QUERY_STRING="" \
REQUEST_METHOD="GET" \
REDIRECT_STATUS="200" \
SERVER_PROTOCOL="HTTP/1.1" \
HTTP_HOST="arch" \
HTTP_CONNECTION="keep-alive" \
HTTP_CACHE_CONTROL="max-age=0" \
HTTP_USER_AGENT="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.83 Safari/535.11" \
HTTP_ACCEPT="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" \
HTTP_ACCEPT_ENCODING="gzip,deflate" \
HTTP_ACCEPT_LANGUAGE="en-US,en;q=0.8" \
HTTP_ACCEPT_CHARSET="ISO-8859-1,utf-8;q=0.7,*;q=0.3" \
cgi-fcgi -bind -connect 127.0.0.1:9000

經過更多修改以使資源文件正常工作,這是目前配置:

server
{
       listen 80;
       server_name arch;

       access_log /var/www/test/log/access.log;
       error_log /var/www/test/log/error.log debug;

       root /var/www/test/public;

       location /
       {
               try_files $uri @proxy;
       }

       location @proxy
       {
               fastcgi_index /Home;
               fastcgi_pass 127.0.0.1:9000;
               fastcgi_param PATH_INFO "";
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               fastcgi_param SERVER_PORT $server_port;
               fastcgi_param SCRIPT_NAME $fastcgi_script_name;
       }
}

引用自:https://serverfault.com/questions/379702