Nginx   发布时间:2022-05-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何干掉这个Nginx配置?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在使用Nginx 0.8.54尝试以尽可能的方式实现以下目标:

>代理直接到localhost:8060如果cookie no_cache为true或者请求方法不是GET.
>否则从$document_root / static / $uri提供静态文件.
>如果不存在此类文件,请尝试$document_root / cache / $uri和$document_root / cache / $uri.html.
>如果请求路径为/,请尝试不使用静态文件,只需$document_root / cache / index.html.
>如果找不到静态文件或缓存文件,最后回退到localhost:8060.

当前配置文件:

server {
    root /srv/web/example.com;
    server_name example.com;

    LOCATIOn @BACkend { proxy_pass http://localhost:8060; }

    LOCATIOn / {
        if ($cookie_no_cache = truE) { proxy_pass http://localhost:8060; }
        if ($request_method != GET) { proxy_pass http://localhost:8060; }
        try_files /static/$uri /cache/$uri /cache/$uri.html @BACkend;
    }

    LOCATIOn = / {
        if ($cookie_no_cache = truE) { proxy_pass http://localhost:8060; }
        if ($request_method != GET) { proxy_pass http://localhost:8060; }
        try_files /cache/index.html @BACkend;
    }
}
最佳答案
http {
  map $cookie_no_cache $cacheZone {
    default "";
    true    X;
  }

  server {
    root /srv/web/example.com;
    server_name example.com;

    error_page 405 = @BACkend;

    LOCATIOn / {
      try_files /cache$cacheZone/$uri.html /static$cacheZone/$uri
                /cache$cacheZone/$uri @BACkend;
    }

    LOCATIOn @BACkend {
      proxy_pass http://localhost:8060;
    }
  }
}

说明.

>关于“no_cache”cookie检查.我们正在用Nginx地图替换它.变量$cacheZone取决于$cookie_no_cache的值.默认情况下它是空的,但是如果有一个“no_cache = true”cookie,我们将$cacheZone设置为任何值来修改try_files中的静态文件搜索路径 – 我希望你的服务器根目录下没有/ cacheX和/ staticX文件夹(如果是,请为$cacheZone选择另一个值)
> Nginx无法将http方法PUT或POST应用于静态文件(这没有意义),因此在这种情况下它会发出http错误405“Not Allowed”.我们通过error_page拦截它并将请求传递给@BACkend位置.

替代方法

否则,使用proxy_cache:

http {
  proxy_cache_path example:1R_780_11845@;

  server {
    root  /srv/web/example.com;
    server_name example.com;

    LOCATIOn / {
      proxy_cache example;
      proxy_cache_bypass $cookie_no_cache;
      proxy_cache_valid 200 10s;
      proxy_pass http://localhost:8060;
    }
  }
}

大佬总结

以上是大佬教程为你收集整理的如何干掉这个Nginx配置?全部内容,希望文章能够帮你解决如何干掉这个Nginx配置?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: