gpt4 book ai didi

nginx - 为什么 nginx "location =/"规则不起作用?

转载 作者:行者123 更新时间:2023-12-04 18:04:11 25 4
gpt4 key购买 nike

我想通过使用“location =/”规则来定义“/”请求的精确匹配,并指定“index.html”作为该请求的响应。但是为什么我的设置不起作用?

我定义了两个位置如下( 更新 :我还在底部发布了我的 nginx.conf 的全部内容。):

location = / {
root /opt/www/static/;
index index.html;
}
location / {
root /opt/www/resource/;
}

我的“/opt/www”目录中的文件如下。 ( # The content is: 后面的注释描述了其中的文件内容。)
/opt/www
├── resource
│   ├── hello.html # The content is: The hello.html from /resource
│   └── index.html # The content is: The index.html from /resource
└── static
└── index.html # The content is: The index.html from /static

但是当我访问以下网址时,输出是:
  • http://localhosthttp://localhost/ - 回复是:The index.html from /resource .
  • http://localhost/index.html - 回复是:The index.html from /resource .
  • http://localhost/hello.html - 回复是:The hello.html from /resource .

  • 我认为#2 和#3 的结果是正确的,但是对于#1,为什么它返回 resource/index.html作为响应而不是 static/index.html ?因为我认为,根据 location的定义,响应应该来自 static/index.html文件。

    using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison.



    另一个问题是,如何更改我的 conf 文件以指定 static/index.html作为 http://localhost 的回应或 http://localhost/通过使用完全匹配?

    更新

    我在使用 error_log logs/error.log debug; 打开 nginx 调试日志后发现了这个技巧。 .根据日志,对于#1,请求与第一条规则 location = /完全匹配, 和 /opt/www/static/index.html开了。但后来,请求被内部重定向到 /index.html ,则匹配第二条规则,结果, /opt/www/resource/index.html用来。

    但我的问题是,为什么它将请求重定向到 /index.html当确切的规则(第一个)已经匹配并且 /opt/www/static/index.html找到了吗?我可以用一些配置或其他指令停止内部重定向吗?

    nginx日志是(我的nginx版本是1.4.6):
    2015/05/01 11:59:46 [debug] 112241#0: *1 http process request line
    2015/05/01 11:59:46 [debug] 112241#0: *1 http request line: "GET / HTTP/1.1"
    2015/05/01 11:59:46 [debug] 112241#0: *1 http uri: "/"
    2015/05/01 11:59:46 [debug] 112241#0: *1 http args: ""
    2015/05/01 11:59:46 [debug] 112241#0: *1 http exten: ""
    *** omit some logs to process request header and others ***
    2015/05/01 11:59:46 [debug] 112241#0: *1 event timer del: 3: 1430452846698
    2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 0
    2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 1
    2015/05/01 11:59:46 [debug] 112241#0: *1 test location: "/"
    2015/05/01 11:59:46 [debug] 112241#0: *1 using configuration "=/"
    2015/05/01 11:59:46 [debug] 112241#0: *1 http cl:-1 max:1048576
    2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 3
    2015/05/01 11:59:46 [debug] 112241#0: *1 post rewrite phase: 4
    2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 5
    2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 6
    2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 7
    2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 8
    2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 9
    2015/05/01 11:59:46 [debug] 112241#0: *1 post access phase: 10
    2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 11
    2015/05/01 11:59:46 [debug] 112241#0: *1 open index "/opt/www/static/index.html"
    2015/05/01 11:59:46 [debug] 112241#0: *1 internal redirect: "/index.html?"
    2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 1
    2015/05/01 11:59:46 [debug] 112241#0: *1 test location: "/"
    2015/05/01 11:59:46 [debug] 112241#0: *1 using configuration "/"
    2015/05/01 11:59:46 [debug] 112241#0: *1 http cl:-1 max:1048576
    2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 3
    2015/05/01 11:59:46 [debug] 112241#0: *1 post rewrite phase: 4
    2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 5
    2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 6
    2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 7
    2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 8
    2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 9
    2015/05/01 11:59:46 [debug] 112241#0: *1 post access phase: 10
    2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 11
    2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 12
    2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 13
    2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 14
    2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 15
    2015/05/01 11:59:46 [debug] 112241#0: *1 http filename: "/opt/www/resource/index.html"

    再次更新 发布我的 nginx.conf 的全部内容。
    worker_processes  1;
    error_log /var/log/nginx/error.log debug;
    pid /var/run/nginx.pid;
    events {
    worker_connections 1024;
    }
    http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log main;
    sendfile on;
    keepalive_timeout 65;
    include /etc/nginx/conf.d/*.conf;
    server {
    listen 80;
    server_name localhost;
    location = / {
    root /opt/www/static/;
    index index.html;
    }
    location / {
    root /opt/www/resource/;
    }
    }
    }

    最佳答案

    在做了一些研究和谷歌之后,我找到了一个自己解决这个问题的解决方案:-)。使用 try_files指令替换 index一,当static/index.html时不触发内部重定向被发现。最终的定义是:

    location = / {
    root /opt/www/static/;
    #index index.html;
    try_files /index.html =404;
    }
    location / {
    root /opt/www/resource/;
    }

    关于nginx - 为什么 nginx "location =/"规则不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29980957/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com