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
worker_processes ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
10 include www_date/brian.conf;
11 include www_date/brianzjz.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

  检查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 ;
server_name www.brian.com brian.com; # 后面加上了brian.com 用空格隔开
location / {
root html/brian;
index index.html index.htm;
}
error_page /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.
built by gcc 4.8. (Red Hat 4.8.-) (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 ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
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已经处理完正在等待下一个连接

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

Nginx的常用功能的更多相关文章

  1. 前端开发掌握nginx常用功能之rewrite

    上一篇博文对nginx最常用功能的server及location的匹配规则进行了讲解,这也是nginx实现控制访问和反向代理的基础.掌握请求的匹配规则算是对nginx有了入门,但是这些往往还是不能满足 ...

  2. nginx介绍及常用功能

    什么是nginx nginx跟Apache一样,是一个web服务器(网站服务器),通过HTTP协议提供各种网络服务. Apache:重量级的,不支持高并发的服务器.在Apache上运行数以万计的并发访 ...

  3. Nginx超详细常用功能演示,够用啦~~~

    前言 Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服 ...

  4. 3.Nginx常用功能介绍

    Nginx常用功能介绍 Nginx反向代理应用实例 反向代理(Reverse Proxy)方式是指通过代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器,并且从内部网络服 ...

  5. Nginx常用功能

    3.Nginx常用功能 3.1 反向代理服务器 3.1.1.demo2 a.我在tomcat下部署了一个javaweb项目,tomcat安装的服务器IP为:192.168.37.136,部署的项目在t ...

  6. Nginx常用功能配置二

    Nginx常用功能配置二 Nginx location匹配设置 location作用:可以根据用户请求的URI来执行不同的应用,根据用户请求的网站的地址URL匹配. location语法: locat ...

  7. Nginx常用功能配置一

    Nginx常用功能配置 参数include配置 说明:如果日常工作中server标签存在太多,可以采用include配置模式,Nginx的主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目 ...

  8. nginx常用功能和配置

    nginx常用功能和配置 1.nginx常用功能和配置 1.1 限流 1.2 压力测试工具--Ab 1.2.1安装 1.2.2 测试 1.2.3 返回值 1.3 limit_conn_zone 1.4 ...

  9. Nginx实战部署常用功能演示(超详细版),绝对给力~~~

    前言 上次分享了一些开发过程中常用的功能,但如果到真实环境中,其实还需要一些额外的配置,比如说跨域.缓存.配置SSL证书.高可用等,老规矩,还是挑几个平时比较常用的进行演示分享.上篇详见Nginx超详 ...

随机推荐

  1. POJ 2593

    #include <iostream> #include <stdio.h> using namespace std; int cmp ( const void *a , co ...

  2. 课程一(Neural Networks and Deep Learning),第三周(Shallow neural networks)—— 0、学习目标

    Learn to build a neural network with one hidden layer, using forward propagation and backpropagation ...

  3. 剑指offer十二之数值的整数次方

    一.题目 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 二.思路 1.传统方法计算,时间复杂度O(n) 2.递归方式计算,时间复杂度O ...

  4. 【JAVA】内部类,内部接口

    内部类: 内部类可以很好的实现隐藏,一般的非内部类,是不允许有 private 与protected权限的,但内部类可以 内部类拥有外围类的所有元素的访问权限 可是实现多重继承 可以避免修改接口而实现 ...

  5. 利用cygwin创建windows下的crontab定时任务

    要求 必备知识 熟悉基本编程环境搭建. 运行环境 windows 7(64位); Cygwin-1.7.35 下载地址 环境下载 什么是Cygwin Cygwin是一个在windows平台上运行的类U ...

  6. Yum安装Zabbix4.2.0

    目录 1. 下载所需的存储库 2. 安装zabbix 3. 安装mysql 4. 配置数据库 5. 基本配置 6. zabbix配置文件 7. 进入web安装zabbix 1. 下载所需的存储库 # ...

  7. C++关于sort和priority_queue的运算符重载

    C++中的sort函数默认是将元素升序排列的,而priority_queue默认是将元素降序排列的(默认实现的是大顶堆). 自定义运算符用的比较多,以下2种对sort和priority_queue运算 ...

  8. SpringBoot入门 (十三) WebSocket使用

    本文记录在SpringBoot中使用WebSocket. 一 什么是WebSocket WebSocket是基于TCP协议的一种网络协议,它实现了浏览器与服务器全双工通信,支持客户端和服务端之间相互发 ...

  9. 请读下面的这句绕口令:ResourceManager中的Resource Estimator框架介绍与算法剖析

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由宋超发表于云+社区专栏 本文首先介绍了Hadoop中的ResourceManager中的estimator service的框架与运行 ...

  10. 安装win8/win10提示无法在驱动器0分区上安装windows解决方法

    在通过U盘或光盘安装win8/win8.1/win10系统时,不少用户遇到无法安装的问题,提示“无法在驱动器0的分区1上安装windows”,格式化分区1也不能解决,进而提示Windows无法安装到这 ...