nginx的优化方案

刚发布了一个系统优化的脚本,继续在发布一个nginx的优化方案的。可能不是很全,有需要的就看下吧。

下面我直接把注释出来的,给大家看下。

nginx优化:{
    1.隐藏版本号,修改源文件{src/http/ngx_http_header_filter_module.c
                            src/http/nginx_http_special_response.c
                            src/core/nginx.h}文件内的nginx字段
    2.隐藏软件名,在主配置文件内的http内加入server_tokens off;
    3.更改nginx服务的默认用户,默认用户为nobody.
    4.优化nginx的worker进程数
       worker_processes字段,最大为cpu总和数的2倍{查看CPU为cat /proc/cpuinfo
                                                    查看每个CPU是几核为grep "physical" /proc/cpuinfo}
    5.绑定不同的nginx进程到不同的cpu上{worker_cpu_affinity 0001 0010 0100 1000;为4核CPU默认不平局分配请求}
    6.nginx事件处理模型的优化:{就是events段内,use epoll模型}
    7.调整nginx单个进程允许的最大连接数,worker_connections数,根据具体情况调节
    8.配置nginx worker进程最大打开文件数,worker_rlimit_nofile的数,防止高负载宕机。
    9.设置linux的允许的最大外部链接,用ulimit设置,最大为65535,一般不设置最大,防止宕机
    10.开启高效文件传输模式,sendfile on,开启后,可设置{tcp_nopush on;减少网络报文段的数量
            tcp_nodelay on;可防止网络或磁盘I/O阻塞}
    11.客户端报头超时,client_header_timeout 15;网络情况或者高负载状态下的客户等待
    12.客户端主体超时,client_body_timeout 15;慢查询的sql语句导致的
    13.连接后端PHP的相关调优,主要为fastCGI模块的调优。{
        包括http段内的{
                fastcgi_connect_timeout 240; #Nginx 允许 fcgi 连接超时时间
                fastcgi_send_timeout 240; #Nginx 允许 fcgi 返回数据的超时时间
                fastcgi_read_timeout 240; #Nginx 读取 fcgi 响应信息的超时时间
                fastcgi_buffer_size 64k; #Nginx 读取响应信息的缓冲区大小
                fastcgi_buffers 4 64k; # 指定 Nginx 缓冲区的数量和大小
                fastcgi_busy_buffers_size 128k; # 当系统繁忙时 buffer 的大小
                fastcgi_temp_file_write_size 128k; #Nginx 临时文件的大小
                # fastcgi_temp_path /data/ngx_fcgi_tmp; # 指定 Nginx 临时文件放置路径
                fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d; 
                # 指定 Nginx 缓存放置路径}
        location内的{
                fastcgi_cache ngx_fcgi_cache; 
                # 开启festcgi缓存并起名叫ngx_fcgi_cache, 有效降低 CPU 负载,并且防止 502 错误发生。
                fastcgi_cache_valid 200 302 1h; 
                # 指定应答代码的缓存时间, 1h=1 小时
                fastcgi_cache_valid 301 1d; #1d=1 天
                fastcgi_cache_valid any 1m; #and 1m :将其他应答缓存 1 分钟
                fastcgi_cache_min_uses 1; # 待缓存内容至少要被用户请求过 1 次
                fastcgi_cache_use_stale error timeout invalid_header http_500; 
                # 当遇到 error , timeout ,或者返回码 500 时,启用过期缓存返回用}
                    }
    14.配置nginx gzip压缩,实现性能优化,主要对纯文本的内容压缩,必须大于1K,否则会越压缩越大。
                {####### 压缩的配置介绍 ######
                gzip on;# 开启 gzip 压缩功能
                gzip_min_length 1k; # 设置允许压缩的页面最小字节数,页面字节数从 header 头的
                Content-Length 中获取。默认值 0 ,表示不管页面多大都进行压缩。
                建议设置成大于 1K ,如果小于 1K 可能会越压越大。
                gzip_buffers 4 16K; # 压缩缓冲区大小。表示申请 4 个单位为 16K 的内存作为压缩结果流缓存
                默认值是申请与原始数据大小相同的内存空间来存储 gzip 压缩结果。
                gzip_http_version 1.1;}
    15.配置nginx expires缓存实现性能优化,主要是对视频,图片,CSS,JS等网站元素缓存时间的设置。
                比如设置图片缓存,或者代码缓存时间处理:
                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
                {
                    expires 3650d
                }
                location ~ ^/(images|javascript|js|css|flash|media|static)/
                {
                    expires 360d;
                }
    16.nginx日志的相关优化与安全处理:
        1.编写日志切割脚本:
            [root@localhost nginx]# cat /server/scripts/cut_nginx_log.sh
            #!/bin/bash
            # 日志切割脚本可挂定时任务,每天 00 点整执行

            Dateformat=`date +%Y%m%d`
            Basedir="/usr/local/nginx"
            Nginxlogdir="$Basedir/logs"
            Logname="access"

            [ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
            [ -f ${Logname}.log ] || exit 1
            /bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
            $Basedir/sbin/nginx -s reload

            [root@localhost nginx]# cat >>/var/spool/cron/root << KOF
            #cut nginx access log by Mr.chen
            00 00 * * * /bin/bash /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
        2.不记录不需要的访问日志:
            location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$
            {
                access_log off;# 这里用 location 标签匹配不记录日志的元素扩展名,然后关闭日志
            }
        3.访问日志的权限设置,主要是把日志目录设置root用户为属组属主,权限掩码设置700
    17.配置nginx禁止非法域名解析访问企业网站,主要有3种方法:
        1.让使用IP访问网站的用户,或者恶意解析域名的用户,收到501错误,命令配置如下:
            server {
                listen 80 default_server;
                server_name_;
                return 501;
            }
        2.通过301跳转到主页,命令配置如下:
            server {
                listen 80 default_server;
                server_name_;
                rewrite ^(.*) http://www.yunjisuan.com/$1 permanent;
            }
        3.发现有人恶意解析公司服务器IP,在server段内添加如下代码,如有多个server则要多处添加
            if ($host ! ~ ^www\.yunjisuan\.com$)
            {
                rewrite ^(.*) http://www.yunjisuan.com/$1 permanent;
            }# 如果 header 信息的 host 主机名字非 www.yunjisuan.com ,就 301 跳转到 www.yunjisuan.com
    18.nginx错误页面的优雅显示,主要是在location段内设置error_page的页面文件。
    19.nginx站点目录文件级目录权限优化,一般文件设置644,目录为755.防止被上传木马。
    20.防爬虫处理,一般在网站目录下创建rebots.txt文件,在文件内添加规则。例如:
                User-agent: Baiduspider
                Disallow: /php/*.html
                Disallow: /script/*
    21.控制nginx并发连接数,防止DDOS攻击。在location下配置limit_conn addr.
    22.nginx可设置反向代理,命令代码如下:
        proxy_connect_timeout 600;
        proxy_send_timeout 600;
        proxy_read_timeout 600;
        proxy_buffer_size 32k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 54k;
        proxy_temp_file_write_size 2m;
        proxy_ignore_client_abort on;
        proxy_cache_path /usr/local/nginx/cache_temp levels=2:2 keys_zone=cache_temp:128m inactive=30m 
        max_size=2g;
        proxy_cache_valid 200 302 10m;
    23.构架优化,随着业务的扩展,可能并发会越来越大,这时就需要考虑优化框架,区分上传下载。
    24.使用普通用户启动nginx服务,就是监牢模式,开发即可不通过root管理。
        防止权限扩大,因为nginx默认socket进程为root,不安全。通过指定conf文件,实现多实例。
        /usr/local/nginx/sbin/nginx -c /home/yunjisuan/conf/nginx.conf &>/dev/null &
        如果不定向到空会显示一些提示,不是错误,可以通过&>/dev/null 定向到空,从而忽略不见
    25.控制客户端请求nginx的速率,主要在http段内添加如下代码:
            limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; 
            # 以请求的客户端 IP 作为 key 值,内存区域命令为 one ,分配 10m 内存空间,访问速

}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值