【问题标题】:Serving react frontend and flask backend simultaneously (nginx)同时提供反应前端和烧瓶后端(nginx)
【发布时间】:2020-01-13 02:21:42
【问题描述】:

我有一个反应前端和一个烧瓶后端。

目前我通过以下方式服务后端

server {
    location / {
        try_files $uri @yourapplication;
    }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }
}

我想以一种允许我从 / 为我的 react 应用程序提供服务并从所有其他路由访问 API 的方式配置 nginx(即 /users 是一个 api 端点)。

这是一个“明智的”设置吗? 我的配置文件应该是什么样的?

【问题讨论】:

    标签: nginx nginx-location nginx-config


    【解决方案1】:

    最终使用以下设置

    server {
    
        root /var/www;
        index index.html index.htm;
    
        location =/ {
            try_files $uri $uri/ /index.html;
        }
    
        # Media: images, icons, video, audio, HTC
        location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
            expires 1M;
            access_log off;
            add_header Cache-Control "public";
        }
    
        # Javascript and CSS files
        location ~* \.(?:css|js)$ {
            try_files $uri =404;
            expires 1y;
            access_log off;
            add_header Cache-Control "public";
        }
    
        # Any route containing a file extension (e.g. /devicesfile.js)
        location ~ ^.+\..+$ {
            try_files $uri =404;
        }
    
        location /user {
            try_files $uri @yourapplication;
        }
    
        location /register {
            try_files $uri @yourapplication;
        }
    
        location /login {
            try_files $uri @yourapplication;
        }
    
        location @yourapplication {
            include uwsgi_params;
            uwsgi_pass unix:///tmp/uwsgi.sock;
        }
    }
    

    换句话说,对于生产,它在 =/ 上提供静态文件,并且对于每个其他端点,它都将其传递给烧瓶。

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 2021-07-11
      • 2021-03-02
      • 2017-12-20
      • 1970-01-01
      相关资源
      最近更新 更多