【问题标题】:nginx: location = / {} errornginx:位置=/{}错误
【发布时间】:2018-02-01 18:42:21
【问题描述】:

我的 nginx vhost 配置内容:

server {
    listen 80;
    server_name t.xianzhi.xxx.domain;
    access_log /data/log/nginx/t.xianzhi.xxx.domain_access.log main;

    location ~ /\. {deny  all;}

    proxy_set_header    X-Real-IP       $remote_addr;
    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header    Host            $host;

    location = / {
        root /data/web/static/html;
        index index.html;
    }

    location / {
        proxy_pass          http://127.0.0.1:9000;
    }

    location = /favicon.ico {
        access_log off;
        root /data/web/static/;
    }

    location = /apple-app-site-association {
        add_header Content-Type "text/html; charset=utf-8";
        root /data/web/show/public/wap/;
    }

    location ~ \.(css|js|png|jpg|woff|ttf)$ {
        root /data/web/static;
        expires 10d;
    }

}

作为配置,我想将路径 / 提供给 /data/web/static/html/index.html 并将其他路径提供给 proxy_pass。

事实是/的路径是404没有找到,其他的都是成功的。

日志是:

24/Aug/2017:10:49:43 +0800  10.5.17.37  t.xianzhi.xxx.domain    -   curl/7.51.0 -   request:GET / HTTP/1.1  bbs:233status:404   upad:127.0.0.1:9000 rspt:0.017  rqtt:0.017  request_body:-

所以,/ 被传递给代理。

一些信息:

nginx版本:nginx/1.10.1

那么,如何解决呢?

【问题讨论】:

    标签: nginx nginx-location


    【解决方案1】:

    问题在于您的= / 位置块。如果您考虑该部分

    location = / {
        root /data/web/static/html;
        index index.html;
    }
    

    您指定了根目录和 index.html,但您不提供任何服务。所以你应该把它改成

    location = / {
        root /data/web/static/html;
        index index.html;
        try_files /index.html =404;
    }
    

    location = / {
        root /data/web/static/html;
        try_files /index.html =404;
    }
    

    【讨论】:

    • 我试过这个。好消息是/ 路径现在在这里匹配。而坏消息仍然是404;为了区别,我的配置是`try_files index.html =502;`。它返回 502。我可以确认文件 `/data/web/static/html/index.html` 存在。
    • 让我确认一下该块是否会被调用
    • 所以= 块确实被调用了。检查我的编辑是否有帮助
    • 啊不知道我在想什么./
    猜你喜欢
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2022-01-17
    • 1970-01-01
    • 2018-04-01
    • 2019-02-07
    相关资源
    最近更新 更多