ubuntu - 如何与 Flask 后端 api 一起构建 Angular 2 前端应用程序?

标签 ubuntu angular nginx flask gunicorn

我想使用 Nginx 为前端 angular 2 应用程序提供服务。我使用 Gunicorn 作为 Nginx 和 Flask 之间的媒介。我想将此 Web 应用程序部署到生产环境。这样做的正确应用程序结构应该是什么?另外,nginx 服务器 block 配置是什么?我托管在基于 digital ocean Linux 的小滴(Ubuntu 16.04)上。我引用了以下链接,但他们似乎也在使用 Flask 来服务前端。
https://www.digitalocean.com/community/tutorials/how-to-structure-large-flask-applications
https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-16-04

最佳答案

这是一个示例 Nginx 配置文件。我确信这不是最好的方法,但它让我测试了我的应用程序。

    server {
        listen       80;
        server_name  localhost;

        # Your angular app
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
            try_files $uri $uri/ =404;
        }

        # Static files 
        location /static {
            alias /usr/share/nginx/flask/static;
        }

        # Gunicorn 
        location /api {
            proxy_pass http://localhost:4000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }   
    }

注意事项
  • 使用 Angular CLI 构建您的 Angular 应用程序,例如,dist文件夹。让 nginx 指向它。给定为 /usr/share/nginx/html在配置中。这个link帮助我更好地了解 Angular CLI。
  • 使用 gunicorn 为您的 flask 后端服务。说,gunicorn -b 127.0.0.1:4000 app:app .转至 /api .
  • 将 nginx 配置为反向代理到 gunicorn。代码中给出。

  • 希望这至少能让你继续前进。

    关于ubuntu - 如何与 Flask 后端 api 一起构建 Angular 2 前端应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41327350/

    相关文章:

    javascript - 错误错误 : Uncaught (in promise): TypeError: Cannot set property 'devices' of undefined in ionic 4 Angular

    linux - nginx(example.app)中的自定义域无法连接

    git - 用户在 Gitolite 上的所有 repos 上的写入权限

    javascript - 如何设计一个全局类来存储 Angular 中的设置?

    Angular2 路由器 : Cannot find the outlet XOutlet to load YComponent

    ruby - 一个网络服务器上有多个 Ruby 版本?

    perl - 如何在Nginx上使用FastCGI运行Perl脚本?

    select - 使用 select、epoll 或 kqueue 服务大文件

    c# - Mono Socket.Bind 在连接之前绑定(bind)以使用特定接口(interface)

    linux - 如何在/etc/fstab 中指定带有空格的标签/路径?