我他么傻逼了, tengine/nginx 到底怎么配置,蛤?求纠错

2015-11-03 21:19:51 +08:00
 tntsec

目前使用是正常的,但是我已经搞不清楚 location 到底是干啥的了
limit_req 写在外面,使用完全没有问题,所有页面都能限制
禁止指定 UA 及 UA 为空的访问的 if 却不能写外面,只能 /和 php 各写一个,如果只写 /那就只能首页限制,其他页面照样访问
nginx -t 没有报错
但是逻辑有没有问题?判断顺序对吗

server {
        listen       80;
        server_name www.tntsec.com;

    #设置只是用 https        
    #add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";

    #记录日志,限制缓存为 1M
    access_log  /var/log/nginx/log/tntsec.log  main buffer=1m;
    #主目录
    root   /var/www/html/tntsec;
    #index
    index  index.php index.html index.htm;

    #系统负载控制
    sysguard on;
     sysguard_load load=10 action=/highload.html;

    #限制并发,白名单
    limit_req_whitelist geo_var_name=white_ip geo_var_value=1;
    #限制并发
    limit_req zone=one burst=5 forbid_action=/nocc.html;
    limit_req zone=two forbid_action=/nocc.html;
    limit_req zone=three burst=5 forbid_action=/nocc.html; 

    #屏蔽指定 IP
    #deny 1.1.1.1;


    #伪静态
    if (!-e $request_filename) {
                 rewrite ^(.*)$ /index.php$1 last;
                         }


    location  / {
    #禁止指定 UA 及 UA 为空的访问
    if ($http_user_agent ~ "FeedDemon|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|YisouSpider|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|^$" ) {
    access_log off;
         return 403;            
    }

    #判断是不是使用代理访问,使用 CDN 请勿开启
    #if ($http_x_real_ip != $http_x_forwarded_for){
    #                access_log off;
    #                return 403;
    #            }

    #禁止非 GET|HEAD|POST 方式的抓取
    if ($request_method !~ ^(GET|HEAD|POST)$) {
    access_log off;
        return 403;
    }
    }

    location ~ .*\.(css|js|ico|png)(.*) {
                  expires 30d;
                 }
    #       error_page  404              /404.html;
    #       error_page 500 502 503 504 =    /500.html;




    location ~ ^/admin/ {
          deny all;
    }

    location ~ /(usr/uploads|usr/plugins/CommentToMail/cache|usr/plugins/Sitemap/sitemap|usr/plugins/CommentToMail/log)/.*\.(php|php5)?$
    {
    deny all;
    }

    location ~ .*\.php(\/.*)*$ {
    #fastcgi_pass 127.0.0.1:9000;
    #location ~ [^/]\.php(/|$) {
    if ($http_user_agent ~ "FeedDemon|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|YisouSpider|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|^$" ) {
    access_log off;
        return 403;
    }
    if ($request_method !~ ^(GET|HEAD|POST)$) {
    access_log off;
        return 403;
        }

            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            fastcgi_pass unix:/dev/shm/fpm-cgi.sock;
            #fastcgi_index index.php;
            fastcgi_param PATH_INFO $fastcgi_path_info; 
            fastcgi_param  PHP_VALUE  "open_basedir=$document_root:/tmp/:/proc/";
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            }
    }
6381 次点击
所在节点    NGINX
24 条回复
tntsec
2015-11-03 21:20:14 +08:00
求姿势
ryd994
2015-11-03 21:36:39 +08:00
有时间来伸手为什么不去看看 error log ?
还有,所有 if!e 都应该用 try_files 代替
认真读完文档,自己写一个出来不难
网上拼拼凑凑的模板真是害死人
ryd994
2015-11-03 21:39:17 +08:00
还有限制 UA 没有任何卵用
对付搜索引擎用 robots
如果对方不遵守 robots ,又凭什么会怪怪写 UA 呢?
msg7086
2015-11-03 21:43:48 +08:00
已经警告过你了,不要随便用 if 。

nginx 的 conf 不是程序,是申明性的配置文件,不是从上到下一句句执行的,不要把它当成是程序一样去理解。完全是两回事。

(特别是这种两个 if 混在一起的情况。多个 if 甚至可能直接造成 SIGSEGV
xfspace
2015-11-03 21:46:29 +08:00
看博客是牛逼人物啊。为什么不问问神奇海螺呢?
wdlth
2015-11-03 22:14:02 +08:00
既然 useragent 是一样的,可以先判断完后设置一个变量值,再判断变量值也行,不用写两遍。
tntsec
2015-11-03 22:28:18 +08:00
@ryd994 这个可以有,这个是 typecho 官方文档里的写法,并没有在意,现在改成了

try_files $uri $uri/ /index.php;

限制 UA 这个其实本意是为了限制空 UA ,防不会加载 UA 的低级 CC
tntsec
2015-11-03 22:29:05 +08:00
@msg7086 判断 UA 和进制其他方式访问的两个 if 能写到一起吗?
tntsec
2015-11-03 22:35:46 +08:00
@xfspace 神奇海螺也不知道呢
tntsec
2015-11-03 22:36:38 +08:00
@wdlth 这个好像没啥,用变量赋值感觉跟直接赋值一样,从效率上看都是加载同样的东西
Andy1999
2015-11-03 22:49:55 +08:00
你以为百度的都是对的吗::doge
msg7086
2015-11-03 22:52:08 +08:00
@tntsec 不太清楚。用 if 是属于「非正常逻辑」,需要多试试多调教才行。
tntsec
2015-11-03 22:52:19 +08:00
@Andy1999 试了才知道,我不认为 V2EX 里都是对的,文档里对每个选项都讲得太过详细,百度的互相转载。但是真像就在里面
tntsec
2015-11-03 22:53:34 +08:00
@msg7086 if 不是正常逻辑吗,判断是不是空 ua ,是就返回 403.判断是不是不常见的请求方式,是就是 403 ,其他 200
ryd994
2015-11-04 02:00:46 +08:00
@tntsec 我可以向你保证,百度搜索的结果里是找不到真相的。真相只存在于源代码里,最接近真相的是官方英文文档,(伪官方)中文文档严重滞后。你嫌官方文档详细,我还嫌不够呢。连某个指令在哪个 phase 执行都不说。 V2EX 不全对,但质量绝对比百度高得多。

如果你觉得 if 就是普通的条件判断,那说明你 Nginx 不及格。 if 是 rewrite 模块内的指令,不是 Nginx 配置的语法结构。如果 if 和其他模块的配置配合,而你又不明白其内在机制的话,会有非常……呃……奇妙的效果。个人认为,学习 Nginx ,应该从 if is evil 开始 https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

你先看了这篇再说: http://www.aosabook.org/en/nginx.html
ericFork
2015-11-04 02:36:39 +08:00
看着这么乱的代码排版,这么多的超长 if ,真是看不下去。
Livid
2015-11-04 03:31:33 +08:00
如果想要写简单的 WAF 逻辑的话,还是上 ngx_lua 吧。 Nginx 自己的 if 真心不靠谱。
coolloves
2015-11-04 07:03:15 +08:00
mark
kn007
2015-11-04 07:34:27 +08:00
千万别用 if ,心中的痛
ynztyl10
2015-11-04 09:30:36 +08:00
if 语句慎用。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/233372

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX