nginx + node + angular 静态文件问题

33 views
Skip to first unread message

flyer

unread,
Sep 25, 2014, 8:30:02 AM9/25/14
to shlug
目前有个项目,nginx 提供反向代理服务,将来自某些特定前缀的 url 转给 node 的 web 服务,这个 web 服务前端由 angular 来管理。

现在有个问题,静态文件我得放到 nginx 的 document_root 目录下,这样 web 服务才能正常访问这些静态文件。
这样做的话,每次 node 的 web 服务项目更新静态文件时,我都需要将这些静态文件转到 nginx 的 document_root 目录下。

我把所有的静态文件都放在 node 的 web 服务项目的 static/ 目录下,然后在 nginx 配置中添加了:

location /static/ {
    alias /PATH/TO/static/;
    autoindex off;
}

但这样修改后访问这个 web 服务时,仍是找不到这些静态文件。
请问这种情况该如何处理?谢谢。

--
时光飞逝,但时光正好。

Armnotstrong

unread,
Sep 25, 2014, 9:02:51 AM9/25/14
to sh...@googlegroups.com
应该这么写
location /static/ {
    root /PATH/TO/;  # <-- no static here
    autoindex off;
}

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了Google网上论坛中的“Shanghai Linux User Group”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout



--
========================================
best regards & a nice day
Zhao Ximing

Armnotstrong

unread,
Sep 25, 2014, 9:06:52 AM9/25/14
to sh...@googlegroups.com
哦,理解错了,你应该用rewirte实现这个需求。
rewirte ^/static/(.*)$ /PATH/TO/static/$1 break;

在 2014年9月25日 下午8:29,flyer <flye...@gmail.com>写道:

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了Google网上论坛中的“Shanghai Linux User Group”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

flyer

unread,
Sep 26, 2014, 1:18:42 AM9/26/14
to shlug
嗯,谢谢 :D

是目录访问权限出现了问题,现在想了一种解决方法:

在 home 下建立一个 nginx 可以访问的目录,然后提供一个部署脚本进行部署,它会通过 git 拉取代码,然后把静态文件拷到之前设置的目录中。

时光飞逝,但时光正好。

Yuanchong Zhu

unread,
Sep 26, 2014, 2:52:11 AM9/26/14
to sh...@googlegroups.com
为什么你上面那种设置root的方法不行呢?
灿烂星空,你就是我的英雄!

Armnotstrong

unread,
Sep 26, 2014, 6:51:48 AM9/26/14
to sh...@googlegroups.com
因为静态文件是在node的content-root下的某个文件夹中,而不是在操作系统的文件夹中。
Reply all
Reply to author
Forward
0 new messages