使用 Apache2 代理服务器部署 Flask 应用程序
我正在尝试使用 Gunicorn 和 Apache2 中的代理服务器部署 Flask 应用程序。 Flask 应用程序在 Docker 容器中运行,但不在 Apache2 服务器中运行。
这是 Apache2 的配置。
<Macro DemoSubdomain $subdomain_name $proxy_pass_proto $proxy_pass_to>
<VirtualHost *:443>
ServerName $subdomain_name.example.com
ProxyPass / $proxy_pass_proto://$proxy_pass_to
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://$proxy_pass_to/$1" [P,L]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /root/example.com-crt.pem
SSLCertificateKeyFile /root/example.com-key.pem
SSLCertificateChainFile /root/example.com-chain.pem
</VirtualHost>
</Macro>
当 Config 中未指定 'SERVER_NAME' 时,Flask 应用程序在 'subdomain_name.example.com' 中运行,但是当调用重定向函数时,它会重定向到 localhost:17000
。
因此,在 Flask 应用程序中,我将 config['SERVER_NAME']
设置为“subdomain_name.example.com”并运行 Gunicorn。但是,我收到以下错误。
/usr/local/lib/python3.10/site-packages/flask/app.py:1777: UserWarning: Current server name 'localhost:17000' doesn't match configured server name 'subdomain_name.example.com'
app_1 | return self.url_map.bind_to_environ(
app_1 | ERROR:some_app.app.app:Exception on / [GET]
我该如何解决这个问题?
I'm trying to deploy a Flask application with Gunicorn with a Proxy Server in Apache2. The Flask application is running in a Docker container but not the Apache2 server.
Here is the configuration for Apache2.
<Macro DemoSubdomain $subdomain_name $proxy_pass_proto $proxy_pass_to>
<VirtualHost *:443>
ServerName $subdomain_name.example.com
ProxyPass / $proxy_pass_proto://$proxy_pass_to
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://$proxy_pass_to/$1" [P,L]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /root/example.com-crt.pem
SSLCertificateKeyFile /root/example.com-key.pem
SSLCertificateChainFile /root/example.com-chain.pem
</VirtualHost>
</Macro>
The Flask application runs in the 'subdomain_name.example.com' when 'SERVER_NAME' is not specified in Config, however when calling redirect function, it redirects to localhost:17000
.
So, in Flask application, I set the config['SERVER_NAME']
to 'subdomain_name.example.com' and run Gunicorn. But, I am getting the following error.
/usr/local/lib/python3.10/site-packages/flask/app.py:1777: UserWarning: Current server name 'localhost:17000' doesn't match configured server name 'subdomain_name.example.com'
app_1 | return self.url_map.bind_to_environ(
app_1 | ERROR:some_app.app.app:Exception on / [GET]
How do I resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的目标是使用 https 设置一个简单的网站,我可以提供我已采取的步骤。
假设:
步骤 0.
步骤 1.
通过本地 IP 测试端口
通过 公共IP:
恭喜,您已确认您的服务器能够提供 http 流量(通过
http://[公共 IP]:[端口]< /code>)
步骤 2.
www.example.com
而不是http://servers.public.ip.addr :4020。
步骤 2A:
步骤 2B:
步骤 2C:
恭喜,您已确认您的服务器能够提供 http 流量(通过
http://example.com
)!步骤 3.
步骤 4.
步骤 5.
恭喜,您已确认您的服务器能够提供 https 流量(通过
https://example.com)。 com
)!步骤 6.
杀死 Simple Flask 应用程序:
删除示例存储库:
从 apache2 配置中删除示例
If your goal is to setup a simple website with https, I can offer the steps I've taken.
Assumptions:
Ubuntu
Image" (ie. on a Raspberry Pi)Step 0.
Step 1.
Testing Port via Local IP
Testing Port via Public IP:
Congrats, you've confirmed your server is able to serve http traffic (via
http://[Public IP]:[Port]
)Step 2.
www.example.com
instead ofhttp://servers.public.ip.addr:4020
.Step 2A:
Step 2B:
Step 2C:
Congrats, you've confirmed your server is able to serve http traffic (via
http://example.com
)!Step 3.
Step 4.
Step 5.
Congrats, you've confirmed your server is able to serve https traffic (via
https://example.com
)!Step 6.
Kill the Simple Flask App:
Remove the example Repo:
Delete example from apache2 config