在react / django / nginx项目中对“静态”文件夹进行协调
我正在与一些使Django一半的同事一起从事一个相当大的项目。我正在研究React Half(使用创建> Create-react-app
,然后弹出)。这两个项目都生活在同一存储库中,并与Nginx进行了协调,看起来像是最终用户POV的一个项目。我遇到的问题是,这两个项目似乎都坚持其静态文件夹被命名为static
,这在NGINX配置中创建了路由问题。目前,React URL已被路由到一个代理
port 端口和django urls到另一个端口,否则该端口正常工作。
我想做的是:
重命名
public_app
react一半中的var,因此所有React静态文件都可以通过/react-project/react-project/静态
。到目前为止设置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:
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.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我想我找到了解决方案。通过在React项目的根部创建
.env
文本文件并在此添加public_url
,然后还添加与basename 在React路由器和
HomePage
inpackage.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 aPUBLIC_URL
there, then ALSO adding that same string as both thebasename
in React router and thehomepage
inpackage.json
I seem to have forced all static files into areact-project/static
folder. Before I was sometimes getting a duplicatereact-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!