从子目录提供文件和资产

问题描述 投票:1回答:1

我正在尝试从站点的子目录中服务器文件和资产,如下所示:

网站:/ go / tools /(index.html是根文件),其资产链接如下:/go/tools/assets/js/main.js

使用nginx,我的配置如下所示:

server {
    listen 80;
    listen 443 ssl http2;
    server_name local.tools;

    index index.html;

    location /go/tools {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            root /code/testing/build;
            try_files $uri /$uri /index.html =404;
    }

当我使用URL local.tools/go/tools加载网站时,将加载index.html页面,并且html如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="icon" href="assets/img/favicon.ico">
    <link href="assets/css/vendor/bootstrap.css" rel="stylesheet" type="text/css" />
    <link href="assets/css/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>...

所以那部分很好。 问题在于样式和javascript不存在。 当查看“网络”标签时,我看到每个资产都在加载index.html内容而不是其自身的内容。

我的配置中缺少什么,所以如果我去:/go/tools/assets/css/styles.css,我看到了实际的样式表?

nginx nginx-location
1个回答
2
投票

您需要使用以下配置

location /go/tools {
        location /go/tools/assets/ {
            alias /code/testing/build/assets/;
        }
        alias /code/testing/build;
        try_files $uri $uri/ /index.html =404;
}

基本上,当url是/go/tools/assets您需要从build目录中的/assets进行搜索。 这就是为什么我们在嵌套位置需要别名

© www.soinside.com 2019 - 2024. All rights reserved.