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. 详解使用flask_paginate进行分页

    分页技术好处: 1.分页技术是把数据全部查询出来,然后再进行分页 2.分页技术可以,降低带宽使用,提高访问速度 使用flask_paginate进行分页 1.要使用flask_paginate,首先安 ...

  2. [Umbraco] DocumentType设计指南

    1. 命名规则 1.1. 文档类型(DocumentType)命名规则 图 1. Document Type命名示例 名称(Name)   采用帕斯卡命名法 如:TextPage 别名(Alias)  ...

  3. Xamarin 绑定安卓第三方库恢复原始参数问题

    大家都知道在绑定xamarin android 第三方库的时候 参数名是乱码的 变成了p1  p2  p3 之类的 这样在实际使用的时候非常不方便. 其实xamarin是提供了三种方式帮助大家恢复ja ...

  4. echarts初探

    最近经常看到echarts,觉得很有意思,并且这个库是百度开发的,目前来说使用的也很广泛,包括百度.阿里.腾讯.网易.小米.新浪.华为.联想.美团等一大批一线互联网公司在使用,且github上的sta ...

  5. Android系统架构及启动流程

  6. RESTful API后台系统架构设计(Java)

    最近设计和实现了一个JAVA的RESTful API的后台业务系统架构,主要基于Java平台.设计要求是: 性能:平均响应时间(RESTful API)小于2s(平均负载的情况下),并发访问200个以 ...

  7. Linux-(ping,traceroute,ss)

    ping命令 1.命令格式: ping [参数] [主机名或IP地址] 2.命令功能: ping命令用于:确定网络和各外部主机的状态:跟踪和隔离硬件和软件问题:测试.评估和管理网络.如果主机正在运行并 ...

  8. 实现比较器接口IComparable<T>,让自定义类型数组也能排序

    using System; namespace LinqDemo1 { class Program { static void Main(string[] args) { Person[] perso ...

  9. 线上问题定位--CPU100%

    服务器CPU突然告警,如何定位是哪个服务进程导致CPU过载,哪个线程导致CPU过载,哪段代码导致CPU过载? 步骤一.找到最耗CPU的进程 工具:top 方法: 执行top -d 1 -c,每秒刷新一 ...

  10. elasticSearch6源码分析(6)http和transport模块

    1.http模块概述 The http module allows to expose Elasticsearch APIs over HTTP. The http mechanism is comp ...