简体   繁体   English

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

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

I have got some google cloud run service endpoints that are secured by HTTPS.我有一些由 HTTPS 保护的谷歌云运行服务端点。 I want to set up my apache webserver to reverse proxy into the services so that people using my defined URL get back the service response.我想设置我的 apache 网络服务器以反向代理到服务中,以便使用我定义的 URL 的人取回服务响应。 I have tried to use mod_proxy and mod_rewrite to proxy the service endpoint but it gives me 500 internal server error.我曾尝试使用 mod_proxy 和 mod_rewrite 代理服务端点,但它给了我 500 内部服务器错误。 How can it be done?怎么做到呢? Worst case please share an nginx solution for this.最坏的情况请为此分享 nginx 解决方案。 Config I tried:我试过的配置:

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

BTW the endpoint is a website and not just a simple JSON response.顺便说一句,端点是一个网站,而不仅仅是一个简单的 JSON 响应。 Though even JSON responses are not working for me either.尽管即使是 JSON 响应也不适合我。

While rewriting the query back to the Cloud Run endpoint (*.run.app), you need to make sure you update Host header to match to that.run.app domain name as well.在将查询重写回 Cloud Run 端点 (*.run.app) 时,您需要确保更新Host header 以匹配 that.run.app 域名。 Otherwise, Cloud Run's frontend IP won't know where to send that.否则,Cloud Run 的前端 IP 将不知道将其发送到哪里。

Check this question this question on how to do this with mod_rewrite, and make sure you use ProxyPreserveHost Off .检查this question this question on how to do this with mod_rewrite,并确保您使用ProxyPreserveHost Off

Also since you're getting HTTP 500, make sure you check the application logs to see if there's something wrong with how the app handles this request.此外,由于您获得的是 HTTP 500,请确保检查应用程序日志以查看应用程序处理此请求的方式是否有问题。

I assume that you have already deployed your cloud run service with --ingress internal option, so that no one can access Cloud Run without the reverse proxy.我假设您已经使用 --ingress internal 选项部署了云运行服务,因此没有反向代理就没有人可以访问 Cloud Run。

Follow the below steps to create a reverse proxy in front of the cloud run service.按照以下步骤在云运行服务前创建反向代理。 You can skip Steps 2 - 5 if you are not installing self-signed SSL and you have your own SSL configured如果您没有安装自签名 SSL 并且您配置了自己的 SSL,则可以跳过步骤 2 - 5

  1. Launch a Ubuntu Compute Engine and install apache2启动 Ubuntu 计算引擎并安装 apache2
  2. Install self-signed SSL using安装自签名 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. Update the SSL conf by replacing the below two lines in /etc/apache2/sites-available/default-ssl.conf通过替换 /etc/apache2/sites-available/default-ssl.conf 中的以下两行来更新 SSL conf
SSLCertificateFile      /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
  1. Enable necessary modules启用必要的模块
sudo a2enmod ssl
sudo a2enmod headers
sudo a2ensite default-ssl
  1. Check the config and restart the server, then check whether you can load the https URL https://your-server-public-ip检查配置并重新启动服务器,然后检查是否可以加载 https URL https://your-server-public-ip
sudo apache2ctl configtest
sudo systemctl restart apache2

  1. Update /etc/apache2/sites-available/000-default.conf with this content, Don't forget to replace the CLOUD-RUN-URL使用此内容更新 /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. Update /etc/apache2/sites-available/default-ssl.conf with this content, Don't forget to replace the CLOUD-RUN-URL使用此内容更新 /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. Enable few more modules启用更多模块
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod ssl
  1. Check the config and restart the server检查配置并重新启动服务器
sudo apache2ctl configtest
sudo systemctl restart apache2
  1. Now load the https URL https://your-server-public-ip again, this time you will get the response from cloud run现在再次加载 https URL https://your-server-public-ip,这次你会得到云运行的响应

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

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