Nginx配置段

#user  nobody;
worker_processes 1;// 有1个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU,一般设置为 CPU数*核数 #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {// 一般是配置nginx连接的特性 如1个word能同时允许多少连接
worker_connections 1024;// 这是指 一个子进程最大允许连1024个连接
} http {//这是配置http服务器的主要段
include mime.types;
default_type application/octet-stream; #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; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {// 这是虚拟主机段
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {//定位,把特殊的路径或文件再次定位 ,如image目录单独处理
root html;
index index.html index.htm;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }

一、基于域名的配置

 server {
listen 80; #监听端口
server_name sang.com; #监听域名 location / {
root sang.com; #根目录定位
index index.html;
}
}

二、基于端口的配置

 server {
listen 8080;
server_name sang.com; location / {
root /var/sang/html;
index index.html;
}
}

三、基于IP的配置  

 server {
listen 80;
server_name 192.168.1.200; location / {
root html/ip;#ip目录
index index.html;
}
}

四、日志管理  

我们观察nginx的server段,可以看到如下类似信息

#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等选项.

1: 日志格式 是指记录哪些选项

默认的日志格式: 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

2: 声明一个独特的log_format并命名

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格式;

3、实际应用:

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

分析思路:

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

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

具体脚本:

#!/bin/bash

base_path='/usr/local/nginx/logs'

log_path=$(date -d yesterday +"%Y%m")

day=$(date -d yesterday +"%d")

mkdir -p $base_path/$log_path

mv $base_path/access.log $base_path/$log_path/access_$day.log

#echo $base_path/$log_path/access_$day.log

kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

定时任务

Crontab 编辑定时任务

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

【Nginx系列】Nginx虚拟主机的配置核日志管理的更多相关文章

  1. 各个nginx conf的虚拟主机的配置

    server { listen 80; server_name t-cl.orangevip.com; rewrite ^(.*)$ https://$host$1 permanent;} serve ...

  2. 第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置

    第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置 软件版本  uwsgi- ...

  3. Nginx教程--02.Nginx虚拟主机的配置

    1.Nginx虚拟主机的配置 1.1 在conf目录下,使用命令 : vim nginx.conf 对上图解释: //全局区 worker _processes 1; //表示当前有1个工作的子进程, ...

  4. 虚拟主机ip配置,nginx.conf文件配置及日志文件切割

    今天粗略整理了一下虚拟主机配置,nginx.conf文件的配置,及日志文件的切割,记录如下: nginx虚拟主机配置:1.IP地址配置,2.绑定ip地址和虚拟主机详情:1.ip地址的配置:ifconf ...

  5. Nginx 虚拟主机 VirtualHost 配置

    Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 无论是静态服务器还是小网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高. 我在& ...

  6. Nginx(二):虚拟主机配置

    什么是虚拟主机? 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的Intemet服务器功 ...

  7. nginx虚拟主机的配置

    nginx虚拟主机的配置 server { listen ; server_name 127.0.0.1; access_log off; root /var/www/html/; location ...

  8. Nginx网络架构实战学习笔记(一):Nginx简介、安装、信号控制、nginx虚拟主机配置、日志管理、location 语法、Rewrite语法详解

    文章目录 nginx简介 nginx安装 nginx信号控制 nginx虚拟主机配置 日志管理 location 语法 精准匹配的一般匹配 正则匹配 总结 Rewrite语法详解 nginx简介 Ng ...

  9. Nginx多站点虚拟主机实现单独启动停止php-fpm、单独控制权限设置

    Nginx多站点虚拟主机实现单独启动停止php-fpm.单独控制权限设置 来源:osyunwei.com 作者:qihang01 发表于:2012-08-19 21:26 点击: 说明: 站点1:bb ...

随机推荐

  1. jQuery遍历table中的tr td并获取td中的值

    jQuery遍历table中的tr td并获取td中的值 $(function(){ $("#tableId tr").find("td").each(func ...

  2. 下拉框的change事件

    6.1,获取下拉框的值(html标签中没有onchange事件的) <script language="javascript"> $(document).ready(f ...

  3. tp5怎么实现搜索分页能保留搜索条件

    $profit=Db::view('profit','settlement_time,money,balance_account,balance_account1,did,user') ->vi ...

  4. order by group by

    order by 后 group by连用, mysql好像 >5.4不起作用 通过 explain 查看执行计划,可以看到没有 limit 的时候,少了一个 DERIVED 操作 估计是内部优 ...

  5. 【BZOJ3884】上帝与集合的正确用法(欧拉定理,数论)

    [BZOJ3884]上帝与集合的正确用法(欧拉定理,数论) 题面 BZOJ 题解 我们有欧拉定理: 当\(b \perp p\)时 \[a^b≡a^{b\%\varphi(p)}\pmod p \] ...

  6. webrtc起步 - apprtc服务器搭建

    简介      apprtc 是什么,webrtc.org官方指定体验app     原料: ubuntu14.04,其他linux版本不限,官方并没特殊说明 chrome M51+ stunnle ...

  7. c++中使用xercesc对xml进行schema校验

    头文件 #pragma once #if !defined(AFX_A1CONTENTHANDLER_H__E0CFBC18_CCC1_42F3_B0A4_B03331AB9693__INCLUDED ...

  8. A+B for Input-Output Practice (VI)

    #include<iostream> using namespace std; void main() { int b,c,sum=0; while(scanf("%d" ...

  9. templet模式

    package template;import java.sql.Connection;import java.sql.ResultSet;/** * Created by marcopan on 1 ...

  10. Python3基础教程1——Python的环境搭建

    2018年3月8日 当然推荐一个比较系统的教程 http://www.runoob.com/python3/python3-tutorial.html 人家也写的也比我好啦 本教程为新手向的,请大佬跳 ...