码迷,mamicode.com
首页 > 其他好文 > 详细

nginx重点优化合集一

时间:2016-05-30 16:04:36      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:优化   nginx   

nginx特色

  • 支持高并发

  • 资源消耗少

  • 负载均衡

  • 缓存功能

  • 支持异步网络


nginx虚拟主机

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    include  extra/www.conf;

    include  extra/blog.conf;

    include  extra/status.conf;

    }

vim     ../conf/extra/www.conf              

server {

        listen       80;

        server_name  www.hequan.com   hequan.com;

        location / {

            root   html/www;

            index  index.html index.htm;

        }

}

server {

        listen       80;

        server_name  status.hequan.com;

        location / {

        stub_status  on;     //打开状态信息开关

        access_log  off;

        }

}


nginx日志

     错误日志      //官方文档 http://nginx.org/en/docs/ngx_core_module.html#error_log

nginx重点优化合集

error_log    logs/error.log     error;  //关键字    日志文件    错误日志级别:一般用warn|error|crit   crit为记录最少错误信息

error_log   /dev/null   crit;    //关闭日志信息记录

     访问日志               //http://nginx.org/en/docs/http/ngx_http_log_module.html

log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

                  ‘$status  $body_bytes_sent  "$http_referer" ‘

                  ‘"$http_user_agent" "$http_x_forwarded_for"‘;

access_log logs/access.log main  gzip  buffer=32k   flush=5s;          //放在http,server,localtion,if in localtion,limit_except


     切割日志    

             脚本切割

vim  cut_nginx_log.sh          //把nginx日志改名为带日期格式的新文件,然后平滑重启,生成新的日志

#!/bin/sh

Dateformat=`date +%Y%m%d`

Basedir="/application/nginx"

Nginxlogdir="$Basedir/logs"

Logname="access"

[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1

[ -f ${Logname}.log ] || exit 1

/bin/mv ${Logname}.log ${Logname}_${Dateformat}.log

$Basedir/sbin/nginx  -s reload



cat >> /var/spool/cron/root <<EOF                 

#cut  nginx   access log by hequan

00 00 * * *  /bin/sh  /hequan/sh/cut_nginx_log.sh >/dev/null  2>&1

EOF


补充

#删除7天前的日志

find . -mtime +7 -name "20[1-9][1-9]*" | xargs rm -f 


开启nginx目录文件列表显示功能


正常访问nginx的话是显示nginx欢迎页,也就是/nginx/html/index.html文件;

如果要显示/html/目录下所有的文件,需要打开目录文件列表显示;

在nginx.conf主配置文件中http或location代码段中,配置一段代码即可实现;

http {

    include       mime.types;

    default_type  application/octet-stream;

    autoindex on;

    autoindex_exact_size off;

    autoindex_localtime on;

autoindex on;    自动显示目录

autoindex_exact_size off; 

默认为on,显示出文件的确切大小,单位是bytes。

改为off后,显示出文件的大概大小,单位是kB或者MB或者GB

autoindex_localtime on;

默认为off,显示的文件时间为GMT时间。

改为on后,显示的文件时间为文件的服务器时间


location   匹配标识    匹配的网站网址     配置URI后要执行的配置段

~ 区分大小写    ~*不区分大小写    ^~ 进行常规匹配后,不做正则表达式检查

location  ^~   /images/ {

     return  404;

}

location  ~*  \.(gif|jpg|jpep)$  {    //正则匹配

      return 500;

}

location = / {} //精确匹配

location  /documents/ {}  //匹配常规字符串  有正则,先匹配正则

location / {}   所有lcoation都不能匹配后的默认匹配


nginx  rewrite    实现url地址重写

server    hequan.com;

rewrite   ^/(.*)     http://www.hequan.com/$1   permanent;                 //$1=(.*)    

permanent是永久301重定向标记      last匹配后,继续向下匹配           break配置完及终止              redirect返回302临时重定向


nginx访问日志

location  /{

     auth_basic                   "提示字符";

     auth_basic_user_file         conf/htpasswd;

}                     //可用Apache的htpasswd生成密码文件

yum -y install httpd-tools

htpasswd   -c  -m /application/nginx/conf/htpasswd  hequan


LNMP

nginx                                          PHP5                                                           mysql

     fastcgi_passFastCGI→   mysql_connet()      → connect DBMS→       

                                                  mysql_select_db()   → connect Database→   data                               

     



 


本文出自 “何全” 博客,请务必保留此出处http://hequan.blog.51cto.com/5701886/1784366

nginx重点优化合集一

标签:优化   nginx   

原文地址:http://hequan.blog.51cto.com/5701886/1784366

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!