'Django subdomain rewrite using uwsgi on Google Cloud Run
I am moving a Django site to GCP and using Google Cloud Run for the first time. The experience is great. I was also very pleased with the domain mapping feature, but I am having trouble with forwarding the www subdomain to the naked domain URL. For consistent page analytics, I would like visitors who browse to https://www.mycooldomainname.com
to be 301 redirected to https://mycooldomainname.com
(real domain redacted)
DNS setup
I added the naked and www subdomain to Cloud Run custom domains:
And added the required A, AAA and CNAME records at my registrar:
After everything propagated I can visit my site at https://mycooldomainname.com
and https://www.mycooldomainname.com
, but how can I configure the forwarding/rewriting?
From other posts I have read where people achieved this with Google Cloud DNS, it looks like they used an alias to do the forwarding.
In this case, I am trying to do the forwarding via uwsgi:
Webserver setup
The django app is served via uwsgi with this configuration - notice the redirect rule:
[uwsgi]
plugins-dir = /usr/lib/uwsgi/plugins
plugins = router_uwsgi
route-uri = ^(.*)\/\/www\.(.*)$ redirect-301:$1//$2
hook-master-start = unix_signal:15 gracefully_kill_them_all
http-socket = 0.0.0.0:8000
cheaper = 1
cheaper-algo = backlog
workers = 3
master = true
enable-threads = true
threads = 12
offload-threads = 12
harakiri = 300
http-harakiri = 300
logformat = %(var.HTTP_X_REAL_IP) - %(user) [%(ltime)] "%(method) %(uri) %(proto)" %(status) %(size) "%(referer)" "%(uagent)\" %(msecs)ms
static-map = /=webroot
static-gzip-all = true
module = app.wsgi
The actual redirect works fine on other parts of the url, for example, if I include the following redirect test rule:
route-uri = ^(.*)est(.*)$ redirect-301:$1ust$2
browsing to https://www.mycooldomainname.com/test/
correctly redirects to https://www.mycooldomainname.com/tust/
So at this point I'm not sure if there is a GCP limitation on redirecting parts of a verified domain or my regex is wrong.
Solution 1:[1]
Thanks for all the advice! Here is what I learned:
- DNS records can not be used for redirecting especially if the CNAME of a subdomain is used by GCP
- Google Cloud Run does not limit URL redirecting in any way
- uWSGI has powerful features, but they are hard to discover
- The following redirect rule worked for me:
[uwsgi]
plugins-dir = /usr/lib/uwsgi/plugins
plugins = router_uwsgi
route-host = ^www.mycooldomainname.com$ redirect-permanent:https://mycooldomainname.com${REQUEST_URI}
And I needed to install the following in my base image to get the internal rewriting and plugins to be available:
FROM $PYTHON_IMAGE as base
RUN apt-get update \
&& \
apt-get install --no-install-recommends -y \
libpcre3-dev zlib1g-dev \
uwsgi-core \
&& \
apt-get clean && rm -rf /var/lib/apt/lists/*
Solution 2:[2]
I believe you should be able to configure this using Google Load Balancer.
See:
https://stackoverflow.com/a/63987798 and https://stackoverflow.com/a/67207300
Beware of not including the http/https as per the second answer above.
There is also a helpful Medium walkthough with a tonne more steps for before and after:
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 | Jannie Theunissen |
Solution 2 | Arun Mahtani |