当前位置: 首页 > 知识库问答 >
问题:

Nginx正在提供默认内容而不是我的内容

松越
2023-03-14

我有一个nginx版本: nginx/1.10.3(Ubuntu)运行在Ubuntu 16.04.2 LTS上。

我使用nginx来提供静态文件,即webpack生成的包,但这是不相关的。

我想要实现的是:

关于示例。com我想提供/home/bundles/main/index。html。我能做到。

项目上。实例com/project_1我想为/home/bundles/project_1/index提供服务。html

项目上。实例com/project_2我想提供/home/bundles/project_2/index。html

最后两个,我做不到。当我进入项目时。实例com/project_1项目。实例com/project_2我收到了默认的nginx页面。

为了使事情更加混乱/etc/nginx/site-启用/def被完全注释掉。

此外,如果在projects.example.com位置块中,我将project_1替换为/,我将获得该特定项目的服务,但是我将无法为另一个服务。

下面,我将向您展示我的nginx配置

server {
    listen 80;
    server_name example.com;

    location / {
        root /home/bundles/main;
        try_files $uri /index.html;
    }

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name example.com;

    location / {
        root /home/bundles/main;
        try_files $uri /index.html;
    }

    ssl_certificate ...
    ssl_certificate_key ...
}

server {
    listen 80;
    server_name projects.example.com;

    location /project_1 {
        root /home/bundles/project_1;
        try_files $uri /index.html;
    }

    location /project_2 {
        root /home/bundles/project_2;
        try_files $uri /index.html;
    }

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name projects.example.com;

    location /project_1 {
        root /home/bundles/project_1;
        try_files $uri /index.html;
    }

    location /project_2 {
        root /home/bundles/project_2;
        try_files $uri /index.html;
    }

    ssl_certificate ...
    ssl_certificate_key ...
}

谢谢你的帮助!

编辑

我找到的解决方案是使用alias更改root

server {
    listen 80;
    server_name example.com;

    location / {
        root /home/bundles/main;
        try_files $uri /index.html;
    }

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name example.com;

    location / {
        root /home/bundles/main;
        try_files $uri /index.html;
    }

    ssl_certificate ...
    ssl_certificate_key ...
}

server {
    listen 80;
    server_name projects.example.com;

    location /project_1 {
        alias /home/bundles/project_1;
        index index.html;
    }

    location /project_2 {
        alias /home/bundles/project_2;
        index index.html;
    }

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name projects.example.com;

    location /project_1 {
        alias /home/bundles/project_1;
        index index.html;
    }

    location /project_2 {
        alias /home/bundles/project_2;
        index index.html;
    }

    ssl_certificate ...
    ssl_certificate_key ...
}

解决方案基于这两个答案。第一个答案显示了如何解决问题,第二个答案解释了为什么alias有效,而root无效。

引用@treecoder

在根指令的情况下,完整路径被附加到包括位置部分的根,而在别名指令的情况下,只有路径不包括位置部分的部分被附加到别名。

在我的特殊情况下,这将像这样翻译;

使用root,nginx尝试访问的路径将是/home/bundles/project\u 1/project\u 1

使用别名访问正确的路径,/home/bundles/project_1

回到一个层次,例如,说:

root/home/bundles/也不是真正的选项。这是因为我的项目实际上没有被称为project\u 1project\u 2。实际结构与此更为相似。

/bundles中,我有项目a项目b的目录。我想将project_1路由到project_a目录,并将project_2路由到project_b目录。

这就是为什么我使用别名

我希望这有帮助。

共有2个答案

施兴言
2023-03-14

我也有同样的问题。。。事实证明,对我来说,默认站点“超越”了我想要的辅助站点并提供默认文件。。。它允许IP6请求,而我的新站点没有。

这是我access.log的一个例子:

::1 - - [05/Sep/2020:15:05:16 -0600] "GET / HTTP/1.1" 200 40 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"

注意::1位?这是一个IP6请求,我猜我的浏览器是默认的。因此,我只是通过更改以下内容,确保在我的站点配置文件中为相关站点启用本地IP6请求:

listen 80;
server_name example.local;

致:

listen 80;
listen [::]:80;
server_name example.local;

就这样。

申黎明
2023-03-14

你有:

location /project_1 {
    root /home/bundles/project_1;
    try_files $uri /index.html;
}

因此,仅为以/project\u 1开头的URI定义。对于任何其他URI,将使用默认的root

如果您显示URI/project_1/(尾随/),假设默认的index指令有效,nginx应该返回您的/project_1/index.html内容。

但是,找不到URI/project_1-因此/索引。返回html。URI/索引。html不是以/project\u 1开头,因此使用默认根目录。

如果希望URI/project_1按预期工作,则默认操作转到项目的索引。html文件,更改try\u files指令。

location /project_1 {
    root /home/bundles/project_1;
    try_files $uri $uri/ /project_1/index.html;
}

有关更多信息,请参阅此文档。

由于两个项目共享一个公共根,您可以简化如下:

server {
    listen 80;
    server_name projects.example.com;

    root /home/bundles
    index index.html;

    location /project_1 {
        try_files $uri $uri/ /project_1/index.html;
    }
    location /project_2 {
        try_files $uri $uri/ /project_2/index.html;
    }
    location / {
        deny all;
    }
}

我添加了index指令,以避免依赖默认值(相同),并添加了一个位置块来拒绝对项目外部区域的访问。

 类似资料:
  • 我无法访问静态内容(angular app),甚至无法访问简单的索引。来自spring boot的html文件。我一直收到404错误。Spring没有为我提供这些静态文件。自从升级到Spring Boot 2.2.4后,我就遇到了这个问题。我必须升级以应对Zip64问题。 我的application.properties里有这样一句话: 我也有自己的staticResourceConfigurat

  • 问题内容: 我无法让Spring-boot项目提供静态内容。 我已经放在一个命名的文件夹下。在其中,我有一个名为的文件夹。当我打包应用程序并运行它时,它找不到我放在该文件夹中的图像。 我试图把静态文件中,并但没有任何工程。 如果我我可以看到文件在正确文件夹的jar中:例如,但是调用:http://localhost:8080/images/head.png,我得到的只是一个404 有什么想法为什么

  • 我有以下代码: 我面临的问题对我来说似乎有点奇怪,因为这是我第一次处理POI。 单元格0的内容是一个日期,我对它没有任何问题,但单元格11的内容应该是一个数字,它返回用于获取内容的公式。以下是示例输出: 等等 我需要做的是显示单元格的实际内容,而不是公式。我肯定我错过了一些小东西,但作为一个完全的初学者,我无法发现它。

  • 我有一个Vue.js应用程序,我的部署设置非常标准, 豆荚- 这是相关代码, Dockerfile: Nginx。形态: Ingress Prod:(为了简洁起见,仅保留必要的位), 本地入口: 我得到的错误是, 未捕获的语法错误:意外令牌 未捕获的语法错误:意外令牌 网络选项卡中这两种资源的内容类型都是。 编辑1: 这是部署后我的文件夹的外观, 我的js文件的路径是,

  • 我从1.8升级了nginx。0到1.9。今天7点,为了让一个网站重新工作,他不得不换几行。现在它可以在ssl(443)上工作,但不能在http(80)上工作。我必须将fastcgi_参数更改为fastcgi。形态。 当我通过Cloud flare尝试一个网站时,它说与我的服务器没有连接。直接连接我得到一个下载的文件。看起来像一个压缩文件,在浏览器上没有得到解压缩。这是标题。 浏览器或服务器上没有错

  • 问题内容: 我开始在Go中编写服务器端应用程序。我想使用Accept- Encoding请求标头来确定是否GZIP响应实体。我曾希望找到一种直接使用http.Serve或http.ServeFile方法执行此操作的方法。 这是一个很普遍的要求。我错过了什么还是我需要推出自己的解决方案? 问题答案: 目前尚无对gzip压缩HTTP响应的“开箱即用”支持。但是添加它非常简单。看一下 https://g