nginx学习1.2 nginx主要配置详解

环境:

centos6/7,nginx-1.9.15.

摘要说明:

上一篇我们介绍的nginx的基础概念,安装、目录及启停,链接

本篇主要讲述nginx的主要配置结构和各配置详解;

步骤:

1.配置文件整体结构:

首先我们基于nginx-1.9,15上一个默认nginx配置如下:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

}

其结构如下:

  • main(全局设置)
  • events设定nginx的工作模式及连接数上限
  • http 服务器相关属性
  • server(虚拟主机设置)
  • upstream(上游服务器设置,主要为反向代理、负载均衡相关配置) l
  • location(URL匹配特定位置后的设置)

2.详细配置:

下面这些配置是全局性配置

  • user :主模块命令, 指定Nginx的worker进程运行用户以及用户组,默认由nobody账号运行。       
  • worker_processes: 指定Nginx要开启的进程数,一般与服务cpu数目一致。
  • error log:用来定义全局错设日志文件的路径和日志名称。日志输出级别有debug,info,notice,warn,error,crit 可供选择,其中debug输出日志最为详细,面crit(严重)输出日志最少。默认是error
  • pid: 用来指定进程id的存储文件位置。
  • event:设定nginx的工作模式及连接数上限,其中参数use用来指定nginx的工作模式(这里是epoll,epoll是多路复用IO(I/O Multiplexing)中的一种方式),nginx支持的工作模式有select ,poll,kqueue,epoll,rtsig,/dev/poll。其中select和poll都是标准的工作模式,kqueue和epoll是高效的工作模式,对于linux系统,epoll是首选。
  • worker_connection是设置nginx每个进程最大的连接数,默认是1024,所以nginx最大的连接数max_client=worker_processes * worker_connections。进程最大连接数受到系统最大打开文件数的限制,需要设置ulimit。

下面这些是对对http服务器相关属性的配置详解:

#下面部分是nginx对http服务器相关属性的设置
http {
    include       mime.types;               主模块命令,对配置文件所包含文件的设定,减少主配置文件的复杂度,相当于把部分设置放在别的地方,然后在包含进来,保持主配置文件的简洁
    default_type  application/octet-stream; 默认文件类型,当文件类型未定义时候就使用这类设置的。

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '   指定nginx日志的格式
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
    sendfile        on;   开启高效文件传输模式(zero copy 方式),避免内核缓冲区数据和用户缓冲区数据之间的拷贝。
    #tcp_nopush     on;  开启TCP_NOPUSH套接字(sendfile开启时有用)

    #keepalive_timeout  0;   客户端连接超时时间
    keepalive_timeout  65;

    #gzip  on;             设置是否开启gzip模块
#下面是server段虚拟主机的配置
server {
        listen       80;   虚拟主机的服务端口
        server_name  localhost;   用来指定ip或者域名,多个域名用逗号分开
        #charset koi8-r;
        location / {        
               #地址匹配设置,支持正则匹配,也支持条件匹配,这里是默认请求地址,用户可以location命令对nginx进行动态和静态网页过滤处理
            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;
        }     
}

3.日志配置

通过访问日志,你可以得到用户地域来源、跳转来源、使用终端、某个URL访问量等相关信息;通过错误日志,你可以得到系统某个服务或server的性能瓶颈等。因此,将日志好好利用,你可以得到很多有价值的信息。 

日志默认为logs/access.log;

我们可以通过配置上述的nginx.conf来配置日志格式;即http下的log_format,这个是默认配置,可随意配置;

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

$remote_addr

客户端的ip地址(代理服务器,显示代理服务ip)

$remote_user

用于记录远程客户端的用户名称(一般为“-”)

$time_local

用于记录访问时间和时区

$request

用于记录请求的url以及请求方法

$status

响应状态码,例如:200成功、404页面找不到等。

$body_bytes_sent

给客户端发送的文件主体内容字节数

$http_user_agent

用户所使用的代理(一般为浏览器)

$http_x_forwarded_for

可以记录客户端IP,通过代理服务器来记录客户端的ip地址

$http_referer

可以记录用户是从哪个链接访问过来的

4.日志定时切割

首先编写切割脚本logcut.sh如下:

#!/bin/bash
#设置日志文件存放目录
LOG_HOME="/usr/local/nginx/logs/"
#备分文件名称
LOG_PATH_BAK="$(date -d yesterday +%Y%m%d%H%M)"
#重命名日志文件
mv ${LOG_HOME}/access.log ${LOG_HOME}/access.${LOG_PATH_BAK}.log
mv ${LOG_HOME}/error.log ${LOG_HOME}/error.${LOG_PATH_BAK}.log
#向nginx主进程发信号重新打开日志
kill -USR1 `cat ${LOG_HOME}/nginx.pid`

添加执行权限:

chmod +x logcut.sh

接着我们可以手动执行下脚本:

./logcut.sh

接着使用crontab -e配置定时器配置每分钟执行切割进行测试,更多可参考corn表达式:

*/1 * * * * /usr/local/nginx/sbin/logcut.sh

当然我们要确认定时任务是开启的

/etc/rc.d/init.d/crond status
/etc/rc.d/init.d/crond start

定时任务的日志启动和查看:

/etc/init.d/rsyslog start
tail -f /var/log/cron

 上述都是基于centos6;下面centos7下直接使用命令:

systemctl start crond
systemctl stop crond
systemctl restart crond
systemctl reload crond
systemctl status crond.service
vi /etc/crond
crontab -l -u root #查看root用户任务
service rsyslog status
service rsyslog start

 

1. Nginx简介 1.1. 什么是nginx 1.2. Nginx的优点 1.3. 哪里使用到nginx 1.4. Nginx和Apache的区别 2. 安装Nginx服务器 2.1. 在windows上安装 2.2. 在Linux上安装 2.2.1. 写在前面 2.2.2. 准备使用yum安装nginx的运行环境 2.2.3. 安装pcre 2.2.4. 安装zlib库 2.2.5. 安装nginx 2.2.6. 控制nginx 2.2.7. nginx安装服务 3. Nginx配置文件详解 3.1. Nginx的主配置文件概述 3.1.1. 认识配置文件 3.1.2. nginx配置文件结构 3.1.3. nginx的全局配置 3.2. events配置 3.3. http的配置 3.4. nginx重要指令之location 4. nginx中的rewrite 4.1. 什么是rewrite 4.2. rewrite的命令的作用域和优先级 4.3. if指令 4.3.1. if指令的语法 4.3.2. if指令中使用的逻辑运算符 4.3.3. If指令中可以使用的变量 4.3.4. if指令实例 4.4. rewrite指令 4.4.1. rewrite指令语法 4.4.2. flag标记 4.4.3. set指令 4.4.4. return指令 4.4.5. rewrite实例 5. nginx的虚拟主机 5.1. 什么是nginx的虚拟主机 5.2. 标准的虚拟主机配置 5.3. 规划虚拟主机的配置文件 6. 动静分离 7. nginx的反向代理 7.1. 什么是反向代理 7.2. 明确两个概念 7.3. 特点 7.4. 反向代理的配置 7.5. 可以将代理配置单独放在一个配置文件中 8. nginx的负载均衡(自学) 8.1. 什么是负载均衡 8.2. 负载均衡的优点 8.3. 负载均衡的分配策略 8.4. 负载均衡配置 9. 安装PHP 10. PHP-FPM 10.1. 什么是PHP-FPM 10.2. 为什么要是使用PHP-FPM 10.3. 安装并且启动PHP-FPM 10.3.1. 安装 10.3.2. fpm的配置 10.3.3. 启动和停止 10.3.4. 自启动php-fpm 10.3.5. 检查php-fpm是否启动 10.4. nginx使用php-fpm处理php
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值