【Web 集群实战】07_Nginx 常用功能配置

版权声明: https://blog.csdn.net/weixin_42061048/article/details/82771146

【Web 集群实战】07_Nginx 常用功能配置

标签(空格分隔): Web集群实战


1. 规范优化 Nginx 配置文件

  • Nginx 的主配置文件为 nginx.conf ,主配置文件包含的所有虚拟主机的子配置文件会统一放入 extra 目录中,虚拟主机的配置文件按照网站的域名或功能取名。

  • 使用的参数是 include,语法:

include file | mask;
  • 可以放在 Nginx 配置中的任何位置,用法示例:
include mime.type;
include www.conf;      // 包含单个文件
include vhosts/*.conf  // 包含 vhosts 下所有以 conf 结尾的文件
  • 具体步骤
[root@ylt001 conf]# mkdir extra
[root@ylt001 conf]# /bin/cp nginx.conf_BaseName nginx.conf
[root@ylt001 conf]# sed -n '10,17p' nginx.conf
    Server {
        listen  80;
        server_name www.yangyangyang.org;
        location / {
            root    html/www;
            index.html index.htm;
        }
    }
[root@ylt001 conf]# sed -n '10,17p' nginx.conf >extra/www.conf
[root@ylt001 conf]# sed -n '18,25p' nginx.conf
    Server {
        listen  80;
        server_name bbs.yangyangyang.org;
        location / {
            root    html/bbs;
            index.html index.htm;
        }
    }
[root@ylt001 conf]# sed -n '18,25p' nginx.conf >extra/bbs.conf   
[root@ylt001 conf]# sed -n '26,33p' nginx.conf
    Server {
        listen  80;
        server_name blog.yangyangyang.org;
        location / {
            root    html/blog;
            index.html index.htm;
        }
    }
[root@ylt001 conf]# sed -n '26,33p' nginx.conf >extra/blog.conf
  • 删除主配置文件 nginx.conf 中所有虚拟主机的配置(需要提前查好行号,确认无误再删除0
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include         mime.types;
    default_type    application/octet-stream;
    sendfile        on;
    keepalived_timeout 65;
}
  • 插入虚拟主机的配置
[root@ylt001 conf]# sed -i '10 include extra/www.conf; \ninclude extra/bbs.conf; \ninclude extra/blog.conf;' nginx.conf
  • 插入后主配置文件如下:
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include         mime.types;
    default_type    application/octet-stream;
    sendfile        on;
    keepalived_timeout 65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
  • 重新加载配置,并测试更改后的效果
[root@ylt001 conf]# ../sbin/nginx -t
[root@ylt001 conf]# ../sbin/nginx -s reload
[root@ylt001 conf]# tail -1 /etc/hosts
[root@ylt001 conf]# curl www.yangyangyang.org
[root@ylt001 conf]# curl blog.yangyangyang.org
[root@ylt001 conf]# curl bbs.yangyangyang.org

2. Nginx 虚拟主机的别名配置

  • 修改主配置文件 extra/www.conf
    Server {
        listen  80;
        server_name www.yangyangyang.org  yangyangyang.org;
        location / {
            root    html/www;
            index.html index.htm;
        }
    }
  • 重新加载配置
[root@ylt001 conf]# ../sbin/nginx -t
[root@ylt001 conf]# ../sbin/nginx -s reload
  • 配置域名解析
[root@ylt001 conf]# vi /etc/hosts
[root@ylt001 conf]# tail -1 /etc/hosts
[root@ylt001 conf]# curl www.yangyangyang.org
[root@ylt001 conf]# curl yangyangyang.org

3. Nginx 状态信息功能

Nginx status 介绍

  • 记录 Nginx 的基本访问信息

  • 检查编译安装 Nginx 时是否设定了 http_stub_status_module 模块

[root@ylt001 conf]# ../sbin/nginx -V

配置 Nginx status

  • 生成状态配置,并增加状态配置参数
[root@ylt001 conf]# cat >>/application/nginx/conf/extra/status.conf<<EOF
>##status
>server{
>   listen  80;
>   server_name status.yangyangyang.org;
>   location / {
>       stub_status on;
>       access_log  off;
>   }
>}
>EOF
[root@ylt001 conf]# cat extra/status.conf
[root@ylt001 conf]# sed -i '13 i include extra/status.conf;' nginx.conf
[root@ylt001 conf]# cat -n nginx.conf
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include         mime.types;
    default_type    application/octet-stream;
    sendfile        on;
    keepalived_timeout 65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/status.conf
}
  • 检查语法,并重启服务
[root@ylt001 conf]# ../sbin/nginx -t
[root@ylt001 conf]# ../sbin/nginx -s reload
  • 配置 hosts 解析(Windows 客户端)
  • 测试访问结果

4. 为 Nginx 增加错误日志(error_log)配置

Nginx 错误日志信息介绍

  • 配置记录 Nginx 的错误信息是调试 Nginx 服务的重要手段,属于核心功能模块(ngx_core_module)的参数,该参数名字为 error_log ,可以放在 Main 区块中全局配置,也可以放置不同的虚拟主机中单独记录。
  • error_log 的语法格式及参数语法说明如下:
error_log       file        level
关键字          日志文件    错误日志级别
  • 其中,关键字 error_log 不能改变,日志文件可以指定任意存放日志的目录,错误日志级别常见的有[debug|info|notice|warn|error|crit|alert|emerg],级别越高,记录的信息越少,生产场景一般是 warn|error|crit 这三个级别之一,注意不要配置 info 等较低级别,会带来巨大的磁盘 I/O 消耗。
  • error_log 的默认值为:
#default: error_log logs/error.log  error;
  • 可以放置的标签段为:
#context: main, http, server, location

参考资料:http://nginx.org/en/docs/ngx_core_module.html#error_log

Nginx 错误日志配置

  • 编辑主配置文件 nginx.conf,增加错误日志的配置方法如下:
worker_processes 1;
error_log   logs/error.log;
events {
    worker_connections 1024;
}
http {
    include         mime.types;
    default_type    application/octet-stream;
    sendfile        on;
    keepalived_timeout 65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/status.conf;
}

猜你喜欢

转载自blog.csdn.net/weixin_42061048/article/details/82771146