mirror of
https://github.com/healthchecks/healthchecks.git
synced 2026-04-25 23:15:49 +03:00
[GH-ISSUE #273] Running in production on a subdomain /app/ possible? #203
Labels
No labels
bug
bug
bug
feature
good-first-issue
new integration
pull-request
question
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/healthchecks#203
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @netopsengineer on GitHub (Aug 9, 2019).
Original GitHub issue: https://github.com/healthchecks/healthchecks/issues/273
Hello,
I have this fantastic app running in a venv, with mod_wsgi setup on Apache, and I need to run it as a subdomain to my main URL, in my case I mapped it to myURL/hc/ and what I am noticing is that several pages don't direct themselves to /hc/ and rather revert to myURL of my existing site.
For instance if I click on an individual "check" it takes me to myURL/checks/ee19e9f1-f727-4a8e-96fd-601cc26d85f6/details/ instead of myURL/hc/check/UUID, but not every page is like this, projects for example works as intended myURL/hc/projects/8195d2d5-af87-4552-a3cd-34886363e7b8/checks/
This is my first shot at turning up a Django app in production and not in debug mode so just wondering what I am missing or if the app is not designed to be used in this way? Please let me know if I can post any other relevant info to troubleshooting. Thank you for taking the time to create such a cool project, looking forward to using it.
@cuu508 commented on GitHub (Aug 12, 2019):
Hello, thank you for reporting this!
To be honest, I've always run the app directly from a domain or a subdomain (not from a subdirectory) and so had not run into this bug myself. But this particular issue should be an easy fix: here the URL is constructed on the client side, without knowledge about the "/hc/" subdirectory.
I intend to fix this, but may take a couple days until I get a chance to do it properly.
@cuu508 commented on GitHub (Aug 12, 2019):
@coxoperationsengineer could you please share your Apache configuration, the part with mod_wsgi and URL rewrites (if any)? Want to make sure my testing setup matches yours.
@netopsengineer commented on GitHub (Aug 12, 2019):
Good morning,
Sure, is this the part that you need? Let me know and I can get whatever you need, admittedly this is the first time I have tried to setup something with mod_wsgi and Django in a venv
There is this file as well since I'm running it in a venv
@cuu508 commented on GitHub (Aug 12, 2019):
Perfect thanks – just got Healtchecks working under Apache & mod_wsgi and can reproduce the issue.
@netopsengineer commented on GitHub (Aug 12, 2019):
@cuu508 While I have you and I'm sorting out the deployment, do you have any recommendations for handling the 'SendAlerts' in production? I haven't been able to find any examples of how that was done, as I tried inside the venv, but I noticed it ran only when I had the venv activated.
We are really enjoying this app, and its changing the way we view future deployments, the idea of doing these Django venv style apps and them being stand alone is really making us think about our traditional HTML/PHP only use of our web server.
@cuu508 commented on GitHub (Aug 12, 2019):
@coxoperationsengineer your virtualenv's
binfolder has apythonbinary. When you run python scripts using that binary it works as if the virtualenv was activated.For example, let's say, your venv is at
/webapps/hc-venvand the healthchecks project is at/webapps/healthchecks. You can run the sendalerts command like so:You can use any task runner / process manager you like. I'm currently using systemd services. An example service file:
(the -u flag is for unbuffered output)
/etc/systemd/system/sendalerts.servicesystemctl daemon-reloadso systemd notices the new servicesystemd restart sendalerts.serviceto start it upsystemd enable sendalerts.serviceto make it start automatically on bootjournalctl -f -u sendalertsto see live logs@cuu508 commented on GitHub (Aug 12, 2019):
Just commited a fix for all instances of incorrect URLs being constructed in JS that I could find. Please let me know if you notice any other breakage – I had not tested running from a subdirectory before, and might have missed something.
@netopsengineer commented on GitHub (Aug 13, 2019):
@cuu508 Thank you for the info on the alerts, I got it working perfectly with systemd.
I cloned over your new files, stashed my files, and brought back my settings files, and most things worked.
A few observations: I have to modify the wsgi.py file because "hc.settings" can't be found, the "fix" was as such:
There are several places I noticed where images don't work because they reference like this:
src="{% site_root %}/static/img/logo-full@2x.png"
The email template is one of those
https://github.com/healthchecks/healthchecks/blob/master/templates/emails/base.html
Also the badges are double appending the root of the URL (/hc/hc/)onto the "tags" and not displaying images either, I haven't been able to figure out where that comes from yet but I'm getting something like:
https://myURL.com/hc/hc/badge/2bcc6bd2-5381-4caa-a295-ce55a811423e/3Kr54ARM/RH_Poller-TPE_Router_OSPF.svg
@cuu508 commented on GitHub (Aug 14, 2019):
I think if you add a
WSGIPythonPathdeclaration to your Apache configuration you would not need to edit wsgi.py and adjust paths there.Here's my configuration:
The paths are different but I'm sure you get the idea. I was referencing https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/modwsgi/
To fix
src="{% site_root %}/static/img/logo-full@2x.png", I adjusted the SITE_ROOT variable in$PROJECT_DIR/hc/local_settings.py:@immanuelfodor commented on GitHub (Sep 28, 2019):
I'm trying to run the project in a domain+folder combo like this one without success:
https://api.xyz.tld/k8s/healthchecksIn my
local_settings.pythere are among others:CSS/JS loads fine but still, the in-template URLs are falling back to
/like/accounts/loginin the HTML which results ending up athttps://api.xyz.tld/accounts/login.I thought of setting a
<base>tag but it doesn't affect links starting with a slash, so I also tried injecting a small JS tag replacing alla.hrefandform.actionattributes, which also feels hacky and incomplete. Some minified JS file still omits this.Oh, and I also tried to inject some Python code into the
hc/urls.pyfile to prepend all URLs withk8s/healthchecksbut it didn't work, this way the app becomes available athttps://api.xyz.tld/k8s/healthchecks/k8s/healthcheckswhich is also not what I want.The entry point of my Kubernetes cluster is at
https://api.xyz.tld/k8sand the/healthchecks/path is managed by Ambassador, so the app's root should be athttps://api.xyz.tld/k8s/healthchecks, it only starts to receive HTTP requests below this point.And by the way, my
Dockerfileis from https://github.com/linuxserver/docker-healthchecks if it helps anything, but I don't think the issue is with that, the app works fine just the links generated on the frontend are problematic.What do you suggest what more should I try to effectively change the base path without too many source code hacks? :)
@cuu508 commented on GitHub (Sep 30, 2019):
Ideally we want this to just work, with no hacks! A couple questions:
@immanuelfodor commented on GitHub (Oct 1, 2019):
Of course, that would be great! 👍
linuxserver/healthchecks:v1.9.0-ls37docker-compose.yml, then converted it to k8s configs withkompose convert, then fine-tuned to my environment (needed to fix the PV to node 4 for example). The below config also contains the latest JS hack I tried to make, it renders the page more or less usable but the forms and the Django internal redirects for login/logout still don't work, many manual URL overwrites are needed in the browser.Applied all of them, then manually edited the
local_settings.pyon the PV to have the below content (empty user and pass for SMTP but this is unrelated, and the static URL), then deleted the pod, to apply it:I tried setting only https://api.domain.tld as SITE_ROOT and then adding the aforementioned
echo -e "\nurlpatterns = [path('k8s/healthchecks/', include(urlpatterns))]" >> /app/healthchecks/hc/urls.pycommand to the deployment arg as another hack, but as I said, it also didn't work out well.At the root of
api.domain.tldthere is an nginx reverse proxy, which has a configuration that any/k8s/route should be passed to the cluster, then the Ambassador annotation on the service YAML takes care of the/healthchecks/part to let me access the deployment in the cluster. I don't have any other config set up related to Healthchecks.As the pod only handles
<this part>in the URL:https://api.domain.tld/k8s/healthchecks/<this part>, I feel like theSITE_ROOTenv is not honored somewhere in Django/templates/etc. This is why I think this is rather an app issue, not a Linuxserver Docker issue.@cuu508 commented on GitHub (Oct 1, 2019):
Thanks for the extra details!
Author of the original ticket is using Apache + mod_wsgi. In this setup, mod_wsgi sets a SCRIPT_NAME environment variable with the URL prefix. Django knows about this variable and handles things automatically: it takes the URL prefix in account when resolving URLs, and it adds the prefix when generating URLs.
The thing I needed to fix there was making sure that
As I understand this is a different situation, because we're not using a CGI-like interface, we are using a reverse proxy. The reverse proxy does not set the SCRIPT_NAME variable and so Django doesn't handle URLs correctly. I think this problem is not specific to Healthchecks, you would run into the same issue with any Django app, or possibly any wsgi app. I think the fix is to use Django's FORCE_SCRIPT_NAME setting.
As an experiment, I set up a nginx site like so:
In
local_settings.pyI added:And with that I had the site running at http://localhost/foo/.
This was with the development server, not with uwsgi that linuxserver's dockerfile uses. Now, I'm not sure what's the least painful way to get your stack to do something similar. You would need a way to set the FORCE_SCRIPT_NAME and the STATIC_URL settings, and you might also need to tweak the "static-map" bit in uwsgi.ini.
@immanuelfodor commented on GitHub (Oct 1, 2019):
Wow, that's it, you're a genious! Or at least you know Django better than me 😀
I simply added
FORCE_SCRIPT_NAME = "/k8s/healthchecks"to the end of the previously postedlocal_settings.py, removed the script hack from the deployment, and it works out of the box as expected. Thank you!For me this issue is resolved :)
In the meantime, I managed to enable Apprise as well with
APPRISE_ENABLED = Truein the local settings and applying the below command in the deployment YAML, as I've found out that therequirements.txtdoesn't have it by default and so it falls back to False intransports.py:github.com/healthchecks/healthchecks@ba886e90cb/hc/api/transports.py (L11-L15)It might help others as well :)