从Nginx代理后面的dotnet核心获取gull请求URL

时间:2020-06-15 08:13:49

标签: nginx asp.net-web-api .net-core nginx-reverse-proxy

我需要从调用api的dotnet-core webapi获取URL。 该API本身托管在特定位置的Nginx代理后面:

nginx配置:

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:

在startup.configureservices中:

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

和startup.configure

app.UseForwardedHeaders();

我现在可以通过以下方式调用此应用

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

在尝试获取类似我在控制器中被调用的URL时

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

我不是代理转发的位置,所以结果始终是

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

缺少“ myapi”位置。

有任何想法如何获取位置吗?

0 个答案:

没有答案