无法访问 Google Cloud 上 Apache HTTPS 代理后面的 FastAPI 应用程序

问题描述 投票:0回答:1

我正在努力通过 HTTPS 在 forge-code.com 上安全地提供我的 FastAPI 应用程序。

这是我到目前为止所做的:

在 Google Cloud 实例上部署我的 FastAPI 应用程序。 已成功通过实例的 IP 地址直接使用 HTTPS 访问应用程序。 在 Hostinger 上购买了一个域名 forge-code.com 并将其配置为将请求转发到我的实例。 从 Certbot 安装了 SSL 证书,并通过 https://forge-code.com 访问了我的实例上的 Apache2 index.html 文件。 我的问题:当我访问 https://forge-code.com 时,我看到的是 Apache2 index.html 而不是我的 FastAPI 应用程序。 我的 000-default.conf 文件如下所示:

  GNU nano 7.2                                                                                                     000-default.conf                                                                                                              
<VirtualHost *:80>
    ServerName forge-code.com

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =forge-code.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>


00-default-le-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        ProxyPass / http://0.0.0.0:8000/
        ProxyPassReverse / http://0.0.0.0:8000/
        SSLEngine on
        ServerName forge-code.com
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/forge-code.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/forge-code.com/privkey.pem
</VirtualHost>

</IfModule>

这里有更多信息 这些是

的输出

systemctl status apache2

shivajay295@instance-20240221-193932:/etc/apache2/sites-available$ systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; preset: enabled)
     Active: active (running) since Fri 2024-02-23 18:37:43 UTC; 2h 30min ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 46449 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
    Process: 47594 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)
   Main PID: 46455 (apache2)
      Tasks: 55 (limit: 1141)
     Memory: 12.5M
        CPU: 1.008s
     CGroup: /system.slice/apache2.service
             ├─46455 /usr/sbin/apache2 -k start
             ├─47598 /usr/sbin/apache2 -k start
             └─47599 /usr/sbin/apache2 -k start

Feb 23 18:37:43 instance-20240221-193932 systemd[1]: Starting apache2.service - The Apache HTTP Server...
Feb 23 18:37:43 instance-20240221-193932 systemd[1]: Started apache2.service - The Apache HTTP Server.
Feb 23 20:56:01 instance-20240221-193932 systemd[1]: Reloading apache2.service - The Apache HTTP Server...
Feb 23 20:56:01 instance-20240221-193932 systemd[1]: Reloaded apache2.service - The Apache HTTP Server.
Feb 23 21:02:15 instance-20240221-193932 systemd[1]: Reloading apache2.service - The Apache HTTP Server...
Feb 23 21:02:15 instance-20240221-193932 systemd[1]: Reloaded apache2.service - The Apache HTTP Server.
shivajay295@instance-20240221-193932:/etc/apache2/sites-available$ 

sudo /usr/sbin/apache2ctl -S

shivajay295@instance-20240221-193932:/etc/apache2/sites-available$ sudo /usr/sbin/apache2ctl -S
VirtualHost configuration:
*:443                  forge-code.com (/etc/apache2/sites-enabled/000-default-le-ssl.conf:2)
*:80                   forge-code.com (/etc/apache2/sites-enabled/000-default.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default 
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
shivajay295@instance-20240221-193932:/etc/apache2/sites-available$ 

附加信息:

我在我的 Google Cloud 实例上使用 Ubuntu。 我已验证我具有 SSL 证书和密钥文件的读取权限。 我不确定为什么我的 FastAPI 应用程序没有通过 forge-code.com 提供服务。 我需要做什么才能在 forge-code.com 上成功为我的 FastAPI 应用程序提供 HTTPS 服务?

google-cloud-platform proxy apache2 fastapi reverse-proxy
1个回答
0
投票

使用 Apache 和 SSL 在域上部署 FastAPI 应用程序:

1。域名购买和DNS配置:

获取域名:从信誉良好的域名注册商处购买域名(例如 forge-code.com)。 点 DNS 记录:在您域的 DNS 设置中,创建一条 A 记录,将您的域映射到 GCP 实例的 IP 地址:

Type: A

Name: @ (or leave blank)

Value: 34.131.190.233 (replace with your instance's IP)

TTL: 14400 (or a suitable value)

2。 SSL证书安装:

获取证书:使用 Certbot 从 Let's Encrypt 获取免费的 SSL 证书:

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d forge-code.com

3.阿帕奇配置:

编辑虚拟主机:修改

/etc/apache2/sites-available
中以下文件:

000-default-le-ssl.conf(适用于 HTTPS): 确保 SSLEngine 已启用。

将 ServerName 设置为您的域。

包括 SSL 证书和密钥文件。

ProxyPass 和 ProxyPassReverse 到 FastAPI 应用程序的端口 (8000)。

这是我的conf文件

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        ProxyPass / http://0.0.0.0:8000/
        ProxyPassReverse / http://0.0.0.0:8000/
        SSLEngine on
        ServerName forge-code.com
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/forge-code.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/forge-code.com/privkey.pem
</VirtualHost>

</IfModule>

000-default.conf(用于 HTTP): 使用 RewriteRule 将所有 HTTP 请求重定向到 HTTPS。

这是我的conf文件

  GNU nano 7.2                                                                                                000-default.conf                                                                                                          
<VirtualHost *:80>
    ServerName forge-code.com

    # Enable proxy modules
    ProxyRequests Off
    ProxyPreserveHost On
    ProxyPass / http://0.0.0.0:8000/
    ProxyPassReverse / http://0.0.0.0:8000/

    # Rewrite rule to redirect HTTP to HTTPS
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =forge-code.com
    SetEnv proxy-initial-not-pooled 1
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

4。阿帕奇重启:

重新加载配置:

sudo systemctl restart apache2

5。 FastAPI 应用程序启动:

运行 FastAPI 应用程序:

venv/bin/uvicorn app:app --host 0.0.0.0 --port 8000

其他注意事项:

防火墙规则:确保您的防火墙允许端口 80 和 443 上的流量。

FastAPI 应用程序配置: 验证您的 FastAPI 应用程序是否配置为侦听端口 8000。

故障排除: 使用 Apache 日志 (/var/log/apache2/error.log) 对任何问题进行故障排除。

进程管理:考虑使用像systemd或supervisord这样的进程管理器来保持FastAPI应用程序在后台运行。 替代代理:如果 Apache 遇到挑战,请探索 Nginx 或 HAProxy 作为替代代理解决方案。

要点:

域配置: 准确的 DNS 设置对于将流量引导至您的服务器至关重要。

SSL 证书: 与 HTTPS 的安全通信需要有效的 SSL 证书。

Apache 配置: 正确的配置使 Apache 能够代理对 FastAPI 应用程序的请求并处理 SSL。

FastAPI 应用程序启动: 确保您的应用程序正在正确的端口上运行并侦听。

故障排除:日志文件和流程管理对于识别和解决问题至关重要。

宾果,您的 fastapi 应用程序正在您的域上运行!!!

© www.soinside.com 2019 - 2024. All rights reserved.