您的位置:首页 > 运维架构 > Nginx

nginx重点优化合集一

2016-05-30 10:04 423 查看
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或者GBautoindex_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_pass→FastCGI←→ mysql_connet() → connect DBMS→
mysql_select_db() → connect Database→ data
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  优化 nginx