【问题标题】:Nginx load static files for multiple locationsNginx 为多个位置加载静态文件
【发布时间】:2015-05-22 20:56:20
【问题描述】:

我在一个 nginx .conf 文件中有多个位置,就像这样。

server {
    ...

    location /api/ {
            client_max_body_size 0;
            proxy_pass http://127.0.0.1:8000/api/;
            proxy_redirect off;
            proxy_read_timeout 5m;
            proxy_set_header Host $host;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X­Forwarded­For $proxy_add_x_forwarded_for;
    }
    ...

    location /cliente/ {
            proxy_pass http://127.0.0.1:4000/;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header HOST $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_redirect off;
    }

   location / {
            proxy_pass http://127.0.0.1:8000/;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header HOST $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_redirect off;
    }

在大多数情况下,位置是节点服务器,为他自己的静态文件和 api rest 提供服务。

每个节点应用程序中的静态文件正在加载使用 html 文件并调用如下

/my-static-path-1/
/my-static-path-2/

问题:

当服务器尝试加载一个直接从/ 加载的路径时,不使用自己的子路径,导致 404 错误。

示例:

1) url http:/myip_sample.com/cliente 调用 nodejs 应用程序来提供一个 html 文件,该文件需要加载一个 css 脚本,如 /my-static-path/js/sample.js

2) 此文件调用 http:/myip_sample.com/my-static-path/js/sample.js 而不是 http://myip_sample.com/cliente/my-static-path/js/sample.js

我想在不改变任何东西的情况下这样做,只使用 nginx conf 文件。

提前致谢!

【问题讨论】:

标签: javascript node.js nginx location static-files


【解决方案1】:

将其用于您拥有的每个资产文件夹位置

location /my-static-path-1 {
        alias /location/to/my-static-path-1;
    }

location /my-static-path-2 {
        alias /location/to/my-static-path-2;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 2021-08-08
    相关资源
    最近更新 更多