I'm trying to setup some of my ASP NET Core services with a Nginx Reverse Proxy but I've been stuck with the same problem for some time now: Getting my service to use the original path for redirects.
I have a service called ConfigCloud, that should be accessed via site.net/api/configcloud. Accessing the service works fine but whenever my service performs a redirect or sends a Url to another service, it never includes the /api/configcloud prefix and this prevents the service from being usable.
This has been quite a headache when it comes to authentication and authorisation as those processes involve redirects and call-backs. For example, site.net/api/configcloud/account/profile will redirect the user to site.net/account/login, which is not desirable. Also, when my Auth provider (Auth0) wants to send a callback, it will send back site.net/callback instead of site.net/api/configcloud/callback.
I have attempted many different Kestrel and Nginx configurations to get this working, including those suggested by Microsoft (Link 1, Link 2).
It's difficult for me to determine whether the problem lies with my Kestrel config or Nginx config.
Initially, I would have thought would have been the answer to this problem but that doesn't seem have any effect.
Environment:
Current Nginx config:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name localhost site.net;
ssl_certificate /etc/letsencrypt/live/site.net/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/site.net/privkey.pem;
# Increase client upload size.
client_max_body_size 10M;
# Reuse ssl sessions, avoids unnecessary handshakes.
ssl_session_tickets on;
location /api/configcloud/ {
proxy_pass https://aurora-configcloud/;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header Referer $http_referer;
# I'm sure some of these are unnecessary but I've just been trying everything.
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-Server $host;
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:$remote_port;
}
# Other services
I would appreciate any assistance on this matter as it has become quite a blocker in trying to learn backend web dev. Thank you.
Edit: I have found a working solution. The following code had to be added to my ASP.NET Core program.
app.Use((httpContext, next) =>
{
httpContext.Request.PathBase = pathBase;
return next();
});
The above code works as you expect a Base URL to work in other environments, unlike ASP NET Core's UsePathBase.
I have found a working solution. The following code had to be added to the ASP.NET Core program.
app.Use((httpContext, next) =>
{
httpContext.Request.PathBase = pathBase;
return next();
});