【问题标题】:Deploying Flask application with Apache2 Proxy Server使用 Apache2 代理服务器部署 Flask 应用程序
【发布时间】:2022-11-12 09:08:50
【问题描述】:

我正在尝试在 Apache2 中使用带有代理服务器的 Gunicorn 部署 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]

我该如何解决这个问题?

【问题讨论】:

    标签: python flask proxy apache2 gunicorn


    【解决方案1】:

    如果您的目标是使用 https 建立一个简单的网站,我可以提供我已采取的步骤。

    很抱歉看到这篇文章的人,它很长。

    1. 从头开始设置一个新服务器(即,新鲜的Ubuntu Raspberry Pi 上的图像)
      # Tell Universal Firewall (ufw) to Allow SSH connections (on the server)
      sudo ufw allow 22
      # Connect to the server using SSH
      ssh ubuntu@servers.public.ip.addr
      # Create some directories
      mkdir repos
      mkdir downloads
      # Install Python3
      sudo apt-get install python3-pip
      # Install Virtualenv to avoid overlapping dependencies between different python scripts
      sudo apt-get install python3-virtualenv
      # Create a virtual environment
      virtualenv .venv/
      # Enter the virtual environment
      . .venv/bin/activate
      # type 'deactivate' to exit the virtual environment
      ####################################################
      #####   TESTING FIREWALL/CONFIGURING PORTS   #######
      ####################################################
      cd repos
      # Run any app to debug your firewall's effect on Ports & Public IP Address
      git clone https://github.com/org-not-included/simple_flask_app
      cd simple_flask_app
      # Install requirements
      pip3 install -r requirements.txt
      # Run script in background, and write terminal output to logs
      python3 main.py &> server_run_details.log & disown
      # type 'cat server_run_details.log' to see the logs
      # Tell ufw to Allow connections on port 4020 
      sudo ufw allow 4020
      # Test the port connection locally
      curl -X GET http://127.0.0.1:4020
      # kill the Flask app
      sudo fuser -k 4020/tcp
      # Testing Port over Public IP:
      # - skip to bottom of file (shift+g)
      # - update '127.0.0.1' -> '0.0.0.0' (i)
      # - save and quit (esc :wq! enter)
      sudo vi main.py
      sudo fuser -k 4020/tcp
      python3 main.py &> server_run_details.log & disown
      # In another browser try to hit the public IP in a terminal
      curl -X GET http://servers.public.ip.addr:4020
      # Or visit the website in a browser
      http://servers.public.ip.addr:4020
      # Congrats your Firewall works
      


      恭喜,服务器接受 http 流量(通过http://[Public IP]:[Port]



      1. 接下来,我们可以设置一个域名,这样用户就可以访问(http://)www.example.com而不是http://servers.public.ip.addr:4020

      步骤 2A:

      • 告诉 DNS 提供商将流量重定向到您的 IP 地址
      ####################################################
      ####  CONFIGURING A DOMAIN NAME  (example.com)  ####
      ####################################################
      # Tell your Domain Management Provider (porkbun.com, godaddy.com, etc) to point your domain name at your Public IP:
      # - visit your Domain Management Provider
      # - update the A Record's ANSWER to your Public IP
      # - delete the CNAME Record
      ####################################################
      

      步骤 2B:

      • 设置 apache2 以重定向 http 流量
      # Tell ufw to Allow connections over traffic over http (ie. port 80)
      sudo ufw allow http
      sudo apt update
      # Install apache2 to redirect traffic from your domain name to Simple Flask App's Public IP + Port
      sudo apt install apache2
      # Remove everything from the default configuration and add your mapping for http traffic
      ### <VirtualHost *:80>
      ###         ServerName example.com
      ###         ServerAlias www.example.com
      ###         ProxyRequests Off
      ###         ProxyPreserveHost On
      ###         ProxyPass / http://localhost:4020/
      ###         ProxyPassReverse / http://localhost:4020/
      ### </VirtualHost>
      sudo vi /etc/apache2/sites-available/000-default.conf 
      # Install apache2 dependencies 
      sudo a2enmod proxy
      sudo a2enmod proxy_http
      sudo a2enmod proxy_ajp
      sudo a2enmod proxy_balancer
      sudo a2enmod proxy_connect
      sudo a2enmod proxy_html
      sudo a2enmod ssl
      # Tell apache2 to stop running 
      sudo a2dissite 000-default.conf
      # Tell apache2 to start running 
      sudo a2ensite 000-default.conf
      # Have the system manager restart apache2
      sudo systemctl reload apache2
      # Check the server logs
      sudo systemctl status apache2.service
      

      步骤 2C:

      • 对域上的 http 流量进行故障排除
      Visit your domain in web browser:
      - example.com 
      - www.example.com
      - http://www.example.com
      
      # Is Apache2 is running okay?
      sudo systemctl status apache2.service
      # If not
      ## Edit Apache2 Config
      sudo vi /etc/apache2/sites-available/000-default.conf 
      ## Restart apache2
      sudo a2dissite 000-default.conf
      sudo a2ensite 000-default.conf
      sudo systemctl reload apache2
      
      # Is Apache2 is running okay?
      sudo systemctl status apache2.service
      # Restart Step 2C (insert recursion joke here)
      
      # Is your app running okay?
      curl -X GET http://my.public.ip.address:4020/
      

      恭喜,您已确认您的服务器能够提供 http 流量!


      1. 下载一个SSH 捆绑包从你的域管理提供商.

      2. 将这些文件复制到您的服务器:

      # Create a folder on server
      ssh ubuntu@my.public.ip.address
      mkdir -p /home/ubuntu/secrets/example/
      exit
      # Copy from local downloads to server
      scp -r /local/directory/ ubuntu@my.public.ip.address:/home/ubuntu/secrets/example/
      
      1. 更新您的 apache2 配置以使用 https 流量而不是 http:
      <VirtualHost *:443>
              ServerName example.com
              ServerAlias www.example.com
              SSLEngine on
              SSLCertificateFile /home/ubuntu/secrets/example/domain.cert.pem
              SSLCertificateKeyFile /home/ubuntu/secrets/example/private.key.pem
              SSLCertificateChainFile /home/ubuntu/secrets/example/intermediate.cert.pem
              ProxyRequests Off
              ProxyPreserveHost On
              ProxyPass / http://localhost: 4020
              ProxyPassReverse / http://localhost: 4020/
      </VirtualHost>
      
      sudo vi /etc/apache2/sites-available/000-default.conf
      # Restart Apache2
      sudo a2dissite 000-default.conf
      sudo a2ensite 000-default.conf
      sudo systemctl reload apache2
      # Is Apache2 is running okay?
      sudo systemctl status apache2.service
      

      恭喜,您的网站现在应该提供 https 流量。



      清理


      杀死简单的烧瓶应用程序:

      sudo fuser -k 4020/tcp
      

      删除示例回购:

      cd ..
      rm -rf simple_flask_app
      

      从 apache2 配置中删除示例

      sudo vi /etc/apache2/sites-available/000-default.conf
      

      否决我的帖子

      ** Good Luck Scrolling that far **
      

    【讨论】:

      猜你喜欢
      • 2021-10-14
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 2014-06-26
      • 2015-11-21
      相关资源
      最近更新 更多