• 09 Nginx配置、多站点及日志模块


    nginx的使用

    选项

      -?,-h         : this help
      -v            : 版本信息
      -V            : 展示版本信息及配置模块
      -t            : 检查配置文件是否正确
      -T            : test configuration, dump it and exit
      -q            : suppress non-error messages during configuration testing
      -s signal     : 指定Nginx启动配置方式
      -p prefix     : 指定Nginx安装路径
      -e filename   : 指定错误日志路径
      -c filename   : 指定配置文件路径
      -g directives : 前台启动 -g "deamon off"
    

    yum安装的配置文件

    #/etc/nginx/nginx.conf
    
    -------------------- 全局配置(全局生效) -----------------
    user  www;	#启动nginx work进程的用户名
    worker_processes  auto;	#启动的worker进程数
    
    error_log  /var/log/nginx/error.log notice;	#错误日志路径
    pid        /var/run/nginx.pid;	#pid 文件路径
    
    -------------- 系统事件配置模块(全局生效) ---------------
    events {	#事件配置模块
        worker_connections  1024;	#最大连接数
        use epool;	#指定网络模型
    }
    
    -------------- Http请求模块(处理Http请求的模块)
    http {
        include       /etc/nginx/mime.types;	#Nginx可以处理的文件类型
        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  /var/log/nginx/access.log  main;	#访问日志
    
        sendfile        on;	#tcp连接配置
        #tcp_nopush     on;
    
        keepalive_timeout  65;	#长链接配置
    
        #gzip  on;	#压缩
    
        include /etc/nginx/conf.d/*.conf;	#包含其他文件
    }
    
    server {
        listen 80;	#监听的端口
        server_name 127.0.0.1;	#域名
    
        location / {
    	root /blog;	#指定的站点目录
    	index index.php;	#指定索引
    	}
    

    源码安装的在家目录的conf的文件夹中

    Nginx的站点

    web网站,nginx是支持多站点的服务的

    • Nginx基于域名代理多个站点

      #web01
      server {
          listen 80;	
          server_name www.web01.com;	
      
          location / {
      	root /web01;	
      	index index.html;	
      	}
      	
      #web02	
      server {
          listen 80;	
          server_name www.web01.com;	
      
          location / {
      	root /web01;	
      	index index.html;	
      	}
      	
      #线上域名
      #购买域名--->备案域名--->修改dns
      #域名解析
      A记录:将域名解析到服务器上
      
      CNAME:将一个域名解析到另一个域名上(域名转发)
      
    • 基于ip的多站点

      #web01
      server {
          listen 80;	
          server_name 192.168.15.8;	
      
          location / {
      	root /web01;	
      	index index.html;	
      	}
      	
      #web02
      server {
          listen 80;	
          server_name 192.168.15.9;	
      
          location / {
      	root /web01;	
      	index index.html;	
      	}
      
    • 基于端口的多站点

      #web01
      server {
          listen 80;	
          server_name www.web.com;	
      
          location / {
      	root /web01;	
      	index index.html;	
      	}
      	
      #web01
      server {
          listen 90;	
          server_name www.web.com;	
      
          location / {
      	root /web01;	
      	index index.html;	
      	}
      

    日志

    access_log  /var/log/nginx/access.log  main;
    
    access_log  	#日志的模块
    /var/log/nginx/access.log  	#日志的路径
    main;	#日志的格式
    

    日志的格式

    $remote_addr        # 记录客户端IP地址
    $remote_user        # 记录客户端用户名
    $time_local         # 记录通用的本地时间
    $time_iso8601       # 记录ISO8601标准格式下的本地时间
    $request            # 记录请求的方法以及请求的http协议
    $status             # 记录请求状态码(用于定位错误信息)
    $body_bytes_sent    # 发送给客户端的资源字节数,不包括响应头的大小
    $bytes_sent         # 发送给客户端的总字节数
    $msec               # 日志写入时间。单位为秒,精度是毫秒。
    $http_referer       # 记录从哪个页面链接访问过来的
    $http_user_agent    # 记录客户端浏览器相关信息
    $http_x_forwarded_for #记录经过的所有服务器的IP地址
    $X-Real-IP		   #记录起始的客户端IP地址和上一层客户端的IP地址
    $request_length     # 请求的长度(包括请求行, 请求头和请求正文)。
    $request_time       # 请求花费的时间,单位为秒,精度毫秒
    # 注:如果Nginx位于负载均衡器,nginx反向代理之后, web服务器无法直接获取到客 户端真实的IP地址。
    # $remote_addr获取的是反向代理的IP地址。 反向代理服务器在转发请求的http头信息中,
    # 增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址。
    

    json日志模板

    log_format access_json '{"@timestamp":"$time_iso8601",'
                               '"host":"$server_addr",'
                               '"clientip":"$remote_addr",'
                               '"size":$body_bytes_sent,'
                               '"responsetime":$request_time,'
                               '"upstreamtime":"$upstream_response_time",'
                               '"upstreamhost":"$upstream_addr",'
                               '"http_host":"$host",'
                               '"url":"$uri",'
                               '"domain":"$host",'
                               '"xff":"$http_x_forwarded_for",'
                               '"referer":"$http_referer",'
                               '"status":"$status"}';
    

    日志的切割

    logrotate(/etc/logrotate.d)

    [root@web03 local]# pwd
    /etc/logrotate.d/
    [root@web03 local]# cat nginx
    /var/log/nginx/*.log {
            daily				# 切割日志的时间				
            missingok			# 忽略错误
            rotate 52			# 日志最多存放52次
            compress			# 使用gzip压缩
            delaycompress		# 延时压缩
            notifempty			# 不处理空文件
            create 640 nginx adm# 定义日志的权限
            sharedscripts		# 开始执行脚本
            postrotate			# 标注脚本内容
                    if [ -f /var/run/nginx.pid ]; then
                            kill -USR1 `cat /var/run/nginx.pid`
                    fi
            endscript			# 结束
    }
    

  • 相关阅读:
    电影记录管理系统 修改与注释,完整代码
    Mybatis用法小结
    springMVC中传值的时候的乱码问题
    MAVEN安装过程
    树形结构的数据库表Schema设计
    SpringMVC的工作原理
    页面底部的回到顶部的按钮实现
    鼠标放上去,div高度随文字增加,并显示剩余的文字。
    freeMarker中list的两列展示
    html的textarea控制字数小案例
  • 原文地址:https://www.cnblogs.com/zhaokunhao/p/14706734.html
Copyright © 2020-2023  润新知