Search code examples
asp.netnginxmonoservicestackfastcgi

Versionized URLs in nginx for multiple FastCGI ASP.NET Mono Backends


I'm planning to have multiple ASP.NET applications running with FastCGI behind nginx.

These multiple applications differ in their binaries, and I want nginx to forward requests to the according ASP.NET applications depending on their URI.

The Mono applications themselves are always listening on the root path "/". I assume I'll have to tinker with the location directive and fastcgi_params, but I don't know which fastcgi_params I'd have to adjto change to make it work.

Sample use case:

  • http://www.server.com/api/1/status should be forwarded to the FastCGI application running on TCP port 9000, the request URI should be rewritten to /status.
  • http://www.server.com/api/2/status should be forwarded to the FastCGI application running on TCP port 9001, the request URI should also be rewritten to /status to match the ASP.NET listener configuration.

The /api/{version} part of the URI should be removed when forwarding to FastCGI.

I tried this nginx configuration, ASP.NET was called via FastCGI but couldn't process the request:

location ~ /api/1/(.*) {
    root   /usr/aspnet/;
    fastcgi_pass 127.0.0.1:9000;
    include /etc/nginx/nginx-fastcgi-params.conf;
    fastcgi_param  REQUEST_URI /$1;
}

I'm not sure if my approach with the location directive is correct at all, any ideas?

/Edit: This is the solution which I came up with. No other fastcgi_params are obviously needed:

location ~ ^/api/1/(.*)$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  SCRIPT_NAME        /$1;
}

Solution

  • Unless you know exactly which fastcgi_param parameters the controller is using, you should take control of them and paste the contents of /etc/nginx/nginx-fastcgi-params.conf into the location container, rather than including the file, at least until you get it all working.

    You may need to modify SCRIPT_NAME and DOCUMENT_URI too.

    Your location regex should be prefixed with ^ and suffixed with $ to remove any ambiguity (not that that has anything to do with the problem in hand).