'How to set the base path or the ApplicationPath for ASP.NET MVC application
I have an ASP.NET MVC web application which I want to put behind NGINX reverse proxy. Currently the web application is served from the domain's root, i.e., http://www.example.com/
, and it works fine.
Now, I am looking to move the application behind NGINX such that the application can be served from something like: http://www.example.com/myapp/
I am not able to figure our how to set the base path or the application path of the application to /myapp
so that the NGINX rule for it would work. The rule I have in NGINX is something like:
location /myapp/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:7654;
}
I have tried setting app.UsePathBase("/myapp");
in Configure
method of my Startup.cs
, but it did not help. The prefix /myapp
is not getting added to the links that are served out of my web application. Is it incorrect for me to expect a URL such as <a href="/foo">Foo</a>
to be automatically treated as href="/myapp/foo"
when sent to the client?
Any suggestions will be greatly appreciated. Thank you!
Solution 1:[1]
Two things you have to do : in the conf file :
location /zola/{
proxy_pass http://localhost:5000/zola/;
}
in the Startup.cs
public void Configure(IaaplicationBuilder app, IWebHostEnvironment env)
{
app.UsePathBase("/zola");
...
...
}
Publish and run the exe of the web app and restart nginx. In fact you can set multiple base paths like show below
public void Configure(IaaplicationBuilder app, IWebHostEnvironment env)
{
app.UsePathBase("/zola");
app.UsePathBase("/kola");
...
...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dr.Sai |