nginx 静态文件转发

2018-07-30 14:24:15 +08:00
 helllkz

nginx 支持把静态文件也一起 proxy_pass 了吗? 我在前端服务器配置了个 nginx,另外个服务器跑 nexus,现在想把 nexus 通过 nginx 代理,遇到一个问题,就是默认的 css js 的静态文件,没法通过 nginx 转发给 nexus,直接提示在 /usr/local/nginx/html/static/下找不到文件,我纯粹想把 nginx 当个端口转发使用,连静态文件一起转发了

6092 次点击
所在节点    NGINX
11 条回复
mringg
2018-07-30 14:37:27 +08:00
支持的 再好好检查下路径对不对吧
Trim21
2018-07-30 14:44:11 +08:00
把 nexus 通过 nginx 代理, 为什么要把静态文件通过 nginx 转发给 nexus ?看你的描述应该是把静态文件通过 Nexus 转发给 Nginx 吧…
helllkz
2018-07-30 15:46:59 +08:00
@Trim21
因为 nexus 自带了一个 netty,nginx 在 A 服务器上,nexus 在 B 服务器上,如果不把 css js png 等静态文件的请求发到 B 上面去的话,那会在 A 服务器的 root 下面找,就 404 了

目前临时的做法是没在 http 里面做,直接写了个 upstream,纯粹当个端口转发用了
anthoy
2018-07-30 16:34:20 +08:00
以我菜菜的水平来说应该可以的,不过不是应该把静态文件用 nginx 管理吗,怎么要 nginx->nexus->静态的?
jswh
2018-07-30 16:37:44 +08:00
贴个配置看看
LoeNet
2018-07-30 17:11:21 +08:00
你这个需要配置 Location 转发到后端的 upstream
helllkz
2018-07-30 21:36:52 +08:00
如果 nginx 和 nexus 在一台服务器,这么是没问题的
server {
listen *:80;
server_name www.example.com;

# allow large uploads of files
client_max_body_size 1G;

# optimize downloading files larger than 1G
#proxy_max_temp_file_size 2G;

location / {
proxy_pass http://localhost:8081/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

如果是在 2 台服务器,这样就有问题了,/static 下的文件就会去找 nginx 的 root,不会被转发的
server {
listen *:80;
server_name www.example.com;

# allow large uploads of files
client_max_body_size 1G;

# optimize downloading files larger than 1G
#proxy_max_temp_file_size 2G;

location / {
proxy_pass http://10.0.0.100:8081/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Judoon
2018-07-31 00:53:05 +08:00
你把 proxy_pass 那行,最后的 /去掉试试
helllkz
2018-07-31 08:30:25 +08:00
@Judoon
没用,我试过的,无论如何都在本地查找文件,不会把静态文件转发
Judoon
2018-07-31 09:24:14 +08:00
@helllkz 你贴的配置里没有指定本地目录,要么是有其他优先级高的配置?你可以发起一条正常请求,并把 log 贴上来看看,要把 upstream_addr 打出来
helllkz
2018-08-01 08:14:17 +08:00
@Judoon
突然又可以了,也许是某条记录冲突了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/475340

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX