【问题标题】:nginx backend returns 404, when using intercept directive it returns 403nginx 后端返回 404,使用拦截指令时返回 403
【发布时间】:2021-06-27 06:43:14
【问题描述】:

我有以下位置的 nginx 配置:

location /some/path- {
  limit_except GET { deny all; }
  proxy_pass            http://my_app;
}

location / {
  deny all;
}

我只允许对 /some/path- 的 GET/HEAD 请求。 我需要对/some/path- 的所有请求,这些请求在后端都是无效路径,以返回 nginx 的 404.html 而不是后端。

位置好: http://some/path-exists

位置不好: http://some/path-doesnt-exist

当我尝试添加时:

proxy_intercept_errors on;

error_page 404 /404.html;

当浏览到错误位置时,我似乎得到 403 Forbidden 而不是 nginx 404.html 页面。我不明白为什么。

【问题讨论】:

  • 你有一个deny all。尝试添加location = /404.html {}
  • 它不起作用..我浏览some/path-doesnt-exist时仍然得到403@

标签: nginx nginx-location


【解决方案1】:

根据您的配置,这是正常行为。 NGINX 为传入请求找到最佳匹配的位置块。在你的情况下:

/some/path-sfsdafhsdaiufasdf` -> Match /some/path-
/some/path/sadfasdf           -> Match /

但是您的后备位置 / 有一个 deny all。这就是为什么您会看到 403 被禁止的原因。这与proxy_intercept_errors on无关。

试试这个

server {                                        
  listen 8001;
  # Convert all 403 to 404 and send it to the internal 404-location.                                  
  error_page 403 =404 /@404;                    
  location /some/path- {                         
    limit_except GET { deny all; }              
    proxy_pass http://mybackend;           
  }                                             
                                                
  location / {                                  
    deny all;                                   
  }                                             
                                                
  location /@404 {                              
   return 404 "Ouch, that did not work well!\n";   
  }                                             
                                                
}                                               

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 2015-10-12
    • 2014-05-05
    • 1970-01-01
    • 2019-03-10
    • 2018-09-08
    • 2019-09-18
    相关资源
    最近更新 更多