› NGINX
› NGINX Trac
› 3rd Party Modules
› Security Advisories
› CHANGES
› OpenResty
› ngx_lua
› Tengine
在线学习资源
› NGINX 开发从入门到精通
NGINX Modules
› ngx_echo
helllkz
V2EX  ›  NGINX

nginx 静态文件转发

  •  
  •   helllkz · Jul 30, 2018 · 7340 views
    This topic created in 3025 days ago, the information mentioned may be changed or developed.

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

    11 replies  •  2018-08-01 08:14:17 +08:00
    ming2050
    ming2050
    Jul 30, 2018
    1
    支持的 再好好检查下路径对不对吧
    Trim21
    Trim21
    Jul 30, 2018 via Android
    2
    把 nexus 通过 nginx 代理, 为什么要把静态文件通过 nginx 转发给 nexus ?看你的描述应该是把静态文件通过 Nexus 转发给 Nginx 吧…
    helllkz
    helllkz
    OP
    Jul 30, 2018
    3
    @Trim21
    因为 nexus 自带了一个 netty,nginx 在 A 服务器上,nexus 在 B 服务器上,如果不把 css js png 等静态文件的请求发到 B 上面去的话,那会在 A 服务器的 root 下面找,就 404 了

    目前临时的做法是没在 http 里面做,直接写了个 upstream,纯粹当个端口转发用了
    anthoy
    anthoy
    Jul 30, 2018
    4
    以我菜菜的水平来说应该可以的,不过不是应该把静态文件用 nginx 管理吗,怎么要 nginx->nexus->静态的?
    jswh
    jswh
    Jul 30, 2018
    5
    贴个配置看看
    LoeNet
    LoeNet
    Jul 30, 2018
    6
    你这个需要配置 Location 转发到后端的 upstream
    helllkz
    helllkz
    OP
    Jul 30, 2018
    7
    如果 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
    Judoon
    Jul 31, 2018 via Android
    8
    你把 proxy_pass 那行,最后的 /去掉试试
    helllkz
    helllkz
    OP
    Jul 31, 2018
    9
    @Judoon
    没用,我试过的,无论如何都在本地查找文件,不会把静态文件转发
    Judoon
    Judoon
    Jul 31, 2018 via Android
    10
    @helllkz 你贴的配置里没有指定本地目录,要么是有其他优先级高的配置?你可以发起一条正常请求,并把 log 贴上来看看,要把 upstream_addr 打出来
    helllkz
    helllkz
    OP
    Aug 1, 2018
    11
    @Judoon
    突然又可以了,也许是某条记录冲突了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   950 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 20:14 · PVG 04:14 · LAX 12:14 · JFK 15:14
    ♥ Do have faith in what you're doing.