fastcgi_mono_server 4 通配符主机名

fastcgi_mono_server 4 wildcard hostname

我已经用 fastcgi_mono_server4 配置了 nginx。 在我的 nginx 配置中,我有 2 个主机名:

server {
    listen       80;
    server_name  dev.example.org
    location / {
             root /var/www/dev.example.org/;
             fastcgi_index Default.aspx;
             fastcgi_pass 127.0.0.1:9001;
             include /etc/nginx/fastcgi_params;
        }
}

 server {
        listen       80;
        server_name  *.example.org
        location / {
             root /var/www/example.org/;
             fastcgi_index Default.aspx;
             fastcgi_pass 127.0.0.1:9000;
             include /etc/nginx/fastcgi_params;
        }
    }

nginx 这个配置没问题。 dev 转到一个,所有其他转到另一个。

我已经试过了:

fastcgi-mono-server4 /applications=*.example.org:/:/var/www/example.org/ /socket=tcp:127.0.0.1:9000

但它抛出错误(Uri 解析异常)

更新: 我需要在我的应用程序中获取完整的主机名,例如,如果请求是 abc.example.org,我需要获取 "abc"。 不幸的是,HttpContext.Current.Request.Url 不包含 "abc" 而是导致解析错误的“*”

如果 nginx 将负责将适当的 sub-domains 路由到每个 fastcgi 端口(9000 或 9001),那么您可以在启动单服务器进程时使用通配符域吗?只需使用 * 而不是 '*.example.org'

fastcgi-mono-server4 /applications=*:/:/var/www/example.org/ /socket=tcp:127.0.0.1:9000

更新: 上面的方法可以让两个 Mono 服务器应用程序通过 nginx 监听,但是,如果你调用 HttpContext.Request.Url 在 catch-all 服务器上。这是因为它不喜欢 *.example.org.

中的 *

有两种可能的解决方案,具体取决于您希望在客户端浏览 foo.example.orgbar.example.org 等时从 HttpContext.Request.Url 返回什么

选项1:如果你不关心sub-domain,想看example.org

将第二个 (*.example.org) nginx 服务器配置为 'default_server' 并让它分配一个没有通配符的 server-name 例如

server {
  listen 80 default_server;
  server_name example.org;
  access_log ... } 

使用这些设置,浏览到 foo.example.org/Default.aspx 会加载页面并 HttpContext.Request.Url returns example.org/Default.aspx

选项 2: 如果您想查看实际的 sub-domain 例如foo.example.org

从第二个服务器定义中删除 server_name 有效。

server {
  listen 80 default_server;
  access_log ... }

使用这些设置,浏览至 foo.example.org/Default.aspx 会加载页面并 HttpContext.Request.Url returns foo.example.org/Default.aspx

@stephen的回答比较简单,不需要修改fastcgi配置

我试过以前的答案(更新前),但没有用。 正如@stephen 所说,Nginx 负责路由,并且路由部分有效。

为了启动 fastcgi 我使用了这个命令来匹配所有路由(和服务器名称)

fastcgi-mono-server4 /applications=/:/var/www/example.org/ /socket=tcp:127.0.0.1:9000

问题是 HttpContext.Request.Url 包含 $server_name 值,在我的例子中它是“*.example.org”,当我尝试解析 URI 时出现错误。

为了处理这个问题,我更改了 nginx fastcgi_params 并替换了 thi 行

fastcgi_param  SERVER_NAME        $server_name;

来自

fastcgi_param  SERVER_NAME        $http_host;

并在 site-available 配置文件中添加

proxy_set_header Host $host;

我认为这是默认设置。

重新加载 nginx

nginx -t && service nginx reload

重新加载fastcgi-mono-server以测试

 fastcgi-mono-server4 /applications=/://var/www/example.org/ /socket=tcp:127.0.0.1:9000 /printlog=True /loglevels=Debug

在日志 SERVER_NAME 中包含真正的(不是 *)子域。