Nginx的常用功能

1、规范nginx的配置文件

  在企业中我们的虚拟主机可能会很多,配置文件的内容也会有很多,这时候我们就可以规范一下我们的配置文件,把每个虚拟主机按照网站的域名或者是功能取名,放到统一的文件夹中,当然我们的虚拟主机可能数量不是很多,那我们也可以把多个虚拟主机配置成一个单独的配置文件,只是和nginx.conf主配置文件分离,这样在架构上显的很规范,在我们配置或者是拍错的时候也会很明确很简单。

  这里我们使用的参数是include,语法就是:

include file | mask;

  它可以放在nginx配置中的任何位置,用法示例:

include       mime.types;
include       brian.conf;       # 单个文件
include       vhosts/*.conf;  # 包含vhosts目录下的所有conf后缀的文件

  我们来对我们之前的nginx配置文件做个规范优化,nginx配置源文件:

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  www.brian.com;
        location / {
            root   html/brian;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
    server {
        listen       80;
        server_name  www.brianzjz.com;
        location / {
            root   html/brianzjz;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
      }

   }
}

  把上面的配置文件按照虚拟主机来进行配置提取,新建一个统一的存储目录为www_date,把提取出来的内容存储到以:域名.conf 的文件中放到www_date的目录中

  新建www_date目录:

[root@Nginx conf]# pwd
/opt/nginx/conf
[root@Nginx conf]# mkdir www_date

  规范提取虚拟主机配置:(用sed按照行数来提取虚拟主机的配置文件)

[root@Nginx conf]# sed -n "10,21p" nginx.conf     # 提取一个虚拟主机的配置文件,如果显示的不一样可以修改行数
    server {
        listen       80;
        server_name  www.brian.com;
        location / {
            root   html/brian;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }
====================================================================================================

[root@Nginx conf]# sed -n "22,34p" nginx.conf     # 提取二个虚拟主机的配置文件,如果显示的不一样可以修改行数
    server {
        listen       80;
        server_name  www.brianzjz.com;
        location / {
            root   html/brianzjz;
            index  index.html index.htm;
        }
	error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
      }

   }

  使用下面的命令把提取出的配置放到我们指定的www_date目录中 文件名为 brian.conf  brianzjz.conf

[root@Nginx conf]# sed -n "10,21p" nginx.conf > www_date/brian.conf            # 第一个虚拟主机配置写到www_date下面的brian.conf文件中
[root@Nginx conf]# sed -n "22,34p" nginx.conf > www_date/brianzjz.conf         # 第二个虚拟主机配置写到www_date下面的brianzjz.conf文件中
[root@Nginx conf]# cat www_date/brian.conf 
    server {
        listen       80;
        server_name  www.brian.com;
        location / {
            root   html/brian;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }
[root@Nginx conf]# cat www_date/brianzjz.conf 
    server {
        listen       80;
        server_name  www.brianzjz.com;
        location / {
            root   html/brianzjz;
            index  index.html index.htm;
        }
	error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
      }

   }

  删除主配置文件nginx.conf 中的所有虚拟主机的配置(server区块)我们可以按照行号来进行删除(提前看好行号)

[root@Nginx conf]# sed -i "10,34d" nginx.conf    # 删除server区块文件
[root@Nginx conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
}

  把刚刚提取出来的虚拟主机的单独配置文件使用include方法导入到nginx.conf文件中(红色标记为插入内容)

[root@Nginx conf]# sed -i '10 i include www_date/brian.conf;\ninclude www_date/brianzjz.conf;' nginx.conf   # 使用sed的方式把提取出的配置文件插入到nginx.conf中
[root@Nginx conf]# cat -n nginx.conf
     1    worker_processes  1;
     2    events {
     3        worker_connections  1024;
     4    }
     5    http {
     6        include       mime.types;
     7        default_type  application/octet-stream;
     8        sendfile        on;
     9        keepalive_timeout  65;
    10    include www_date/brian.conf;
    11    include www_date/brianzjz.conf;
    12    }

 

  重新加载配置,并测试访问结果:

  在重启nginx服务之前,我们要先对语法进行检测:

[root@Nginx conf]# ../sbin/nginx -t
nginx: the configuration file /opt/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx//conf/nginx.conf test is successful

  平滑重启nginx服务(reload方法,不中断业务)

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

  检查nginx的重新加载情况:

[root@Nginx conf]# netstat -lntup | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      23305/nginx: master
[root@Nginx conf]# ps -ef | grep nginx
root      23305      1  0 06:48 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx
nginx     24379  23305  0 10:44 ?        00:00:00 nginx: worker process
root      24383  23911  0 10:44 pts/2    00:00:00 grep --color=auto nginx
[root@Nginx conf]# lsof -i :80
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   23305  root    6u  IPv4  47849      0t0  TCP *:http (LISTEN)
nginx   24379 nginx    6u  IPv4  47849      0t0  TCP *:http (LISTEN)

  windows浏览器测试:

 

 

2、虚拟主机的别名配置

  所谓的虚拟机别名,就是为虚拟主机设置除主域名之外的名字(一个或者多个),这样就可以实现用户访问多个域名对应同一个网站了

  下面我们就修改一下,以 上面的www.brian.com为例:因为我们上面吧配置文件提取成单独的配置文件了,所谓下面看到的都是单独的配置文件进行修改的

  源文件内容:

[root@Nginx www_date]# cat brian.conf 
    server {
        listen       80;
        server_name  www.brian.com;
        location / {
            root   html/brian;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }

  修改后的文件:(红色标记修改文件)

[root@Nginx www_date]# vim brian.conf 
[root@Nginx www_date]# cat brian.conf 
    server {
        listen       80;
        server_name  www.brian.com brian.com;    # 后面加上了brian.com 用空格隔开
        location / {
            root   html/brian;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }

  重新加载配置,并测试访问结果:

  在重启nginx服务之前,我们要先对语法进行检测:

[root@Nginx www_date]# ../../sbin/nginx -t
nginx: the configuration file /opt/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx//conf/nginx.conf test is successful

  平滑重启nginx服务(reload方法,不中断业务)

[root@Nginx www_date]# ../../sbin/nginx -s reload

  检查nginx的重新加载情况:

[root@Nginx www_date]# netstat -lntup | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      23305/nginx: master
[root@Nginx www_date]# ps -ef | grep nginx
root      23305      1  0 06:48 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx
nginx     24379  23305  0 10:44 ?        00:00:00 nginx: worker process
root      24383  23911  0 10:44 pts/2    00:00:00 grep --color=auto nginx
[root@Nginx www_date]# lsof -i :80
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   23305  root    6u  IPv4  47849      0t0  TCP *:http (LISTEN)
nginx   24379 nginx    6u  IPv4  47849      0t0  TCP *:http (LISTEN)

  windows浏览器测试:

 

3、nginx状态信息(主要对status模块进行讲解)

  Nginx服务软件中有一个叫ngx_http_stub_status_module的模块,此模块的主要功能就是记录nginx的基本访问信息,让使用者了解nginx的工作状态,包括连接数等。。。。。在编译nginx的时候必须增加这个模块来支持

  可以通过下面的命令,来查看是否编译的时候添加上了此模块:

[root@Nginx conf]# ../sbin/nginx -V    # 命令
nginx version: nginx/1.6.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/opt/nginx/ --with-http_stub_status_module --with-http_ssl_module   # 查看到时编译的时候添加上了

 

  配置Nginx的status文件(这个文件也是个.conf结尾的文件,我们也是直接把这个文件放到我们之前建立好的www_date目录中)

[root@Nginx conf]# cat >>/opt/nginx/conf/www_date/status.conf<<EOF     # 通过EOF的方式直接写入文件
> ### status
> server {
>     listen    80;
>     server_name    status.brian.com;                                  # 域名
>     location / {
>         stub_status on;                                              # 打开状态信息开关
>         access_log  off;
>     }
> }
> EOF
[root@Nginx conf]# cat /opt/nginx/conf/www_date/status.conf          # 查看写入的文件
### status
server {
    listen    80;
    server_name    status.brian.com;
    location / {
        stub_status on;                                              
        access_log  off;
    }
}

  在nginx.conf主配置文件中使用include导入这个文件

[root@Nginx conf]# sed -i '12 i include www_date/status.conf;' nginx.conf    # sed 命令导入
[root@Nginx conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;                   # 所添加的内容
}

  重新加载配置,并测试访问结果:

  在重启nginx服务之前,我们要先对语法进行检测:

[root@Nginx conf]# ../sbin/nginx -t
nginx: the configuration file /opt/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx//conf/nginx.conf test is successful

  平滑重启nginx服务(reload方法,不中断业务)

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

  添加hosts文件:

windows添加hosts文件
路径:C:\Windows\System32\drivers\etc\hosts
打开上面路径的文件添加,下面内容 保存
192.168.1.108 status.brian.com

  windows浏览器测试:

Active connections: 3      # 正在处理的活动链接数
server accepts handled requests  # server表示:nginx启动到现在一共处理的连接  accepts表示:nginx启动到现在一共处理了多少次握手  handled requests表示:总共处理的连接数
 3 3 144            
Reading: 0 Writing: 1 Waiting: 2  # Reading表示:nginx读取到客户端的Header信息数   Writing表示:nginx返回给客户端的Header信息数  Waiting表示:nginx已经处理完正在等待下一个连接

  温馨提示:为了安全这些信息要防止外部用户看到

 

posted @ 2018-03-21 17:17  Brian_Zhu  阅读(1656)  评论(0编辑  收藏  举报