简体   繁体   English

如何从网络浏览器调用Flask应用程序?

[英]How to call a flask application from a web browser?

I have a flask application that works on my PC. 我有一个可在PC上运行的flask应用程序。 When I deployed it on the virtual server (CentOs 6.5) I used nginx according to the article: https://www.digitalocean.com/community/tutorials/how-to-deploy-flask-web-applications-using-uwsgi-behind-nginx-on-centos-6-4 当我在虚拟服务器(CentOs 6.5)上部署它时,我根据以下文章使用了nginx: https ://www.digitalocean.com/community/tutorials/how-to-deploy-flask-web-applications-using-uwsgi- centos-6-4后面的nginx

I had to change the port in /etc/nginx/nginx.conf because it created conflict with apache port (caused nginx to fail to start because port is already in use). 我必须更改/etc/nginx/nginx.conf中的端口,因为它与apache端口产生了冲突(由于端口已被使用,导致nginx无法启动)。 So my nginx.conf is: 所以我的nginx.conf是:

worker_processes 1;

events {

    worker_connections 1024;

}

http {

    sendfile on;

    gzip              on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                      text/comma-separated-values
                      text/javascript
                      application/x-javascript
                      application/atom+xml;

    # Configuration containing list of application servers
    upstream uwsgicluster {

        server 127.0.0.1:8081;
        # server 127.0.0.1:8081;
        # ..
        # .

    }

    # Configuration for Nginx
    server {

        # Running port
        listen 81;

        # Settings to by-pass for static files 
        location ^~ /static/  {

            # Example:
            # root /full/path/to/application/static/file/dir;
            root /app/static/;

        }

        # Serve a static file (ex. favico) outside static dir.
        location = /favico.ico  {

            root /app/favico.ico;

        }

        # Proxying connections to application servers
        location / {

            include            uwsgi_params;
            uwsgi_pass         uwsgicluster;

            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;
            proxy_set_header   X-Forwarded-Host $server_name;

        }
    }
}

My application is located under 12.12.15.16/cgi-bin/My_app (the ip address here is dummy). 我的应用程序位于12.12.15.16/cgi-bin/My_app下(此处的IP地址为虚拟)。

I used the following command line to start the app: 我使用以下命令行启动应用程序:

env/bin/uwsgi --socket 127.0.0.1:8081 --protocol=http --wsgi-file main.py --callable app

My question is: How can I now call my app from a web browser? 我的问题是:现在如何从网络浏览器调用我的应用程序?

Thank you for your help! 谢谢您的帮助!

Your Flask app is listening on port 8081 but only for localhost connections (127.0.0.1). 您的Flask应用正在侦听端口8081,但仅侦听localhost连接(127.0.0.1)。 Nginx is listening on port 81 for connections and piping them to Flask on 8081. So from your own browser you want to access http://your-digital-ocean-ip:81/ Nginx正在侦听端口81上的连接并将其通过管道输送到8081上的Flask。因此,您想从自己的浏览器访问http://your-digital-ocean-ip:81/

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

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