V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
helllkz
V2EX  ›  NGINX

nginx 静态文件转发

  •  
  •   helllkz · 2018-07-30 14:24:15 +08:00 · 6073 次点击
    这是一个创建于 2068 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    11 条回复    2018-08-01 08:14:17 +08:00
    mringg
        1
    mringg  
       2018-07-30 14:37:27 +08:00
    支持的 再好好检查下路径对不对吧
    Trim21
        2
    Trim21  
       2018-07-30 14:44:11 +08:00 via Android
    把 nexus 通过 nginx 代理, 为什么要把静态文件通过 nginx 转发给 nexus ?看你的描述应该是把静态文件通过 Nexus 转发给 Nginx 吧…
    helllkz
        3
    helllkz  
    OP
       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
        4
    anthoy  
       2018-07-30 16:34:20 +08:00
    以我菜菜的水平来说应该可以的,不过不是应该把静态文件用 nginx 管理吗,怎么要 nginx->nexus->静态的?
    jswh
        5
    jswh  
       2018-07-30 16:37:44 +08:00
    贴个配置看看
    LoeNet
        6
    LoeNet  
       2018-07-30 17:11:21 +08:00
    你这个需要配置 Location 转发到后端的 upstream
    helllkz
        7
    helllkz  
    OP
       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
        8
    Judoon  
       2018-07-31 00:53:05 +08:00 via Android
    你把 proxy_pass 那行,最后的 /去掉试试
    helllkz
        9
    helllkz  
    OP
       2018-07-31 08:30:25 +08:00
    @Judoon
    没用,我试过的,无论如何都在本地查找文件,不会把静态文件转发
    Judoon
        10
    Judoon  
       2018-07-31 09:24:14 +08:00 via Android
    @helllkz 你贴的配置里没有指定本地目录,要么是有其他优先级高的配置?你可以发起一条正常请求,并把 log 贴上来看看,要把 upstream_addr 打出来
    helllkz
        11
    helllkz  
    OP
       2018-08-01 08:14:17 +08:00
    @Judoon
    突然又可以了,也许是某条记录冲突了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4864 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:54 · PVG 17:54 · LAX 02:54 · JFK 05:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.