• 请不要在回答技术问题时复制粘贴 AI 生成的内容
LxnChan
V2EX  ›  程序员

如何在 Nginx 中限制特定 UA 访问特定路径(或文件)?

  •  
  •   LxnChan ·
    lxnchan · May 20, 2022 · 2173 views
    This topic created in 1479 days ago, the information mentioned may be changed or developed.

    能做到这个效果就行。

    5 replies    2022-05-22 11:06:49 +08:00
    sardina
        1
    sardina  
       May 20, 2022
    应该可以用 lua 实现
    eason1874
        2
    eason1874  
       May 20, 2022
    用 map 匹配定义变量然后用 if 判断,或者直接用 if 正则匹配。原生 if 不支持多条件,写起来有点麻烦。如果有 ngx_lua 或者 njs 模块就很简单

    http://nginx.org/en/docs/http/ngx_http_map_module.html#map
    http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if
    cccssss
        3
    cccssss  
       May 20, 2022
    set $mobile_rewrite do_not_perform;
    if ($http_user_agent ~* "(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino" ) {
    set $mobile_rewrite perform;
    }
    location / {
    root /home/wwwroot/pc.static;
    if ($mobile_rewrite = perform) {
    root /home/wwwroot/pc.static;
    }
    }

    之前写的一个判断 pc 还是手机设备的逻辑
    MeteorCat
        4
    MeteorCat  
       May 21, 2022 via Android
    我感觉挂 ngx_lua 处理好点
    myki
        5
    myki  
       May 22, 2022
    caddy 不香吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2677 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 10:36 · PVG 18:36 · LAX 03:36 · JFK 06:36
    ♥ Do have faith in what you're doing.