我正在使用 Django+DRF
、CELERY+REDIS
、ReactJs+REDUX
和 JWT
运行应用,我很难将 backend
与 frontend
连接起来进行部署。
我已经使用 create-react-app
生成 React 代码;和 npm run build
生成生产版本。
我一直在四处寻找,但找不到有关如何将它们链接在一起的教程。
如果你们有任何我可以关注的链接,我将不胜感激。
最佳答案
您不需要将 React 文件“移入”Django。只需分别为他们服务。
这就是我为我的项目服务的方式:
我从 /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/