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)
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:
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.
Thanks for all the advice! Here is what I learned:
[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/*