We have different proxy setups in different environments, some of them have path /app, and some of them have /test/app. This is due to some internal constraints.
Wondering, if we can set the root path in a dynamic way in places like middleware where we can use x-forwarded header to determine the root path? The article only talks about setting up root_path at start time but not at request times
https://fastapi.tiangolo.com/advanced/behind-a-proxy/.
Related
I came into a situation today. Please share your expertise 🙏
I have a project (my-app.com) and one of the features is to generate a status page consisting of different endpoints.
Current Workflow
User login into the system
User creates a status page for one of his sites (e.g.google) and adds different endpoints and components to be included on that page.
System generates a link for a given status page.
For Example. my-app.com/status-page/google
But the user may want to see this page in his custom domain.
For Example. status.google.com
Since this is a custom domain, we need on-demand TLS functionality. For this feature, I used Caddy and is working fine. Caddy is running on our subdomain status.myserver.com and user's custom domain status.google.com has a CNAME to our subdomain status.myserver.com
Besides on-demand TLS, I am also required to do reverse proxy as
shown below.
For Example. status.google.com ->(CNAME)-> status.myserver.com ->(REVERSE_PROXY)-> my-app.com/status-page/google
But Caddy supports only protocol, host, and port format for reverse proxy like my-app.com but my requirement is to support reverse proxy for custom page my-app.com/status-page/google. How can I achieve this? Is there a better alternative to Caddy or a workaround with Caddy?
You're right, since you can't use a path in a reverse-proxy upstream URL, you'd have to do rewrite the request to include the path first, before initiating the reverse-proxy.
Additionally, upstream addresses cannot contain paths or query strings, as that would imply simultaneous rewriting the request while proxying, which behavior is not defined or supported. You may use the rewrite directive should you need this.
So you should be able to use an internal caddy rewrite to add the /status-page/google path to every request. Then you can simply use my-app.com as your Caddy reverse-proxy upstream. This could look like this:
https:// {
rewrite * /status-page/google{path}?{query}
reverse_proxy http://my-app.com
}
You can find out more about all possible Caddy reverse_proxy upstream addresses you can use here: https://caddyserver.com/docs/caddyfile/directives/reverse_proxy#upstream-addresses
However, since you probably can't hard-code the name of the status page (/status-page/google) in your Caddyfile, you could set up a script (e.g. at /status-page) which takes a look at the requested URL, looks up the domain (e.g. status.google.com) in your database, and automatically outputs the correct status-page.
I'm working on an app, with the front(vueJS) and back(symfony with API-platform) separated on the same domain.
We have an NGINX reverse proxy that redirects url base on these rule :
domain.com/ -> front container on port 7000
domain.com/api -> back container on port 8000
domain.com/preview-mrXXX/ -> front container on port 7XXX
domain.com/api/preview-mrXXX/ -> back container on port 8XXX
The problem is that the symfony assets aren't found because the request are on the root url and not the api or preview url.
e.g:
request goes to https://example.com/bundles/apiplatform/web.png
instead of https://example.com/api/bundles/apiplatform/web.png
I could pass a base_url in some header with nginx but I still need to configure symfony to requests on /api/ rather than /.
You can set the base_url for assets in each container for the Framework configuration file. (from the documentation https://symfony.com/doc/current/reference/configuration/framework.html#base-urls).
Since you need it to be dynamic, you can create an .env.local file in each container with the base_url for that particular container. Then in the Framework configuration file mentioned above, you can set the value to %base_url%(the variable you created in the .env.local file) so that it will dynamically resolve to the correct value based on your env. You can refer to more information on env configuration here. https://symfony.com/doc/current/configuration.html
UPDATE: You can also set the value to a real environment variable (from your shell or OS) by using '%env(resolve:ENV_VAR)%' (therefore avoiding defining it in the .env.local file). Relevant documentation: https://symfony.com/doc/current/configuration.html#configuration-based-on-environment-variables
web.xml's <error-page> allows a developer to specify what to return to client in case of some error (either HTTP status or java exception).
But I have 2 different 404 error pages, per locale.
My web application is structured so that all resources for locale A is under path /a/; resources for locale B under path /b/.
I'd like to have a localized error page for 404 when trying to access pages under each locale (to be clear, trying to access /a/some-undefined-resource should return 404 + an error page localized for locale A).
Given other limitations, it is not really possible to deploy 2 separate applications, a.war and b.war for each locale.
How can I serve an error page that depends on original resource requested?
I ended giving up the idea to use <error-page> to serve my SPA and used the urlrewrite filter to rewrite URLs to either /a/index.html or /b/index.html.
The downside is that now I have two places to edit when I add new routes inside my SPA:
angular routes (app-routing.module.ts), and
urlrewrite.xml.
Besides, if I add a new locale in the future, I'll need to add a new set of rules to cover all my routes.
Also I don't know how this filter will impact my site's performance. Since it is a low traffic project, I'll keep it this way until I find a better solution.
There is an upside too: now those - otherwise legal - requests are served with status 200, and really missing resources are signaled with 404 and no default content included.
When we send an HTTP request to a web server as to load a web page e.g. http://wwww.nothing_is_here.com, what exactly the server does as to serve our request? Till now, I thought the server was looking to find a file named index (index.html, index.php) which should have HTML content and send it back to our browser. Now, I know this is not always the case. For example, in ASP .NET where we apply routing, home/index path is added to the URL by default as for our app to be routed. That's I cannot understand is how exactly the server acts upon a similar situation. Why it does not return an error message in case there is no index file, how it knows it has to apply routing rules? How can we instruct the server what to do in either case?
What the server does when it receives a request for the root (which is what it will receive in your example), is up to the server configuration. It is not within the control of the client making the request.
The server will be configured to have a default document (file name) in such cases, which is often index.html, but equally could be any file set by the administrator of the server.
The server will often be configured to recognise different hosts (e.g. if it's serving multiple sites on the same interface:port. These different sites will often have different configuration about what the default file name(s) is/are (if any). In some cases the server is configured to display a directory listing of a server-configured site root folder.
I am running behind a CDN that turns my https request into http and sets X-Forwarded-Proto.
I know in nginx I can create a rule that rewrites my request as
localhost:8080/VirtualRequestBase/https/realsite.com:80/VirtualHostRoot/siteroot so that urls come out right.
However I have a zope setup where I keep many sites and I don't want to have to change the nginx config every time I add a new one. I normally use the VHM mappings tab to map domains to siteroots, however it will ignore the VirtualHostBase directive in the path. In addition you can mix the two styles of VHM, ie set localhost:8080/VirtualRequestBase/https/realsite.com:80/ and expect it use the mappings.
Any ideas?