使用NGINX服务多个静态网站时出现404错误

时间:2018-08-15 22:48:03

标签: javascript html nginx

我有一个具有以下文件夹结构的项目

  • 公开

    • js
    • 资产
    • index.html
  • 事件公开

    • js
    • 资产
    • index.html
  • admin-public

    • 资产
    • index.html

每个文件夹都有自己的唯一资产集(css,图像等)和自己的主页。我已经设置了NGINX服务器,以从同一地址为所有服务器提供服务(并代理到节点服务器,效果很好)。

 server {    
    location /api/ {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_http_version 1.1;
        proxy_pass http://127.0.0.1:8000/;
    }
    location / {
        root /home/ubuntu/project/public;
        autoindex on;
        try_files $uri $uri/ =404;

        # any additional configuration for non-static content
    }
    location /event {
        alias /home/ubuntu/project/event-public;
        autoindex on;
        try_files $uri $uri/ =404;

        # any additional configuration for non-static content
    }    
    location /admin {
        alias /home/ubuntu/project/admin-public;
        try_files $uri $uri/ =404;

        # any additional configuration for non-static content
    }
}

带有location /的公共(主页)服务很好,并且管理站点也可以工作,因为大多数JS与索引文件夹位于同一目录中。但是,当我尝试转到事件页面时,index.html能够很好地加载,但是资产中包含的所有内容均返回404。在控制台上,我看到index.html中嵌入的脚本正在尝试请求服务器根目录/其他(例如https://localhost/js/angular.min.js)。我在事件index.html中的脚本标签如下:

<script src="js/angular.min.js"></script>

我知道我可以将其更改为event / js / angular.min.js,但是有没有一种方法可以配置NGINX来识别这些相对于它们在文件夹中位置的路径?我在本地主机上运行了它,并且工作正常,但是对于在服务器上进行部署,我宁愿不必每次都更改这些脚本标签。

0 个答案:

没有答案
相关问题