nginx优化

2019-11-10  本文已影响0人  August________

nginx优化

然后客户机访问Web服务器验证效果

  1. 问题:Tengine和nginx的关系?

​ Tengine是淘宝开源nginx的分支,

  1. 问题:访问nginx是出现状态码"403 forbidden"

自定义报错页面

[root@web-0002 ~]# vim /usr/local/nginx/conf/nginx.conf
 location / {
            root   html;
            index  index.php index.html index.htm ;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            set_real_ip_from 192.168.1.11;
}
        error_page  404              /404.html;  //自定义错误页面

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {

[roxinxot@web-0002 ~]# vim /usr/local/nginx/html/404.html
Oops,No NO no page …
[root@web-0002 ~]# nginx  -s reload

状态码 功能描述
200 一切正常
301 永久重定向
302 临时重定向
401 有户名或密码错误
403 禁止访问(客户端IP地址被拒绝)
404 文件不存在
414 请求URI头部过长
500 服务器内部错误
502 Bad Gateway

如何查看服务器状态信息

[root@web-0002 ~]# vim /usr/local/nginx/conf/nginx.conf
location /status {
                stub_status on;
           [root@web-0002 ~]# vim /usr/local/nginx/conf/nginx.conf      #allow IP地址;
                 #deny IP地址;
        }
  
[root@web-0002 ~]# nginx  -s reload
[root@web-0002 ~]# curl http://192.168.1.12/status
Active connections: 1   //当前活动的连接数量
server accepts handled requests
 5983 5593 3939 
Reading: 0 Writing: 1 Waiting: 0 

accepts:已经接受客户端的连接总数连
handled:已经处理客户端的连接总数量
Request:客户端发送的请求数
Reading:当前服务器正在读取客户端请求头的数量
Writing:当前服务器正在写响应信息的数量
Waiting:当前多少客户端在等待服务器的响应.


优化nginx并发量

[root@web-0002 ~]# ab -n 100 -c 100 http://192.168.1.12/
Benchmarking 192.168.1.12 (be patient)
apr_socket_recv: Connection reset by peer (104)
[root@web-0002 ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;  //与CPU核心数量一致

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  65535;  //每个worker最大并发连接数
}

[root@web-0002 ~]# ulimit -a    //查看所有属性值
[root@web-0002 ~]# ulimit -Hn 100000   //设置硬限制(临时规则)
[root@web-0002 ~]# ulimit -Sn 100000   //设置软限制(临时规则)
[root@web-0002 ~]# vim /etc/security/limits.conf 
    .. ..
*               soft    nofile            100000
*               hard    nofile            100000
#该配置文件分4列,分别如下:
#用户或组    硬限制或软限制    需要限制的项目   限制的值
[root@web-0002 ~]# ab -n 2000 -c 2000 http://192.168.1.12/

优化nginx数据包头缓存

[root@web-0002 ~]# vim buffer.sh
#!/bin/bash
URL=http://192.168.4.5/index.html?
for i in {1..5000}
do
    URL=${URL}v$i=$i
done
curl $URL    
[root@web-0002 ~]# bash buffer.sh
<center><h1>414 Request-URI Too Large</h1></center>        //提示头部信息过大
[root@web-0002 ]#  vim /usr/local/nginx/conf/nginx.conf
.. ..
http {
client_header_buffer_size    1k;        //默认请求包头信息的缓存    
large_client_header_buffers  4 4k;        //大请求包头部信息的缓存个数与容量
.. ..
}

日志切割

  1. 把久的日志重命名
  2. kill USR1 PID(nginx的进程PID号)
[root@web-0002 logs]# mv access.log access2.log 
[root@web-0002 logs]# kill -USR1  $(cat /usr/local/nginx/logs/nginx.pid)
[root@web-0002 nginx]# vim /usr/local/nginx/logbak.sh
#!/bin/bash
date=`date +%Y%m%d`
logpath=/usr/local/nginx/logs
mv $logpath/access.log $logpath/access-$date.log
mv $logpath/error.log $logpath/error-$date.log
kill -USR1 $(cat $logpath/nginx.pid)
[root@00 00 * * * /usr/local/nginx/logbak.sh-0002 nginx]# crontab -e

[root@web-0002 nginx]# awk '{ip[$1]++}END{for(i in ip){print ip[i],i}}' /usr/local/nginx/logs/access2.log
针对文本排序输出可以采用sort命令,相关的常见选项为-r、-n、-k。其中-n表示按数字顺序升序排列,而-r表示反序,-k可以指定按第几个字段来排序。
[root@web-0002 nginx]# awk '{ip[$1]++}END{for(i in ip){print ip[i],i}}' /usr/local/nginx/logs/access2.log  | sort -nr

对页面进行压缩处理

[root@web-0002 nginx]# vim /usr/local/nginx/conf/nginx.conf
http {
.. ..
gzip on;                            //开启压缩
gzip_min_length 1000;                //小文件不压缩
gzip_comp_level 4;                //压缩比率
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
                                    //对特定文件压缩,类型参考mime.types
.. ..
}

服务器内存缓存

[root@web-0002 nginx]# vim /usr/local/nginx/conf/nginx.conf
http { 
open_file_cache          max=2000  inactive=20s;
        open_file_cache_valid    60s;
        open_file_cache_min_uses 5;
        open_file_cache_errors   off;
//设置服务器最大缓存2000个文件句柄,关闭20秒内无请求的文件句柄
//文件句柄的有效时间是60秒,60秒后过期
//只有访问次数超过5次会被缓存
} 

隐藏nginx软件版本号信息

[root@web-0002 nginx]# curl -I 192.168.1.12
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 22 May 2019 08:56:37 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.16
[root@web-0002 nginx]# vim /usr/local/nginx/conf/nginx.conf
http {
     server_tokens off;
 .........
[root@web-0002 nginx]# nginx -s reload
[root@web-0002 nginx]# curl -I 192.168.1.12
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 22 May 2019 08:59:01 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.16

不记录不需要的访问日志

[root@web-0002 nginx]# vim /usr/local/nginx/conf/nginx.conf
    location ~.*\.(js/jpg/JPG/gif/IGF)${
        access_log off;
    }

nginx图片及目录放盗链解决方案

Nginx防爬虫优化

nginx程序架构优化

三者的功能分离,分别使用独立的服务器还可以前端负载均衡Haproxy/nginx上,根据URI 过滤请求.

使用普通用户启动Nginx(监牢模式)

  1. 添加用户,创建目录和文件
[root@web-0002 ~]# useradd inca
[root@web-0002 ~]# su - inca
[inca@web-0002 ~]$ pwd
/home/inca
[inca@web-0002 ~]$ mkdir conf logs www
[inca@web-0002 ~]$ cp /usr/local/nginx/conf/mime.types ~/conf/
[inca@web-0002 ~]$ echo inca > www/index.html

  1. 配置nginx配置文件
[inca@web-0002 ~]$ vim conf/nginx.conf
内容见老男孩Web集群书336页
上一篇 下一篇

猜你喜欢

热点阅读