-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for proxying rocket in a subdirectory #828
Comments
Another workaround might be to mount everything in the rocket application starting at "/live-coding/" (thus matching the client's perceived path), and
I agree that support for an "alternative root" would be convenient, but it can be very tricky to get right. I think only mounting and uri generation care about this, but it is a pretty significant change that will likely be difficult to test.
That's an interesting idea. You should be able to do this yourself now with Template::custom, maybe pulling from your own environment variable or config. Such a helper would have to be available as a free function to remain engine-agnostic, but I suppose we could expose it in handlebars/tera.
It looks like those only rewrite various headers coming from the server, so I'm not understanding how they help in this case. |
You'd still have to manually pass What I would recommend for now is to:
This would replicate what we'd do internally were we to add support for this feature. I think this is a fairly reasonable requests, but like @jebrosen, I'm concerned about getting it right everywhere, and moreover, about the maintainability of the feature. There are some upcoming features that might make this easier to implement, so I'll leave this open until at least then. |
The one thing I'd add to this is that it should ideally support the prefix being configurable (in |
@jonhoo To accomplish that, instead of defining a One big issue with supporting this in Rocket proper is that it'll be the only piece of global state that Rocket "maintains". Because Because of this, I'd have a really tough time justifying the feature in core. I'd be more inclined to offer this in [global]
uri_root = "/live-coding" #[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
// presumably `Template` would need a `rooted_templates` feature or something.
use rocket_contrib::rooted::{Rooted, Template};
#[get("/foo")]
fn foo() { ... }
fn main() {
rocket::ignite()
.attach(Rooted::fairing())
.attach(Template::fairing() /* or Template::custom(..) */)
}
// uri!(foo) returns "/live-coding/foo".
// uri_root() in templates returns "/live-coding"
// uri_rooted(path) in templates returns "/live-coding/$path" |
Note that this doesn't actually work, since |
My preference would be to allow setting the prefix/base path on This prefix would then be included in the request URI (and typically in any generated URIs in the response), but crucially would be ignored for routing purposes. This would support use-cases where the application doesn't know/care about the prefix ahead of time, so you can e.g. add a fairing to read the prefix from an This would also be very useful for me in rocket-lamb which adapts rocket webservers to run in AWS lambda, typically under a base path configured in AWS API Gateway. Currently it hackily achieves this by cloning all routes and re-mounting them under the base path when the first request is received, which is only possible because rocket isn't handling the actual HTTP request in this case, so the |
I recently built a small web app using rocket, and want to host it in a subdirectory of my regular website. My website runs nginx, so I basically want this rule:
Unfortunately, this doesn't work very well. Absolute URLs in my templates now go to the root of my main website, as opposed to being relative to Rocket. I can mostly get around this by using
<base href>
, but that falls down when it comes to redirects, as they need to be absolute URLs and do not take the<base href>
into account.Ideally, I'd like to be able to inform Rocket of where it is running (a
ROOT_URL
field if you will), and have that automatically be integrated with redirects. It'd be awesome if there was also a link helper I could use in templates and elsewhere to produce the right absolute URLs so I wouldn't need<base>
. Alternatively, for nginx specifically, you can use<base href>
+proxy_redirect
, and for Apache you can useProxyPassReverse
, but this seems brittle (maybe it could be documented somewhere?).There is precedence for this kind of feature in other frameworks. For example, Rails supports this through
relative_url_root
. Django supports it withFORCE_SCRIPT_NAME
.tThe text was updated successfully, but these errors were encountered: