【问题标题】:Set up Vue app run using Nginx on port 80 from Raspbian along with Flask backend running on port 8080使用 Nginx 在 Raspbian 的 80 端口上运行 Vue 应用程序,并在 8080 端口上运行 Flask 后端
【发布时间】:2019-05-27 12:03:33
【问题描述】:

我有 Nginx 设置基于 Flask 的后端运行在端口 8080 上,配置如下

server {
    listen 8080 default_server;
    listen [::]:8080;

    root /var/www/html;

    server_name _;

    location /static {
        alias /var/www/html/static/;
    }

    location / {
        try_files $uri @wsgi;
    }

    location @wsgi {
        proxy_pass http://unix:/tmp/gunicorn.sock;
        include proxy_params;
    }

    location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off;
        log_not_found off;
        expires max;
    }
}

我还设置了一个使用 gunicorn 运行烧瓶应用程序的 systemd 服务:gunicorn --bind=unix:/tmp/gunicorn.sock --workers=4 start_backend:web_app

现在以上内容适用于端口 8080 上的 Python Flask 后端,我还想在默认端口 80 上添加 Vue 应用程序。

更新

server {
        listen 80 default_server;
        listen [::]:80;
        root /var/www/html/dist;
        server_name _;

        location /static {
        alias  /var/www/html/dist/static/;
        }

        location / {
        root /var/www/html/dist;
        try_files $uri $uri/ /index.html;
        }

        location /api {
                root /var/www/html;
                try_files $uri @wsgi;
        }

        location @wsgi {
            proxy_pass http://unix:/tmp/gunicorn.sock;
            include proxy_params;
        }

【问题讨论】:

    标签: nginx vue.js raspbian


    【解决方案1】:

    听起来您需要添加另一个服务器块来为前端提供服务。

    server {
        listen 80 default_server;
        listen [::]:80;
    
        location / {
             root /path/to/dist;
             try_files $uri $uri/ /index.html;
        }
    }
    

    我的代码基于this tutorial,上面示例中的/path/to/dist 应更改为the dist directory of the Vue.js front-end from your vue app

    如果您阅读过本教程中的设置 Nginx 部分,您会注意到它们正在为 Flask 应用程序提供服务,而 Vue.js 位于同一服务器块中的不同 URL 前:

    server {  
        listen 80;
        server_name 123.45.67.89;
    
        location /api {
            include uwsgi_params;
            uwsgi_pass unix:/home/survey/flask-vuejs-survey/backend/surveyapi.sock;
        }
    
      location / {
        root /home/survey/flask-vuejs-survey/frontend/survey-spa/dist;
        try_files $uri $uri/ /index.html;
      }
    

    如果此应用程序将面向互联网,那么这可能是一种更好的处理方式,因为端口 8080 的传出可能会被您用户的互联网提供商阻止。使用第二种配置,一切都通过端口 80 提供服务。

    您可能需要稍微调整您的 vue.js 应用程序,使其在 /api(或其他地方)查找 API,让 / 可以免费为前端提供服务。

    【讨论】:

    • 当然,如果这不是位于另一个在端口 443 上实现 SSL 的反向代理之后,并且您正在自己配置前置 nginx 服务器,那么您肯定应该使用以Mozilla nginx conf generator 为起点,阅读Security/Server Side TLS 指导。
    • 答案似乎就在我的问题中,我为/static 服务的地方可以是/dist for root
    • 现在 vuejs 应用程序已加载,但 /var/www/html/dist/static 下的 css/js 文件夹不可见。我在/api 上从/var/www/html/ 提供的烧瓶应用程序未在浏览器中加载。我在上面添加了一个更新
    猜你喜欢
    • 1970-01-01
    • 2019-02-16
    • 2015-01-07
    • 2013-01-27
    • 1970-01-01
    • 2018-12-26
    • 2011-06-16
    • 2012-02-12
    • 2019-06-23
    相关资源
    最近更新 更多