Nginx subdirectory hosting (embedded resources)

I’m hosting a .net app with servicestack in it under an /api subdirectory in nginx (wordpress is running on the root of the site).

While the ServiceStack web services work, the metadata page works, none of the embedded resources work (which breaks Swagger-UI, and Admin Viewer).

I tried messing with the HandlerFactoryPath, WebHostUrl, even proxy_pass, but can’t really seem to figure out how to get the embedded resources to work. Is there a simple fix to this?

location /api {
                proxy_pass http://localhost:5000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection keep-alive;
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_buffering off;
                proxy_ignore_client_abort off;
                proxy_intercept_errors on;
                proxy_pass_request_headers on;

Can you try setting HandlerFactoryPath="api" and proxying to:

location /api/ {
    proxy_pass http://localhost:5000/api/;

I did try that, and didn’t work.

But I just found that this was easily resolved by doing this:

location ^~ /api {

Found the tip here

I was thinking that putting /api at the front of the location rules would do it, but turns out Nginx does have some built-in conventions for loading location mappings, and /{whatever} is very low in the pecking order. I have a regex lower in the nginx file for static assets that I think was overriding this one.

Resolved. Sorry 'bout the trouble.

For anyone that runs into this, my settings:

  • nginx config
location ^~ /api {
    proxy_pass http://localhost:5000;
  • ServiceStack HostConfig
config.WebHostUrl = "";
config.HandlerFactoryPath = "api";
config.DefaultRedirectPath = "/api/swagger-ui";
1 Like