2.nginx.conf配置文件

所在位置:/nginx/conf nginx.conf

#user  nobody; #指定用户
#全局块
#值越大,可以支持的并发处理量也越多。指定进程数,一般是cpu核的2倍或和cpu核数一样
worker_processes  1;  

#日志记录
#错误日志存放目录
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#进程pid存放位置
#pid        logs/nginx.pid;

#event块:涉及的指令主要是影响nginx服务器与用户的网络连接
events {
	 use epoll;      #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
 	 worker_connections 1024;  #;单个后台worker process进程的最大并发链接数
}

#http块 包括http块和server块
http {
    include       mime.types;  #文件扩展名与类型映射表   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参数可以允许把httpresponse header和文件的开始放在一个文件里发布, 积极的作用是减少网络报文段的数量
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;  #开启压缩,传输数据更快,但是压缩会消耗服务器性能
	#反向代理负载均衡设定部分
#upstream表示负载服务器池,定义名字为backend_server的服务器池
	upstream backend_server {
	    server  10.254.244.20:81 weight=1 max_fails=2 fail_timeout=30s;
	    server  10.254.242.40:81 weight=1 max_fails=2 fail_timeout=30s;
	    server  10.254.245.19:81 weight=1 max_fails=2 fail_timeout=30s;
	    server  10.254.243.39:81 weight=1 max_fails=2 fail_timeout=30s;
	  }
  #基于域名的虚拟主机
 
    server {
        listen       80; #监听端口 
   		server_name  hczr.icu www.hczr.icu;   #ip或者备案后的域名,或者是localhost

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

         location /nacos {
            root   html;
	        proxy_pass http://backend_server ;
	    }

        #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;
    #    }
    #}

}

log详细说明

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

在这里插入图片描述

nginx log切割
日志切割
测试,按天切割:
可以在 conf 目录下vim logcut.sh:

#!/bin/bash
#设置日志文件存放目录
LOG_HOME=/usr/local/nginx/logs/
#备分文件记录时间名称
RECORD_TIME=$(date -d "yesterday" +%Y-%m-%d)
#重命名日志文件,mv 会删除原文件,导致日志丢失,因为Nginx不会自己去再创建一个日志文件
mv ${LOG_HOME}/access.log ${LOG_HOME}/access.${RECORD_TIME}.log
mv ${LOG_HOME}/error.log ${LOG_HOME}/error.${RECORD_TIME}.log
#向 Nginx 主进程发送 USR1 信号。Nginx 里 USR1 信号是重新打开日志文件
kill -USR1 `cat ${LOG_HOME}/nginx.pid`

为 logcut.sh脚本设置权限

chmod 777  logcut.sh

linux中创建定时器

crontab -e
# 此时会进入 vi 的编辑画面让您编辑工作!注意到,每项工作都是一行。

在定时配置中添加

0 0 * * *  /usr/local/nginx/conf/logcut.sh
#0 0 * * * 明天0点执行
#分0-59 时0-23 几号1-31 月0-11 星期几0-7

说明:
cron是一个linux下 的定时执行工具,可以在无需人工干预的情况下运行作业。

  systemctl start crond    //启动服务
  systemctl stop crond     //关闭服务
  systemctl restart crond  //重启服务
  systemctl reload crond   //重新载入配置
  systemctl status crond   //查看服务状态 

   crontab -l  //查看root用户的crontab任务
   crontab -r  //删除root用户所有crontab任务
   crontab -u  //使用者名称

补充: nginx 静态文件配置
例如在 /home/picture 和 /home/video 目录分别传入 图片 1.JPG 和 视频 milk.mp4

在nginx.conf 里配置

server {
        listen   520;
        server_name  localhost;
       # 通过 http://服务器:520/1.JPG   访问图片
        location / { 
            root   /home/picture;
        }
          # 通过 http://服务器:520/static/1.JPG   访问图片
        location /static {
            alias /home/picture; # alias 定义别名,隐藏真实服务器地址信息
        }   
        # 通过 http://服务器:520/video/milk.mp4   访问视频
        location /video {
            alias /home/video;  # alias 定义别名,隐藏真实服务器地址信息
        }
}

在nginx sbin目录下执行

./nginx -t  #查看配置文件是否正确
./nginx -s reload #重启nginx

说明:
路由 location配置
4.1.1 路由配置含义

# 路由匹配前缀内容
=:精确匹配
^~:精确前缀匹配
~:区分大小写的正则匹配
~*:不区分大小写的正则匹配
/uri:普通前缀匹配
/:通用匹配

4.1.2 规则路由与请求uri

# 规则设置
location = / {
   echo "规则A";
}
location = /login {
   echo "规则B";
}
location ^~ /static/ {
   echo "规则C";
}
location ^~ /static/files {
    echo "规则X";
}
location ~ \.(gif|jpg|png|js|css)$ {
   echo "规则D";
}
location ~* \.png$ {
   echo "规则E";
}
location /img {
    echo "规则Y";
}
location / {
   echo "规则F";
}

uri配置下的请求路由

       请求uri                               匹配路由规则
http://localhost/                              规则A
http://localhost/login                         规则B
http://localhost/register                      规则F
http://localhost/static/a.html                 规则C
http://localhost/static/files/a.txt            规则X
http://localhost/a.png                         规则D
http://localhost/a.PNG                         规则E
http://localhost/img/a.gif                     规则D
http://localhost/img/a.tiff                    规则Y
server {
        listen       80;
        server_name file.download.cn;
        access_log /Users/xiaoming/Documents/nginxlog/down.log main;

     location /svg {
         #转到的Nginx文件目录
         alias /Users/xiaoming/Documents/nginxlog;
         #打开目录浏览功能
         autoindex on;
         #默认为on,显示出文件的确切大小,单位是bytes
         #显示出文件的大概大小,单位是kB或者MB或者GB
         autoindex_exact_size off;
         #默认为off,显示的文件时间为GMT时间。
         #改为on后,显示的文件时间为文件的服务器时间
         autoindex_localtime on;
         #让浏览器不保存临时文件
         add_header Cache-Control no-store;
         #避免中文乱码
         charset utf-8,gbk;
         #希望请求文件是下载而不是显示内容
         #add_header Content-Disposition attachment; 
        }
}

nginx 负载策略:
Nginx负载均衡是通过upstream模块来实现的,内置实现了三种负载策略:轮询,最少连接,IP Hash两种第三方策略(fair、url_hash)。

引用的博客1
引用的博客2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值