繁体   English   中英

如何使用 apache web 服务器反向代理到谷歌云运行服务?

[英]How to reverse-proxy using apache web server to google cloud run services?

我有一些由 HTTPS 保护的谷歌云运行服务端点。 我想设置我的 apache 网络服务器以反向代理到服务中,以便使用我定义的 URL 的人取回服务响应。 我曾尝试使用 mod_proxy 和 mod_rewrite 代理服务端点,但它给了我 500 内部服务器错误。 怎么做到呢? 最坏的情况请为此分享 nginx 解决方案。 我试过的配置:

<VirtualHost *:80>
ServerName hello.world.com
ServerAlias hello.world.com
RewriteEngine On
RewriteRule ^ https://helloworld-zxtb3wfs2a-de.a.run.app [P]
</VirtualHost>

顺便说一句,端点是一个网站,而不仅仅是一个简单的 JSON 响应。 尽管即使是 JSON 响应也不适合我。

在将查询重写回 Cloud Run 端点 (*.run.app) 时,您需要确保更新Host header 以匹配 that.run.app 域名。 否则,Cloud Run 的前端 IP 将不知道将其发送到哪里。

检查this question this question on how to do this with mod_rewrite,并确保您使用ProxyPreserveHost Off

此外,由于您获得的是 HTTP 500,请确保检查应用程序日志以查看应用程序处理此请求的方式是否有问题。

我假设您已经使用 --ingress internal 选项部署了云运行服务,因此没有反向代理就没有人可以访问 Cloud Run。

按照以下步骤在云运行服务前创建反向代理。 如果您没有安装自签名 SSL 并且您配置了自己的 SSL,则可以跳过步骤 2 - 5

  1. 启动 Ubuntu 计算引擎并安装 apache2
  2. 安装自签名 SSL 使用
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt
  1. 通过替换 /etc/apache2/sites-available/default-ssl.conf 中的以下两行来更新 SSL conf
SSLCertificateFile      /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
  1. 启用必要的模块
sudo a2enmod ssl
sudo a2enmod headers
sudo a2ensite default-ssl
  1. 检查配置并重新启动服务器,然后检查是否可以加载 https URL https://your-server-public-ip
sudo apache2ctl configtest
sudo systemctl restart apache2

  1. 使用此内容更新 /etc/apache2/sites-available/000-default.conf,不要忘记替换 CLOUD-RUN-URL
<VirtualHost *:80>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    ProxyPreserveHost On
    ProxyPass / https://CLOUD-RUN-URL-as.a.run.app/
    ProxyPassReverse / https:///CLOUD-RUN-URL-as.a.run.app/
</VirtualHost>
  1. 使用此内容更新 /etc/apache2/sites-available/default-ssl.conf,不要忘记替换 CLOUD-RUN-URL
<IfModule mod_ssl.c>
        <VirtualHost _default_:443>
                ServerAdmin webmaster@localhost

                ErrorLog ${APACHE_LOG_DIR}/error.log
                CustomLog ${APACHE_LOG_DIR}/access.log combined

                SSLProxyEngine On
                ProxyPass / https://CLOUD-RUN-URL-as.a.run.app/
                ProxyPassReverse / https://CLOUD-RUN-URL-as.a.run.app/

                SSLCertificateFile      /etc/ssl/certs/apache-selfsigned.crt
                SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key

                <FilesMatch "\.(cgi|shtml|phtml|php)$">
                                SSLOptions +StdEnvVars
                </FilesMatch>
                <Directory /usr/lib/cgi-bin>
                                SSLOptions +StdEnvVars
                </Directory>

        </VirtualHost>
</IfModule>
  1. 启用更多模块
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod ssl
  1. 检查配置并重新启动服务器
sudo apache2ctl configtest
sudo systemctl restart apache2
  1. 现在再次加载 https URL https://your-server-public-ip,这次你会得到云运行的响应

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM