Author: jpuyy

  • sort命令

    对 /etc/hosts 文件排序默认是按字符串

    如果对 ip 排序

    sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts
    sort -t . +0 -1n +1 -2n +2 -3n +3 -4n /etc/hosts

    发现一个简单方法

    sort -V /etc/hosts

    排序命令Sort
    sort -k 2,2
    -k, –key=POS1[,POS2]
    start a key at POS1 (origin 1), end it at POS2 (default end of line)

  • 统计进程占用内存

    
    ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
    
  • mysql添加联合唯一索引

    为表 project_server 添加联合唯一索引

    mysql> alter table project_server add unique index(project_id,object_id);
    ERROR 1062 (23000): Duplicate entry '11-1177' for key 'project_id'

    如果提示说记录已经存在,那么加上 ignore,会自动将重复的去掉,很有效

    mysql> alter ignore table project_server add unique index(project_id,object_id);
    Query OK, 166 rows affected (0.06 sec)
    Records: 166  Duplicates: 1  Warnings: 0
  • nginx block常用爬虫

    代码如下
    agent匹配不区分大小写

    if ($http_user_agent ~* "qihoobot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot|bingbot|ChinasoSpider|Sogou inst spider") {
        return 403; 
    }
    

    上面的没有 baidu 百度的为

    Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)
  • 使用nat时net.ipv4.tcp_timestamps参数

    在使用 iptables 做 nat 时,发现内网机器 ping 某个域名 ping 的通,而使用 curl 测试不通

    原来是 net.ipv4.tcp_timestamps 设置了为 1 ,即启用时间戳

    cat /proc/sys/net/ipv4/tcp_timestamps

    这时将其关闭

    修改 /etc/sysctl.conf 中

    net.ipv4.tcp_timestamps = 0
    sysctl -p

    生效

    原理

    问题出在了 tcp 三次握手,ping 的通 icmp ok ,http ssh mysql 都不 ok

    经过nat之后,如果前面相同的端口被使用过,且时间戳大于这个链接发出的syn中的时间戳,服务器上就会忽略掉这个syn,不返会syn-ack消息,表现为用户无法正常完成tcp3次握手,从而不能打开web页面。在业务闲时,如果用户nat的端口没有被使用过时,就可以正常打开;业务忙时,nat端口重复使用的频率高,很难分到没有被使用的端口,从而产生这种问题。

    只有客户端和服务端都开启时间戳的情况下,才会出现能ping通不能建立tcp三次握手的情况

     

  • php array merge

    两个数组合并为一个数组

    $a = array(1 => 1, 2 => 2, 3 => 3);
    $b = array(3 => 3, 5 => 5, 6 => 6);
    $c = $a + $b;
    print_r($c);
    

    结果

    Array
    (
        [1] => 1
        [2] => 2
        [3] => 3
        [5] => 5
        [6] => 6
    )