小编Ali*_*i W的帖子

Nginx 使用 try_files 将 https 上不存在的文件重定向到 http

我已经搜索了一段时间的正确方法来做到这一点。

在 https 服务器上请求不存在的文件时重定向到 http 服务器并请求相同的文件。

例如。

https://example.org/some_missing_file.html - 重定向 -> http://example.org/some_missing_file.html

https://example.org/existing_file.html - 提供文件

https://example.org/SomeDir/missing_file - 重定向 -> http://example.org/SomeDir/missing_file

https://example.org/SomeMissingDir/ - 重定向 -> http://example.org/SomeMissingDir/missing_file

这个基于 if 的代码段有效

listen 443 ssl;

#... more config

if (!-e $request_filename) {
    rewrite ^ http:// example.org$request_uri permanent;
    break;
}
Run Code Online (Sandbox Code Playgroud)

但是“如果是邪恶的” - http://wiki.nginx.org/IfIsEvil

所以这是我对 try_files 版本的尝试 - 这不起作用。

   try_files $uri @redirect;

   location @redirect {
           rewrite ^ http:// example.org$request_uri permanent;
           break;
   }
Run Code Online (Sandbox Code Playgroud)

我已经尝试了很多变种;proxy_redirects,返回 302 - 当文件位于子目录中时,它们无法重定向或不起作用,或者如果为空则不重定向根。

有没有人有基于 try_files 的防弹替换?

(ps。由于链接检查器不了解example.org而导致的空格!)

nginx

9
推荐指数
1
解决办法
1万
查看次数

Nginx:如何基于用户代理限制请求速率

尝试在 nginx 中实现以下行为

使用浏览器时,每个 ip 的默认速率限制为 1r/s。bing 和 google 蜘蛛的速率限制为 10r/s。拒绝不良机器人。

不幸的是,谷歌没有发布 googlebot 的 IP 地址,所以我只能使用 useragent。

到目前为止,这已经很接近了:

http { 
  # Rate limits
  map $http_user_agent $uatype {
    default 'user';
    ~*(google|bing|msnbot) 'okbot';
    ~*(slurp|nastybot) 'badbot';
  }

  limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
  limit_req_zone $binary_remote_addr zone=two:10m rate=10r/s;

  ...

  server {
    ...

    location / {
      if ($uatype == 'badbot) {
        return 403;
      }

      limit_req zone=one burst=5 nodelay;
      if ($uatype != 'user') {
        limit_req zone=two burst=10 nodelay;
      }

      ...
    }

  ...
  }
}
Run Code Online (Sandbox Code Playgroud)

但是 - 'if' 不允许这样做。 …

nginx rate-limiting

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

nginx ×2

rate-limiting ×1