Nginx配置段

  1. #user nobody;
  2. worker_processes 1;// 有1个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU,一般设置为 CPU数*核数
  3.  
  4. #error_log logs/error.log;
  5. #error_log logs/error.log notice;
  6. #error_log logs/error.log info;
  7.  
  8. #pid logs/nginx.pid;
  9.  
  10.  
  11. events {// 一般是配置nginx连接的特性 如1个word能同时允许多少连接
  12. worker_connections 1024;// 这是指 一个子进程最大允许连1024个连接
  13. }
  14.  
  15.  
  16. http {//这是配置http服务器的主要段
  17. include mime.types;
  18. default_type application/octet-stream;
  19.  
  20. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  21. # '$status $body_bytes_sent "$http_referer" '
  22. # '"$http_user_agent" "$http_x_forwarded_for"';
  23.  
  24. #access_log logs/access.log main;
  25.  
  26. sendfile on;
  27. #tcp_nopush on;
  28.  
  29. #keepalive_timeout 0;
  30. keepalive_timeout 65;
  31.  
  32. #gzip on;
  33.  
  34. server {// 这是虚拟主机段
  35. listen 80;
  36. server_name localhost;
  37.  
  38. #charset koi8-r;
  39.  
  40. #access_log logs/host.access.log main;
  41.  
  42. location / {//定位,把特殊的路径或文件再次定位 ,如image目录单独处理
  43. root html;
  44. index index.html index.htm;
  45. }
  46.  
  47. #error_page 404 /404.html;
  48.  
  49. # redirect server error pages to the static page /50x.html
  50. #
  51. error_page 500 502 503 504 /50x.html;
  52. location = /50x.html {
  53. root html;
  54. }
  55.  
  56. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  57. #
  58. #location ~ \.php$ {
  59. # proxy_pass http://127.0.0.1;
  60. #}
  61.  
  62. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  63. #
  64. #location ~ \.php$ {
  65. # root html;
  66. # fastcgi_pass 127.0.0.1:9000;
  67. # fastcgi_index index.php;
  68. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  69. # include fastcgi_params;
  70. #}
  71.  
  72. # deny access to .htaccess files, if Apache's document root
  73. # concurs with nginx's one
  74. #
  75. #location ~ /\.ht {
  76. # deny all;
  77. #}
  78. }
  79.  
  80.  
  81. # another virtual host using mix of IP-, name-, and port-based configuration
  82. #
  83. #server {
  84. # listen 8000;
  85. # listen somename:8080;
  86. # server_name somename alias another.alias;
  87.  
  88. # location / {
  89. # root html;
  90. # index index.html index.htm;
  91. # }
  92. #}
  93.  
  94.  
  95. # HTTPS server
  96. #
  97. #server {
  98. # listen 443 ssl;
  99. # server_name localhost;
  100.  
  101. # ssl_certificate cert.pem;
  102. # ssl_certificate_key cert.key;
  103.  
  104. # ssl_session_cache shared:SSL:1m;
  105. # ssl_session_timeout 5m;
  106.  
  107. # ssl_ciphers HIGH:!aNULL:!MD5;
  108. # ssl_prefer_server_ciphers on;
  109.  
  110. # location / {
  111. # root html;
  112. # index index.html index.htm;
  113. # }
  114. #}
  115.  
  116. }
  1. server {
  2. listen 80; #监听端口
  3. server_name sang.com; #监听域名
  4.  
  5. location / {
  6. root sang.com; #根目录定位
  7. index index.html;
  8. }
  9. }
  1. server {
  2. listen 8080;
  3. server_name sang.com;
  4.  
  5. location / {
  6. root /var/sang/html;
  7. index index.html;
  8. }
  9. }
  1. server {
  2. listen 80;
  3. server_name 192.168.1.200;
  4.  
  5. location / {
  6. root html/ip;#ip目录
  7. index index.html;
  8. }
  9. }

我们观察nginxserver,可以看到如下类似信息

 #access_log  logs/host.access.log  main;

这说明 server, 它的访问日志的文件是  logs/host.access.log ,

使用的格式”main”格式.

除了main格式,你可以自定义其他格式.

main格式是什么?

log_format  main  ‘$remote_addr – $remote_user [$time_local] “$request” ‘

    #                  ‘$status $body_bytes_sent “$http_referer” ‘

    #                  ‘”$http_user_agent” “$http_x_forwarded_for”‘;

main格式是我们定义好一种日志的格式,并起个名字,便于引用.

以上面的例子, main类型的日志,记录的 remote_addr…. http_x_forwarded_for等选项.

 

 

默认的日志格式: main

 

     log_format  main  ‘$remote_addr – $remote_user [$time_local] “$request” ‘

 

                            ‘$status $body_bytes_sent “$http_referer” ‘

 

                            ‘”$http_user_agent” “$http_x_forwarded_for”‘;

 

 

 

如默认的main日志格式,记录这么几项

 

远程IP- 远程用户/用户时间 请求方法(GET/POST) 请求体body长度 referer来源信息

 

http-user-agent用户代理/蜘蛛 ,被转发的请求的原始IP

 

http_x_forwarded_for:在经过代理时,代理把你的本来IP加在此头信息中,传输你的原始IP

 

 

    log_format  mylog ‘$remote_addr- “$request” ‘

 

                     ‘$status $body_bytes_sent “$http_referer” ‘

 

                        ‘”$http_user_agent” “$http_x_forwarded_for”‘;

 

在下面的server/location,我们就可以引用 mylog

 

server段中,这样来声明

 

Nginx允许针对不同的server做不同的Log ,(有的web服务器不支持,lighttp)

 

access_log logs/access_8080.log mylog;   

 

声明log   log位置          log格式;

 

shell+定时任务+nginx信号管理,完成日志按日期存储

分析思路:

凌晨00:00:01,把昨天的日志重命名,放在相应的目录下

USR1信息号控制nginx重新生成新的日志文件

 

具体脚本:

 

  1. #!/bin/bash
  2.  
  3.  
  4. base_path='/usr/local/nginx/logs'
  5.  
  6.  
  7. log_path=$(date -d yesterday +"%Y%m")
  8.  
  9.  
  10. day=$(date -d yesterday +"%d")
  11.  
  12.  
  13. mkdir -p $base_path/$log_path
  14.  
  15.  
  16. mv $base_path/access.log $base_path/$log_path/access_$day.log
  17.  
  18.  
  19. #echo $base_path/$log_path/access_$day.log
  20.  
  21.  
  22. kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

 

定时任务

 

Crontab 编辑定时任务

 

01 00 * * * /xxx/path/b.sh  每天01(建议在02-04点之间,系统负载小)

 

版权声明:本文为dream-to-pku原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/dream-to-pku/p/8687690.html