集群架构02·Nginx进阶·服务优化配置管理

企业场景常用的Nginx http功能模块汇总

ngx_ http_ core_ module

包括-些核心的http 参数配置,对应Nginx的配置为HTTP区块部分

ngx_ http _access_ module

访问控制模块,用来控制网站用户对Nginx的访问

ngx_ http_ gzip_ module

压缩模块,对Nginx返回的数据压缩,属于性能优化模块

ngx_ http_fastcgi_ module

FastCGI模块,和动态应用相关的模块,例如PHP

ngx_ http_ proxy_ module

proxy代理模块

ngx_ http_upstream_ module

负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查

ngx_ http_ rewrite_module

URL地址重写模块

ngx_ http_ limit_conn_module

限制用户并发连接数及请求数模块

ngx_ http_ limit req module

根据定义的key限制Nginx请求过程的速率

ngx_ http_ log_ module

访问8志模块,以指定的格式记录Nginx客户访问8志等信息

ngx_ http_ auth_basic_module

Web认证模块,设置Web用户通过账号、密码访问Nginx

ngx_ http_ ssl_ module

ssI模块,用于加密的http连接,如httpts

ngx_ http _stub_ status_ module

记录Nginx基本访问状态信息等的模块

核心配置文件 nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

nginx中一个server就是一个虚拟主机,分为域名,端口,ip三种类型的虚拟主机

基于域名的虚拟主机实战

1、配置基于域名的nginx.conf

#diff nginx.conf nginx.conf.default  #有些重要文件它本身就有备份文件
#egrep -v "^$|^#|..#" nginx.conf>nginx.conf2/egrep -v "^$|#" nginx.conf>nginx.conf2
 server {
        listen       80;
        server_name  www.ram.shop;
        location / {
            root   html/www;
            index  index.html index.htm;
        }

2、创建域名对应的站点目录和文件

[root@moban conf]# mkdir ../html/www -p #在html目录下新建www文件夹和server中root html/www对应

[root@moban conf]# echo "http://www.ram.shop" >../html/www/index.html

[root@moban conf]# cat ../html/www/index.html

http://www.ram.shop

3、检查语法,并重新加载nginx文件

[root@moban conf]#../sbin/nginx -t

nginx: the configuration file /application/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx/conf/nginx.conf test is successful

[root@moban conf]# ../sbin/nginx -s reload

[root@moban conf]# ps -ef |grep nginx    

root      8078  1978  0 05:59 pts/0    00:00:00 grep nginx

[root@moban conf]# echo "192.168.2.60 www.ram.shop" >>/etc/hosts

[root@moban conf]# tail -1 /etc/hosts

192.168.2.60 www.ram.shop

客户端hosts文件及配置

C:WindowsSystem32driversetchosts  添加192.168.2.60   www.ram.shop

 

4、配置多个基于域名的虚拟主机

在nginx.conf的http区中添加一个server区,就是增加一个虚拟主机,修改域名和对应的文件夹,重新加载服务,在hosts文件中添加域名,在客户端hosts文件中添加dns配置,over

添加zm的server区

   server {

        listen       80;

        server_name  www.ram.com;

        location / {

            root   html/www;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

    server {

        listen       80;

        server_name  www.zm.com;

        location / {

            root   html/zm;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

检测一下文件格式是否正确

重新加载nginx服务  /sbin/nginx -s reload

添加hosts

浏览器测试

基于端口的虚拟主机配置实战

修改每个虚拟主机的监听端口

修改nginx.conf

    server {

        listen       81;

        server_name  www.ram.com;

        location / {

            root   html/www;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

    server {

        listen       82;

        server_name  www.zm.com;

检查文件格式,重启服务,查看端口

[root@moban nginx]# netstat -lntup |grep nginx

tcp        0      0 0.0.0.0:81                  0.0.0.0:*                   LISTEN      1666/nginx         

tcp        0      0 0.0.0.0:82                  0.0.0.0:*                   LISTEN      1666/nginx         

tcp        0      0 0.0.0.0:83                  0.0.0.0:*                   LISTEN      1666/nginx

 

测试

 

端口只有不和已有服务冲突,就可以随意改,原则上是大于1024,小于65535

基于IP地址的虚拟主机配置实战

操作也类似,都是修改nginx.conf文件,你得提前给主机添加几个地址,使用较少

用脚本检查网站是否存活

脚本内容

#!/bin/bash
# 检查网站是否存活,加入定时任务,每分钟检查3次
#by authors patrick
. /etc/init.d/functions
check(){
  if (( $a ==  "0" ));then
    action "This url:$b is alive!"  /bin/true
    exit 0
  else
    action  "This url:$b is not alive!" /bin/false
    exit 1
  fi
}
main(){
  b=$1
  wget -O /dev/null -q $1
  a=`echo $?`
  check
  exit 0
}
main $1

测试

nginx常用功能配置实战

优化nginx配置文件:主配置和各个虚拟主机配置文件分离

把nginx.conf中的server模块拿出来放到一个目录下,方便管理,在主配置文件中使用include file;来声明文件的去处就可以了

include写法

 

配置

#mkdir extra

#sed -n '11,22p' nginx.conf >extra/www.conf

#sed -n '23,34p' nginx.conf > extra/nginx.conf

#sed -n '35,46p' nginx.conf > extra/bbs.conf

#sed  -i '11,46d' nginx.conf

#sbin/nginx  -t

#sbin/nginx -s reload

nginx.conf文件配置

http {

    include       mime.types;

    include       extra/www.conf;

    include          extra/nginx.conf;

    include          extra/bbs.conf;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

}

或者使用sed -i '10 i include  extra/www.conf; include  extra/nginx.conf'  nginx.conf

[root@moban extra]# pwd

/application/nginx/conf/extra

[root@moban extra]# ls

bbs.conf  nginx.conf  www.conf

虚拟主机的别名配置

给虚拟主机设置出了主域名以外的一个或多个域名名字,实现用户访问多个域名对应同一个虚拟主机网站的功能

 

状态信息功能实战

 

ngx_ http _stub_ status_ module

记录Nginx基本访问状态信息等的模块

nginx version: nginx/1.17.4[root@moban conf]# ../sbin/nginx -V

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --user=nginx --group=nginx --prefix=/application/nginx --with-http_stub_status_module --with-http_ssl_module    <==有这个就对了

[root@moban conf]# cat extra/status.conf

##STATUS

server{

    listen 80;

    server_name status.ram.com;

    location / {

        stub_status on;

        access_log off;

    }

}

给主配置文件添加include信息

include    extra/status.conf;

给window添加hosts解析  status.ram.com

检查语法,重启服务

[root@moban conf]# ../sbin/nginx -t

root@moban conf]# ../sbin/nginx -s reload

测试:随着连接数的增加,会自动统计连接数

Active connections: 5   ==>正处理的活动链接数为5个

server accepts handled requests
 76 76 63

第一个server表示nginx从启动到现在处理了76个连接

第二个server表示nginx从启动到现在共计成功处理了76个连接,相等这说明没有丢失请求

第三个server表示nginx从启动到现在处理了63个请求

Reading: 0 Writing: 1 Waiting: 4

nginx读取客户端的header信息数

nginx返回给客户端的header信息数

等待需要处理的连接数

nginx增加错误日志配置

error_log是核心功能模块的参数

配置主配置文件,增加错误日志的位置就可以了

error_log  logs/error.log -->nginx.conf

 访问日志(access_log)配置在http标签内

ngx_ http_ log_ module

访问8志模块,以指定的格式记录Nginx客户访问8志等信息

主配置文件 添加log_format的主格式,可以从默认文件中找到复制出来

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"';

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       81;

        server_name  www.ram.com;

        location / {

            root   html/www;

            index  index.html index.htm;

        }

access_log logs/access_www.log main;

查看原来网站日志,访问一下,在查看日志文件,

[root@moban logs]# cat access_www.log

192.168.2.2 - - [10/Oct/2019:02:44:18 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "-"

[root@moban logs]# curl www.ram.com:81

http://www.ram.com

[root@moban logs]# cat access_www.log

192.168.2.2 - - [10/Oct/2019:02:44:18 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "-"

192.168.2.60 - - [10/Oct/2019:02:44:54 +0800] "GET / HTTP/1.1" 200 19 "-" "curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"

访问日志轮询切割

#!/bin/sh

Dateformat= `date +%Y%m%d`

Basedir=" /applicat ion/nginx"

Nginxlogdir=$Basedir/logs"

Logname=”access www ”

[ -d $Nginxlogdir ] && cd $Nginxlogdir llexit 1

[ -f ${Logname} .1og ]|| lexit 1

/bin/mv  ${Logname).1og ${Dateformat)_${Logname}.1og

$Basedir/sbin/nginx -S reload

原文地址:https://www.cnblogs.com/unicornam/p/11645768.html