Madagaga
Madagaga

Reputation: 879

fastcgi_mono_server 4 wildcard hostname

I have configured nginx with fastcgi_mono_server4. In my nginx config I have 2 hostnames :

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 is OK with this configuration. dev goes to one and all other to another one .

I've already tried this :

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

but it throws an error (Uri parse exception)

Update : I need to get the full host name in my application, for example if the request was abc.example.org, I need to get "abc". Unfortunately, HttpContext.Current.Request.Url does not contains "abc" but "*" which causes the parse error

Upvotes: 0

Views: 172

Answers (2)

Madagaga
Madagaga

Reputation: 879

@stephen's answer is more simple and does not need fastcgi config modification.

I tried previous answer (before update), but it did not work. Nginx take care of routing, as @stephen said, and the routing part worked.

to start fastcgi I used this command to match all routes (and server names)

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

The problem was that HttpContext.Request.Url contains the $server_name value in my case it was "*.example.org" and when I try to parse URI there was an error.

To handle this I changed nginx fastcgi_params and replaced thi line

fastcgi_param  SERVER_NAME        $server_name;

by

fastcgi_param  SERVER_NAME        $http_host;

and add in site-available conf

proxy_set_header Host $host;

I think it is set by default.

reload nginx

nginx -t && service nginx reload

reload fastcgi-mono-server to test

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

in the log SERVER_NAME contains the real (not *) subomain.

Upvotes: 0

stephen
stephen

Reputation: 1200

If nginx is going to take care of routing the appropriate sub-domains to each fastcgi port (9000 or 9001) then can you get away with a wildcard domain when you start the mono server process e.g. just use a * instead of '*.example.org'

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

Update: The above works to get two Mono server apps listening via nginx, but, using the nginx config from the original question will lead to an exception if you call HttpContext.Request.Url on the catch-all server. This is due to it not liking the * in *.example.org.

There are two possible solutions, depending what you'd like to see returned from HttpContext.Request.Url when a client browses foo.example.org, bar.example.org etc.

Option 1: If you don't care about the sub-domain and want to see example.org

Configure the second (*.example.org) nginx server to be the 'default_server' and have it assign a server-name without the wildcard e.g.

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

With these settings, browsing to foo.example.org/Default.aspx loads the page and HttpContext.Request.Url returns example.org/Default.aspx

Option 2: If you want to see the actual sub-domain e.g. foo.example.org

Removing the server_name from the second server definition works.

server {
  listen 80 default_server;
  access_log ... }

With these settings, browsing to foo.example.org/Default.aspx loads the page and HttpContext.Request.Url returns foo.example.org/Default.aspx

Upvotes: 1

Related Questions