在react / django / nginx项目中对“静态”文件夹进行协调

发布于 2025-02-12 03:41:06 字数 751 浏览 2 评论 0原文

我正在与一些使Django一半的同事一起从事一个相当大的项目。我正在研究React Half(使用创建> Create-react-app,然后弹出)。这两个项目都生活在同一存储库中,并与Nginx进行了协调,看起来像是最终用户POV的一个项目。我遇到的问题是,这两个项目似乎都坚持其静态文件夹被命名为static,这在NGINX配置中创建了路由问题。目前,React URL已被路由到一个代理 port 端口和django urls到另一个端口,否则该端口正常工作。

我想做的是:

  1. 重命名public_app react一半中的var,因此所有React静态文件都可以通过/react-project/react-project/静态。到目前为止

  2. 设置nginx以将所有/static/路由Django代理路由,如果找不到的话,请使用React Proxy。我几乎没有nginx的经验,似乎无法使语法工作。我想要这样的东西:

  try_files $uri @django-project
  proxy_pass http://react-project:0000
}

@django-project {
  proxy_pass http://django-project:0000
}

如果我没有提供足够的信息 /代码,请告诉我!

I'm working on a rather large project with some colleagues who are making the Django half. I'm working on the React half (created with create-react-app, then ejected). Both projects live in the same repo, and are coordinated with nginx to look like one single project from the end user pov. The issue I'm running into is that both projects seem to insist on their static folders being named static, which is creating routing issues in the nginx config. Right now the React urls are being routed to one proxy-pass port and Django urls to a different port, which is otherwise working fine.

What I'd like to do is either:

  1. rename the PUBLIC_APP var in the React half so all the React static files can be differentiated by urls of say, /react-project/static. So far I've been unable to find a way to do this that consistently renames all static files and it seems to be an outstanding React bug.

  2. set up nginx to route all /static/ routes to the Django proxy, and if not found there, then to the React proxy. I have very little nginx experience and can't seem to get the syntax to work. I want something like this:

  try_files $uri @django-project
  proxy_pass http://react-project:0000
}

@django-project {
  proxy_pass http://django-project:0000
}

Let me know if I didn't give enough info / code!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十二 2025-02-19 03:41:06

好的,我想我找到了解决方案。通过在React项目的根部创建.env文本文件并在此添加public_url,然后还添加与basename 在React路由器和HomePage in package.json中,我似乎已将所有静态文件强加于react-projoction/static文件夹。有时我有时会获得重复React-Project/React-Project url,但我很疯狂地发动,我不确定我是怎么做到的。

希望这对别人有帮助!

Ok I think I found a solution to this. By creating a .env text file at the root of the React project and adding a PUBLIC_URL there, then ALSO adding that same string as both the basename in React router and the homepage in package.json I seem to have forced all static files into a react-project/static folder. Before I was sometimes getting a duplicate react-project/react-project url but I was kind of flailing madly and I'm not sure how I did that.

Hopefully this is of help to someone else!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文