1.Nginx 实现一个 IP 访问总流量限制

http {
    limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
    limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
    # 其他配置
}
server {
    listen 80;
    server_name example.com;

    # 限制单个客户端 IP 最大连接数为 2
    limit_conn conn_zone 2;
    # 限制单个客户端 IP 1s 内最多发起 1 个请求
    limit_req zone=req_zone burst=5;
    # 计算客户端 IP 累计请求流量,限制累计请求流量为 1GB
    set $limit_rate 128k;
    limit_rate_after 500M;
    limit_rate 1m;
    # 其他处理逻辑
}

2. 统计服务器所有 url 被请求的数量

#统计其他端口时:netstat -pnt | grep :xx | wc -l
netstat -pnt | grep :443 | wc -l

3. 查找请求数前 20 个 IP

#服务器被恶意高频访问时可以用到
netstat -ant |awk '/:443/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A[i],i}' |sort -rn|head -n20

4. 统计文件中某个字符转出现的次数

#如统计文件api_error.log中error关键词出现的次数
grep -o 'error' /www/payment/runtime/logs/api_error.log | wc -l

5. 监听文件

#可以监听整个文件,也可根据关键词监听
cd /www/payment/runtime/logs/

tail -f xxx.log #监听整个文件
tail -f xxx.log | grep 'xxx' #根据关键词监听

6. 查看文件大小

#以下是本人开发中常用到的
一、du命令
du命令是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的。

du -h filepath 直接得出人好识别的文件大小

du -h ljl.txt

或者
du -ah 当前目录下所有文件的大小

二、ls命令
ls命令用来显示目标列表,在Linux中是使用率较高的命令。ls命令的输出信息可以进行彩色加亮显示,以分区不同类型的文件。

ls -l filepath 第五列为文件字节数

ls -l ljl.txt

ls -h filepath h表示human, 加-h参数得到人好读的文件大小

ls -lh ljl.txt

7. 查看 ip 来源地

#使用这个是为了查看异常登录地址
yum -y install whois
whois 119.53.224.203

8. 查看磁盘使用情况

df -h

9. 创建软链接

ln -s [源文件或目录] [目标文件或目录]

例如:创建软链时,先进入根目录的bin目录下

cd /bin

从/bin这个路径创建软链nginx 引入/usr/local/nginx/sbin/nginx

ln -s /usr/local/nginx/sbin/nginx nginx

创建软链之后 重载nginx 
就可以从 /usr/local/nginx/sbin/nginx -s reload 简化为 nginx -s reload

10. 查看是否监听 9000 端口(其他端口雷同)

netstat -ant | grep 9000

11.Nginx 解决跨域问题

#server模块中添加以下内容,修改完后,记得重启nginx
add_header  Access-Control-Allow-Headers '*';
add_header  Access-Control-Allow-Origin '*';
add_header  Access-Control-Allow-Credentials 'true';
add_header  Access-Control-Allow-Methods 'GET,POST,OPTIONS';

#跨域参数解释
#Access-Control-Allow-Origin:服务器默认是不被允许跨域的。给Nginx服务器配置Access-Control-Allow-Origin *后,表示服务器可以接受所有的请求源(Origin),即接受所有跨域的请求。
#Access-Control-Allow-Methods:允许通过的方法,可以防止Content-Type is not allowed by Access-Control-Allow-Headers in preflight response的错误信息
#Access-Control-Allow-Credentials true 表示允许跨域请求携带 cookie
#Access-Control-Allow-Headers:为了防止出现Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response的错误信息。这个错误表示当前请求Content-Type的值不被支持。其实是我们发起了”application/json”的类型请求导致的

12.Nginx 配置 ssl (小白看过来)

server {
    listen 443 ssl;#监听443端口
    #server_name 购买域名后解析到该服务器上,如果没有域名,也可以给此项目单独开端口
    server_name  api.applet.xxx.com;
    root        /xxx/xxxx/xxxx;#root 项目地址
    index       index.php index.html index.htm;#项目入口文件

    #存放证书的地址.pem和.key文件
    ssl_certificate /usr/local/nginx/conf/vhost/cert/xxx.pem;
    ssl_certificate_key /usr/local/nginx/conf/vhost/cert/xxx.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    if (!-e $request_filename){
        rewrite ^/(.*) /index.php last;
    }

    location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
    }
    error_log  /var/log/nginx/api_applet.log error;
}

13.Nginx 开放防火墙端口

#这里讲到的是CentOS7,CentOS7的防火墙和CentOS6不一样了, CentOS 6 系列中的 iptables 相关命令不能用了,Centos7中使用firewalld代替了原来的iptables。

#停止firewall
systemctl stop firewalld.service

#禁止firewall开机启动
systemctl disable firewalld.service

#添加8022端口
firewall-cmd --zone=public --add-port=8022/tcp --permanent

#多个端口:
firewall-cmd --zone=public --add-port=80-90/tcp --permanent

#命令含义:
--zone #作用域
--add-port=8022/tcp #添加端口,格式为:端口/通讯协议
--permanent #永久生效,没有此参数重启后失效

#centos7查看防火墙所有信息
firewall-cmd --list-all

#centos7查看防火墙开放的端口信息
firewall-cmd --list-ports

#删除8022端口
firewall-cmd --zone=public --remove-port=8022/tcp --permanent

#重启防火墙
firewall-cmd --reload

常用命令介绍:
firewall-cmd --state                ##查看防火墙状态,是否是running
firewall-cmd --reload               ##重新载入配置,比如添加规则之后,需要执行此命令
firewall-cmd --get-zones            ##列出支持的zone
firewall-cmd --get-services         ##列出支持的服务,在列表中的服务是放行的
firewall-cmd --query-service ftp    ##查看ftp服务是否支持,返回yes或者no
firewall-cmd --add-service=ftp      ##临时开放ftp服务
firewall-cmd --add-service=ftp --permanent     ##永久开放ftp服务
firewall-cmd --remove-service=ftp --permanent  ##永久移除ftp服务
firewall-cmd --add-port=80/tcp --permanent     ##永久添加80端口

转自:https://learnku.com/articles/78462