Django + react : How to connect them for deployment?

标签 django reactjs production

我正在使用 Django+DRFCELERY+REDISReactJs+REDUXJWT 运行应用,我很难将 backendfrontend 连接起来进行部署。

我已经使用 create-react-app 生成 React 代码;和 npm run build 生成生产版本。

我一直在四处寻找,但找不到有关如何将它们链接在一起的教程。

如果你们有任何我可以关注的链接,我将不胜感激。

最佳答案

您不需要将 React 文件“移入”Django。只需分别为他们服务。

这就是我为我的项目服务的方式:

enter image description here

我从 /api/ url 提供我的 API,所以这是我的 Nginx 配置的粗略表示:

server {

    # urls that start with /api or /admin
    location ~ ^/(api|admin) {
        # pass them to uwsgi or gunicorn
        ...
    }

    # urls that start with /media
    location /media  {
        # serve from your project's media folder
        ...
    }

    # urls that start with /static/admin 
    location /static/admin {
        # these requests are made to admin's static files
        # serve them from your staticroot folder
        # i.e. where your files are stored after you run `collectstatic`
        ...
    }

    # everything else
    location / {
        # serve from frontend build folder
        root   /path/to/the/build/folder;
        index  index.html;
        # this is important
        # first try to find files matching url, like css, or js, or favicon
        # if nothing found, serve the index.html            
        try_files $uri /index.html; 
    }
}

关于Django + react : How to connect them for deployment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60705957/

相关文章:

python - 如何在 DJANGO 中使用基于类的 View 调用函数并在 ui 上获取结果

javascript - React子组件不获取 Prop 并更新状态

javascript - 调试 react 。确定更改了哪个 Prop

css - typescript 可拖动 slider

javascript - Vue.js 应用程序中生产模式下静态文件的路径错误

django - 在 Django 中使用 Rest api 而不是 Views

python - 如何设置 DJANGO_SETTINGS_MODULE 环境变量?

python - 复杂的 Django 模型查询

sql - 用于将数据库更改从开发数据库同步到生产的工具?

tomcat - 我能知道如何在 centos 服务器上部署我的 spring boot 应用程序吗……提一下这个应用程序在本地主机上运行良好