nginx服务一常用功能与配置

1.nginx301重定向跳转

[root@nginx nginx]# vim conf/nginx.conf
 10     server {
 11         listen       80;
 12         server_name  liang.com;
 13         rewrite ^/(.*) http://www.liang.com/$1 permanent;
 14     }
[root@nginx nginx]# nginx -t               
nginx: the configuration file /usr/local/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test is successful
[root@nginx nginx]# nginx -s reload

2.nginx防止恶意解析

[root@nginx nginx]# vim conf/nginx.conf
 10     server {
 11         listen       80;
 12         location / {
 13                 deny all;
 14         }
 15     }
[root@nginx nginx]# nginx -t
nginx: the configuration file /usr/local/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test is successful
[root@nginx nginx]# nginx -s reload

3.nginx日志配置与切割

3.1日志格式配置

nginx官网:关于日志模块的帮助

[root@nginx nginx]# vim conf/nginx.conf
 10     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 11                       '$status $body_bytes_sent "$http_referer" '
 12                       '"$http_user_agent" "$http_x_forwarded_for"';
 24     server {
 25         listen       80;
 26         server_name  www.liang.com;
 27         location / {
 28             root   html/www;
 29             index  index.html index.htm;
 30             access_log logs/access_www_log main;
 31         }
 32     }
 [root@nginx nginx]# nginx -t

nginx: the configuration file /usr/local/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test is successful
[root@nginx nginx]# nginx -s reload
[root@nginx nginx]# ll logs/access_www_log    
-rw-r--r--. 1 root root 569 Aug  5 12:35 logs/access_www_log

3.2按天切割日志

[root@nginx ~]# cat /server/scritp/ngins_log.sh 
#!/bin/bash
Dir=/application/nginx/logs
File=access_www_log
cd ${Dir}
[ -f $File ]&&{
mv $File $(date +%F -d -1day)_$File
/application/nginx/sbin/nginx -s reload
}
[root@nginx ~]# crontab -l
#cut nginx access log
00 00 * * * /bin/bash /server/scritp/ngins_log.sh >/dev/null 2>&1

猜你喜欢

转载自blog.csdn.net/liang_operations/article/details/81432813