Django子域在Google Cloud Run上使用UWSGI重写

发布于 2025-01-28 20:13:33 字数 1543 浏览 6 评论 0原文

我正在将Django网站移至GCP,并首次使用Google Cloud运行。体验很棒。我也对域映射功能感到非常满意,但是我在将www子域转发到裸域URL方面遇到了麻烦。对于一致的页面分析,我希望访问访问https://www.mycooldomainname.com将301重定向到https://mymycooldomainname.com编辑的)

DNS设置

我添加了裸露和www子域中的云运行自定义域:

我的书记官员中添加了所需的A,AAA和CNAME记录:

并在 我可以访问我的网站https://mycooldomainname.comhttps://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:

gcr

And added the required A, AAA and CNAME records at my registrar:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮云壑 2025-02-04 20:13:33

我相信您应该能够使用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

旧竹 2025-02-04 20:13:33

感谢您的所有建议!这是我学到的:

  1. DNS记录不能用于重定向,尤其是如果GCP
  2. Google Cloud Run使用子域的CNAME不会限制
  3. UWSGI具有强大功能的URL重定向,但是它们很难发现
  4. 以下重定向规则对我有用:
[uwsgi]
plugins-dir = /usr/lib/uwsgi/plugins
plugins = router_uwsgi
route-host = ^www.mycooldomainname.com$ redirect-permanent:https://mycooldomainname.com${REQUEST_URI}

我需要在基本图像中安装以下内容,以使内部重写和插件可用:

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/*

Thanks for all the advice! Here is what I learned:

  1. DNS records can not be used for redirecting especially if the CNAME of a subdomain is used by GCP
  2. Google Cloud Run does not limit URL redirecting in any way
  3. uWSGI has powerful features, but they are hard to discover
  4. 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/*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文