nginx常用功能配置
一、规范优化nginx配置文件
nginx的主配置文件为nginx.conf,主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目录中,虚拟主机的配置文件按照网站的域名或功能取名,例如www.conf、bbs.conf、blog.conf等。当然,如果虚拟主机的数量不是很多,也可以把多个虚拟主机配置成一个单独的配置文件,仅仅和nginx的主配置文件 nginx.conf分离开即可。
这里使用的参数是include,下面先看看它的语法:
include file | mask
它可以放置在nginx配置中的任何位置。用法示例如下:
include mime.types;
include www.conf; #包含单个文件;
include vhosts/*.conf 包含vhosts下所有以conf结尾的文件;
下面是nginx配置的实战方案,具体如下:
#以基于域名的虚拟主机为例: [root@nginx conf]# mkdir extra
[root@nginx conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf #过滤包含#号和空行,生成新文件nginx.conf #查看新生成的nginx配置文件
[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 server {
11 listen 80;
12 server_name localhost;
13 location / {
14 root html;
15 index index.html index.htm;
16 }
17 error_page 500 502 503 504 /50x.html;
18 location = /50x.html {
19 root html;
20 }
21 }
22 } #把10-21行的虚拟主机配置内容分别写到extra/dmtest1.conf,extra/dmtest2.conf和extra/dmtest3.conf文件中,并做修改
[root@nginx conf]# sed -n '10,21p' nginx.conf >extra/dmtest1.conf
[root@nginx conf]# sed -n '10,21p' nginx.conf >extra/dmtest2.conf
[root@nginx conf]# sed -n '10,21p' nginx.conf >extra/dmtest3.conf [root@nginx conf]# cat extra/dmtest1.conf
server {
listen 80;
server_name www.dmtest1.com;
location / {
root html/dmtest1; #站点目录修改为html/dmtest1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
} [root@nginx conf]# cat extra/dmtest2.conf
server {
listen 80;
server_name www.dmtets2.com;
location / {
root html/dmtest2; #站点目录修改为html/dmtest2;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
} [root@nginx conf]# cat extra/dmtest3.conf
server {
listen 80;
server_name www.dmtest3.com;
location / {
root html/dmtest3; #站点目录修改为html/dmtest1;
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,21d' nginx.conf
[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;
}
把虚拟主机独立配置文件dmtest1.conf、dmtest2.conf、dmtest3.conf的信息包含到nginx.conf里,这样就把主配置和各个虚拟主机配置分离了。
#操作前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 } #执行下面的插入命令:
[root@nginx conf]# sed -i '10 i include extra/dmtest1.conf;\ninclude extra/dmtest2.conf;\ninclude extra/dmtest3.conf;' nginx.conf #查看修改后的配置文件:
[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 extra/dmtest1.conf;
include extra/dmtest2.conf;
include extra/dmtest3.conf;
}
检查配置文件并重载
[root@nginx conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful
[root@nginx conf]# systemctl reload nginx
创建虚拟主机站点目录和首页文件
[root@nginx conf]# mkdir -p ../html/{dmtest1,dmtest2,dmtest3} [root@nginx conf]# echo "www.dmtest1.com" >../html/dmtest1/index.html
[root@nginx conf]# echo "www.dmtest2.com" >../html/dmtest2/index.html
[root@nginx conf]# echo "www.dmtest3.com" >../html/dmtest3/index.html
测试
[root@nginx extra]# curl www.dmtest1.com
www.dmtest1.com
[root@nginx extra]# curl www.dmtest2.com
www.dmtest2.com
[root@nginx extra]# curl www.dmtest3.com
www.dmtest3.com
二、nginx虚拟主机的别名配置
虚拟主机别名,就是为虚拟主机设置除了主域名以外的-个或多个域名名字,这样就能够实现用户访问的多个域名对应同一个虚拟主机网站的功能。
以www.dmtest1.com域名的虚拟主机为例,为其增加一个别名dmtest1.com。使其访问时得到的网站内容和www.dmtest1.com一样。
配置如下:
[root@nginx conf]# cat extra/dmtest1.conf
server {
listen 80;
server_name www.dmtest1.com dmtest1.com;
location / {
root html/dmtest1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
} 重新加载配置文件
[root@nginx conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful
[root@nginx conf]# systemctl reload nginx Linux下面进行配置域名解析
[root@nginx conf]# tail -3 /etc/hosts
192.168.200.102 www.dmtest1.com dmtest1.com 测试
[root@nginx conf]# curl dmtest1.com
www.dmtest1.com
[root@nginx conf]# curl www.dmtest1.com
www.dmtest1.com 说明:如果想要在Windows访问,也需要配置hosts解析。
三、nginx状态信息功能
nginx status介绍
nginx软件的功能模块中有一个ngx_http_stub_status_module模块,这个模块的主要功能是记录nginx的基本访问状态信息,让使用者了解nginx的工作状态,例如链接数信息等。要使用状态模块必须增加http_stub_status_module模块来支持。
如果需要重新编译增加nginx模块,请参考:https://www.cnblogs.com/Mr-Ding/p/9539192.html
可以通过下面的方法检查编译安装nginx时是否设定了上述模块:
[root@nginx nginx]# sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.8.1 --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module 有这个--with-http_stub_status_module模块就对了。
配置nginx status
具体配置过程如下:
[root@nginx nginx]# cat conf/extra/status.conf
server {
listen 80;
server_name status.dmtest1.com dmtest1.com;
location /nginx_status {
stub_status on; #打开信息状态开关
access_log off;
}
} [root@nginx nginx]# sed -i '13 i include extra/status.conf;' conf/nginx.conf
[root@nginx nginx]# cat conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/dmtest1.conf;
include extra/dmtest2.conf;
include extra/dmtest3.conf;
include extra/status.conf; #不要忘记增加包含文件的配置到主配置文件nginx.conf
}
检查语法并重启服务
[root@nginx nginx]# sbin/nginx -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful
[root@nginx nginx]# systemctl reload nginx
测试结果如下:
Linux下面的测试结果: [root@nginx nginx]# curl status.dmtest1.com
Active connections: 2
server accepts handled requests
5 5 18
Reading: 0 Writing: 1 Waiting: 1
[root@nginx nginx]# curl dmtest1.com
Active connections: 2
server accepts handled requests
6 6 19
Reading: 0 Writing: 1 Waiting: 1
Windows下面的测试结果:
也可以使用location的方法来实现状态信息配置,例如在任意一个虚拟主机里,为serer标签增加如下配置:
[root@nginx nginx]# cat conf/extra/dmtest2.conf
server {
listen 80;
server_name www.dmtest2.com;
location / {
root html/dmtest2;
index index.html index.htm;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.200.0/24;
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
} 结果如下:
[root@nginx nginx]# curl www.dmtest2.com/nginx_status
Active connections: 1
server accepts handled requests
71 71 87
Reading: 0 Writing: 1 Waiting: 0
ngixn status 显示结果详解:
Active connections: 1
server accepts handled requests
71 71 87
Reading: 0 Writing: 1 Waiting: 0 Active connections: 1 表示正在活动的链接数有1个; 其中server表示nginx启动到现在共处理了71个链接; accepts表示nginx启动到现在共创建了71次握手; 请求丢失数=(握手数-链接数),可以看出本次状态显示没有丢失请求; handled requests表示总共处理了87次请求; Reading:为nginx读取到客户端的Header信息数; Writing:为nginx返回给客户端的Header信息数; Waiting:为nginx已经处理完正在等候下一次请求指令的驻留链接。在开启keep-alive的情况下,这个值等于active-(reading+writing) 注意:为了安全起见,这个状态信息要防止外部用户查看。
四、为nginx增加错误日志(error_log)配置
nginx软件会把自身运行的故障信息及用户访问的日志信息记录到指定的日志文件里。
nginx错误日志信息介绍:
nginx错误日志信息参数名字为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:miain,http,server,location
参考资料:http://nginx.org/en/docs/ngx_core_module.html#error_log
nginx错误日志配置:
编辑nginx的主配置文件 nginx.conf,增加错误日志的配置方法如下: [root@nginx nginx]# cat conf/nginx.conf
worker_processes 1;
error_log logs/error.log error; #一般配置这一行即可;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/dmtest1.conf;
include extra/dmtest2.conf;
include extra/dmtest3.conf;
}
nginx常用功能配置的更多相关文章
- Nginx常用功能配置二
Nginx常用功能配置二 Nginx location匹配设置 location作用:可以根据用户请求的URI来执行不同的应用,根据用户请求的网站的地址URL匹配. location语法: locat ...
- Nginx常用功能配置一
Nginx常用功能配置 参数include配置 说明:如果日常工作中server标签存在太多,可以采用include配置模式,Nginx的主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目 ...
- 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 ...
- 3.Nginx常用功能介绍
Nginx常用功能介绍 Nginx反向代理应用实例 反向代理(Reverse Proxy)方式是指通过代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器,并且从内部网络服 ...
- Nginx常用功能
3.Nginx常用功能 3.1 反向代理服务器 3.1.1.demo2 a.我在tomcat下部署了一个javaweb项目,tomcat安装的服务器IP为:192.168.37.136,部署的项目在t ...
- 2.了解nginx常用的配置
作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-07-10 20:56:10 星期三 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...
- 前端开发掌握nginx常用功能之rewrite
上一篇博文对nginx最常用功能的server及location的匹配规则进行了讲解,这也是nginx实现控制访问和反向代理的基础.掌握请求的匹配规则算是对nginx有了入门,但是这些往往还是不能满足 ...
- nginx常用服务配置
一.nginx.conf的配置方式,创建新vhost user nginx; worker_processes ; worker_cpu_affinity ; worker_rlimit_nofile ...
- Apache运维中常用功能配置笔记梳理
Apache 是一款使用量排名第一的 web 服务器,LAMP 中的 A 指的就是它.由于其开源.稳定.安全等特性而被广泛使用.下边记录了使用 Apache 以来经常用到的功能,做此梳理,作为日常运维 ...
随机推荐
- RN初始化项目报错
解决方法:全局删除yarn
- 洛谷 P1908 逆序对(树状数组解法)
归并排序解法:https://www.cnblogs.com/lipeiyi520/p/10356882.html 题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不 ...
- ES5(基本包装类型)字符串的方法
看一下字符串有哪些常用的方法: 1.concat();将多个文本组合起来,返回新的字符串,就是拼接字符串. 查找位置 2.indexOf();返回要匹配的字符在字符串第一次出现的索引,参数就是匹配的字 ...
- Django (八) 中间件&验证码&富文本&缓存
中间件&验证码&富文本&缓存 1. 中间件&AOP 中间件:是一个轻量级的,底层的插件,可以介入Django的请求和响应过程(面向切面编程) 中间件的本质就是一 ...
- centos7安装文档
1.当载入安装镜像时,我们会看到如下图中的画面,我们选择第一项,安装centos7 2.选择英语(个人测试环境可以使用中文安装) 3.选择network&hostname配置网络 4.在配置网 ...
- web移动端滑动插件
1.slip只有6.3k可以说是非常小了,主要是通过css3里面的transform来改变的位置,控制的是父容器,使用也非常简单,具体信息移步slip.js.一个简单的demo如下 <!DOCT ...
- PM2常用命令
安装pm2 npm install -g pm2 1.启动 pm2 start app.js pm2 start app.js --name my-api #my-api为PM2进程名称 pm2 ...
- springJDBC 事物隔离
五.Spring-jdbc的实现 第一步:导jar包 pom.xml <!--引入spring-beans节点--><dependency> <groupId> ...
- 開玩樹莓派(一):安裝Raspbian系統
目錄: 開玩樹莓派(一):安裝Raspbian系統 開玩樹莓派(二):配置IP,實現無顯示器局域網內Putty連接和RDP遠程 開玩樹莓派(三):Python編程 開玩樹莓派(四):GPIO控制和遠程 ...
- HTML中实现Table表头点击升序/降序排序
题目:如下图,请实现表格信息的排序功能,当点击表头的属性区域,将表格信息进行排序切换功能,即第一次点击为降序排序,再一次点击进行升序排序. 姓名 力量 敏捷 智力 德鲁伊王 17 24 13 月之骑士 ...