-
Notifications
You must be signed in to change notification settings - Fork 310
Question: best practice Linux + nginx + subfolder in url #815
Comments
|
Can't there just be a middleware in the pipeline that sets the path base on http context? (that's effectively) what happens when you put the path in there anyways. |
There could be, but this is a deployment parameter so it should be easily configurable. That's why we included it in the Server. |
@Weboholics As a workaround add this code to your application's Startup.cs public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Map("/weboholics", ConfigureCore);
}
private void ConfigureCore(IApplicationBuilder app)
{
// Configure your pipeline here
}
} This should unblock you for now. |
@Tratcher You sure about this? It looks to me that both UseUrls and UseIISIntegration clobber the I see that aspnet/IISIntegration#14 is closed with a comment saying this has been fixed by the ASP.NET Core Module. I would like to know how. I don't see anything in IISIntegration that updates the |
It gets the base path from IIS/ANCM via environment variable and gives it to Kestrel to manage. (Note UseSetting and UseUrl do the same thing here). Can we come up with a syntax for the user to specify a base path in UseUrls for the unix sockets scenario? |
Feels like a hack. We should be using different fields for different data even in the default case. |
@davidfowl Thanks. Your workaround seems to work. Looking forward for a final solution. |
Do we want this as a feature? |
Yes pls |
@shirhatti Can you come up with the requirements for 1.1.0? |
@shirhatti Are we doing anything on this in the next few weeks? |
Sorry to see that the milestone has been moved - davidfowl hack is working for us and we appreciate the help received from you. But for people new to .net core this will create problems - the hack IS NOT intuitive. With a major version change to 2.0 it would be natural to fix the API. |
@Weboholics In 2.0 you will be able to do something like the following by using KestrelServerOptions.ListenUnixSocket and app.UsePathBase. It's conceptually pretty similar to @davidfowl's hack, but hopefully more intuitive. Program.Main()var host = new WebHostBuilder()
.UseKestrel(options =>
{
options.ListenUnixSocket("/var/disk2/sparfiler/www/weboholics.sock");
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run(); Startup.Configure(IApplicationBuilder app)app.UsePathBase("/weboholics");
// Configure your pipeline here |
Wait, are we talking about the situation where the Kestrel webserver thinks it's called in the root path of the domain but the public web server is rewriting this into a subdirectory? This can only work with the URL rewriting in place, not without, right? So it's impossible to use and test this with Kestrel alone, you absolutely need the proxy web server for the URL rewriting. If I'm correct until here, how could this possibly be configured in the source code? This isn't a configuration feature of the application itself, it's only a thing of the hosting environment. The application itself just doesn't care or know about the URL remapping outside of it. So this should not be configurable through an API call but only with an environment variable or some other means accessible through the command line or such. At least that's what I need for my hosting environment. I can configure an ASP.NET Core app to run in a public subdirectory of the domain – it just won't work because it thinks it's in the root directory and generates wrong URLs to resources and other pages. |
@ygoe If requests are already being routed to the correct handlers, and you just wan to prefix all your generated URLs with "/weboholics", you can do so by adding the following to the start of your app.Use((context, next) =>
{
context.Request.PathBase = "/weboholics";
return next();
}); Of course, if you change your rewrite rule, you will also need to change the PathBase to match, but there's nothing stopping you from reading the PathBase from config yourself. |
I'm thinking of an application that's not written by me, some CMS for example. I, as the admin, want to be able to install that app in whatever subdirectory I like. Obviously I don't want to recompile that app (written by somebody else, possibly not even available as source code) to fit my subdirectory. And if the app doesn't provide such configuration, it just won't work anywhere else than the root directory. To me this is clearly a hosting environment option, not an application option. And as such it doesn't belong in the code. (Or at least not exclusively. The server TCP port is another such option and it works perfectly from the environment. I can't run multiple apps all on port 5000.) |
The application is self hosted so they are one and the same. You can always write code that reads from configuration and sets up the correct base path as a result. |
I can set the TCP port from the outside, without any specific code in the application itself. I'm just thinking the same should be possible for the URL path.
This might look something like the following:
But that doesn't work last time I checked (ASP.NET Core 2.0 preview 1). |
Yea, that doesn't work. You can't bind to a path (like @halter73 said) but that really has nothing to do with being able to set the url via an environment variable or not. That's because Kestrel itself doesn't support "binding to a path". We should do a few things here:
We're shutting down for 2.0 so this would be a 2.1 thing. /cc @Tratcher |
That sounds good! |
Thanks @halter73 and @davidfowl with the feedback that the most important parts of the API cleanup regarding Subfolders has been resolved. As I understand you can now use app.UsePathBase instead of app.Map. @davidfowl ASPNETCORE_PATHBASE environment variable seems a future (2.1) nice to have feature, but doesn't sound critical - you can use configuration - as we do today to make subfolders configurable without recompilation |
Wait a sec. what problem are we trying to solve here? Because this worked just fine for me. |
@rubberduck203 - your solution doesn't work on Linux using unix sockets - a limitation of method .UseUrls. The original question did spawn further questions about API and how to enable this functionality during deployment / administration - you don't want to recompile an application because changes in hosting. |
Okay. That's what I was missing. The environment variable works for some cases, but not for unix sockets. Got it. |
Kestrel used to allow PathBase in UseUrls, but it dropped it due to incomplete support. That's when we introduced UsePathBase. |
How can I handle it with Apache2? (Ubuntu 16.04) |
Comments on closed issues are not tracked, please open a new issue with the details for your scenario. Did you try UsePathBase? |
Hi,
We are having problem using subfolder in url, example:
http://domainName.com/weboholics
We have tried two approaches:
(What we think is the right way) Rewrite url in nginx
(nginx configuration)
rewrite ^(/weboholics)(.*)$ $2 break;
With this approach razor get confused and @Url.Action("Index","Home") returns:
/Home/Index when the correct url should be
/weboholics/Home/Index
We have asked others, and they have suggested to use WebHostBuilder().UseUrls("http://domainName.com/weboholics"), but seems not feasible due to our usage of Unix Socket
(our UseUrl)
.UseUrls("http://unix:/var/disk2/sparfiler/www/weboholics.sock")
Second approach - change routing
With this approach Razor Url.Action() works but this doesn't work in Razor:
(code)
<script type="application/ecmascript" src="~/ckeditor/ckeditor.js"></script>
Razor will render this to:
<script type="application/ecmascript" src="/ckeditor/ckeditor.js" ></script>
(How it should be)
<script type="application/ecmascript" src="/weboholics/ckeditor/ckeditor.js" ></script>
Our startup (both approaches )
What is the correct approach for using subfolders? especially in an Linux + nginx environment?
Best Regards
Jesse / Weboholics
The text was updated successfully, but these errors were encountered: