【问题标题】:Impossible to access Flask app through Vagrant private network and port forwarding (through domain name)无法通过 Vagrant 私网和端口转发(通过域名)访问 Flask 应用
【发布时间】:2020-10-19 01:02:15
【问题描述】:

我刚刚完成了一个 Flask 应用程序,我想通过虚拟机(使用 Vagrant)在我的笔记本电脑(Ubuntu 18.04 下)上托管它。

我的网络配置如下:

network configuration

我定义了一个域名(通过无 IP 网站),我为我的互联网路由器配置了端口转发(从 80 到 8080 以及从 443 到 8443),我添加了动态 DNS 以将我的域名链接到我的互联网路由器公共IP 地址。

host my_domain_name 命令返回了我的互联网路由器公共 IP 地址。

然后我创建了一个 Vagrant 专用网络(IP 地址为 192.168.33.10)和一个 Vagrant 端口转发:

Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/bionic64"
  config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
  config.vm.network "forwarded_port", guest: 443, host: 8443, auto_correct: true
  config.vm.network "private_network", ip: "192.168.33.10"

  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
  end
end

然后我使用 nginx + gunicorn + supervisor 设置服务器(按照本教程说明https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux)。

我的 nginx 配置是:

server {
    # listen on port 80 (http)
    listen 80;
    server_name www.my_domain_name;
    location / {
        # redirect any requests to the same URL but on https
        return 301 https://$host$request_uri;
    }
}
server {
    # listen on port 443 (https)
    listen 443 ssl;
    server_name www.my_domain_name;

    # location of the self-signed SSL certificate
    ssl_certificate /home/vagrant/my_flask_app/certificates/certificate.pem;
    ssl_certificate_key /home/vagrant/my_flask_app/certificates/key.pem;

    # write access and error logs to /var/log
    access_log /var/log/my_flask_app_access.log;
    error_log /var/log/my_flask_app_error.log;

    location / {
        # forward application requests to the gunicorn server
        proxy_pass http://localhost:8000;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /static {
        # handle static files directly, without forwarding to the application
        alias /home/vagrant/my_flask_app/app/static;
        expires 30d;
    }
}

我在 VM 上配置了防火墙,以便打开以下端口:

~$ sudo ufw satus

Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                  
443/tcp                    ALLOW       Anywhere                  
22/tcp (v6)                ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)             
443/tcp (v6)               ALLOW       Anywhere (v6)   

对于主机(我的笔记本电脑):

~$ sudo ufw status

Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                  
443/tcp                    ALLOW       Anywhere                  
8080/tcp                   ALLOW       Anywhere                  
8443/tcp                   ALLOW       Anywhere                  
22/tcp (v6)                ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)             
443/tcp (v6)               ALLOW       Anywhere (v6)             
8080/tcp (v6)              ALLOW       Anywhere (v6)             
8443/tcp (v6)              ALLOW       Anywhere (v6)

当我在浏览器中尝试通过192.168.33.10 访问该网站时,它工作正常。

但是当我尝试使用 http://www.my_domain_name 时,我得到了 Welcome to nginx! 页面。

我真的不明白我做错了什么。

由于这是我第一次与网站托管打交道,我很乐意获得任何帮助 :-)

【问题讨论】:

    标签: nginx flask vagrant ubuntu-18.04 portforwarding


    【解决方案1】:

    我认为您可能误解了专用网络的作用。当您有多个需要相互通信的虚拟机时,它很有用,但在您的情况下不是必需的。从 VM 内监听的任何内容都必须监听 0.0.0.0 而不是 127.0.0.1。好在 nginx 已经做到了前者。

    在笔记本电脑上(运行 Ubuntu 14.04,因为它很旧),我使用

    Vagrant.configure("2") do |config|
        config.vm.box = "ubuntu/bionic64"
        config.vm.network "forwarded_port", guest: 80, host: 8000
    

    ssh 进入,并安装了 nginx。

    我可以从我的笔记本电脑通过http://0.0.0.0:8000 访问 nginx,从我的 Intranet 上的其他地方通过(在我的情况下)http://192.168.1.94:8000。没有必要在 VM 中使用ufw(“forwarded_port”负责处理)。为什么这台笔记本电脑不需要ufw...可能取决于我几年前忘记做的事情。

    在 Intranet 部分正常工作的情况下,将 VM 中的 nginx(以及它背后的代理)暴露给 Internet 是路由器配置的问题。

    【讨论】:

    • 戴夫您好,非常感谢您的帮助!其实问题很简单。我试图用http://www.my_domain_name 访问该网站,但我意识到我的域名不包含www(菜鸟错误...... -_-)!因此,只需使用http://my_domain_name 就可以正常工作。我在查看我的路由器配置时意识到了这一点,所以我认为你解决了我的问题。再次感谢您,我已经使用了一周^^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    相关资源
    最近更新 更多