0

I need to get the URL from the dotnet-core webapi from where the api is called. The api itself is hosted behind a nginx-proxy in a specific location:

nginx config:

server {
    server_name my.domain.com;
    root /var/www/html/someotherapp;
    index index.php;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log info;

    location /myapi/ {
            proxy_pass http://localhost:8181/;
            proxy_buffering off;
            proxy_read_timeout 7200;

            proxy_set_header Upgrade           $http_upgrade;
            proxy_set_header Connection        "upgrade";
            proxy_set_header Host              $host;
            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_set_header X-Forwarded-Host  $host;
            proxy_set_header X-Forwarded-Port  $server_port;

    }
    location / {
            index index.html;
    }

listen 443 ssl; # managed by Certbot
}

dotnet api:

in startup.configureservices:

services.Configure<ForwardedHeadersOptions>(options =>
{
  options.ForwardedHeaders =
    ForwardedHeaders.All;
});

and startup.configure

app.UseForwardedHeaders();

I can call this app now via

https://my.domain.com/myapi/api/testapi?myquery=test

And while trying to get the URL I'm called with in the controller with something like

$"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{HttpContext.Request.Path}{HttpContext.Request.QueryString}";

I won't the the location, where the proxy is forwarding, so the result is always

https://my.domain.com/api/testapi?myquery=test

missing the "myapi" location.

Any Idea how to get the location as well?

MADMap
  • 3,132
  • 3
  • 25
  • 31
  • 1
    Read [this](https://stackoverflow.com/questions/53649885/a-little-confused-about-trailing-slash-behavior-in-nginx) Q/A. – Ivan Shatsky Jun 15 '20 at 10:00
  • Thanks, this helps: but for this to work, I need to adapt all routes in the dotenet-core application. I was hoping this was not neccesairy... – MADMap Jun 15 '20 at 13:11

0 Answers0