Django子域在Google Cloud Run上使用UWSGI重写
我正在将Django网站移至GCP,并首次使用Google Cloud运行。体验很棒。我也对域映射功能感到非常满意,但是我在将www子域转发到裸域URL方面遇到了麻烦。对于一致的页面分析,我希望访问访问https://www.mycooldomainname.com
将301重定向到https://mymycooldomainname.com
编辑的)
DNS设置
我添加了裸露和www子域中的云运行自定义域:
我的书记官员中添加了所需的A,AAA和CNAME记录:
并在 我可以访问我的网站https://mycooldomainname.com
和https://www.mycooldomainname.com
,但是我如何配置转发/重写?
来自其他帖子我已经阅读了人们使用Google Cloud DNS实现这一目标的地方。
在这种情况下,我正在尝试通过UWSGI进行转发:
WebServer设置
Django应用程序通过UWSGI使用此配置提供 - 请注意重定向规则:
[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
实际重定向在URL的其他部分上正常工作,例如,如果我包括在内,则以下重定向测试规则:
route-uri = ^(.*)est(.*)$ redirect-301:$1ust$2
浏览到https://www.mycooldomainname.com/test/
正确地将重定向到https://www.mycooldomainnname.com/tust/tust/
在这一点上,我不确定是否有GCP限制重定向验证的域的部分或我的正则是错误的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您应该能够使用Google Load Balancer进行配置。
请参阅:
https://stackoverflow.com/a/63987798
和
https://stackoverflow.com/a/67207300
请注意不包括http/https的第二个答案。
还有一个有用的媒介步行,以吨之前和之后的一吨步骤:
https://medium.com/@shivamgrg38/setting-up-httpp-to-to-https----- -non-www-to-www-redirects-for-external-http-s-load-load-balancer-on-b73be558ab5
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:
https://medium.com/@shivamgrg38/setting-up-http-to-https-and-non-www-to-www-redirects-for-external-http-s-load-balancers-on-b73be558eab5
感谢您的所有建议!这是我学到的:
我需要在基本图像中安装以下内容,以使内部重写和插件可用:
Thanks for all the advice! Here is what I learned:
And I needed to install the following in my base image to get the internal rewriting and plugins to be available: