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 以来经常用到的功能,做此梳理,作为日常运维 ...
随机推荐
- docker Mac安装和使用
1.安装docker brew cask install docker 2.安装后可以用命令查看版本 docker --version 3.build java 项目(jar) docker buil ...
- E. The Best among Equals
http://codeforces.com/gym/101149/problem/E 这题的话,关键是注意到一定是要max score 然后就可以选出一个L最大优先,并且R最大的区间, 扫一次就能得到 ...
- Unity AssetBundle笔记
1.入门: Resources:表示U3D自动将资源打成一个AssetBundle包,所有放在Resources下的文件夹都会打成一个AssetBundle包,资源非常大,Resources文件夹在真 ...
- 03.Javascript——入门一些方法记录之Map和Set
JavaScript的默认对象表示方式{}可以视为其他语言中的Map或Dictionary的数据结构,即一组键值对. 但是JavaScript的对象有个小问题,就是键必须是字符串.但实际上Number ...
- linux 的iptables失效解决方法
1.首先查看iptables配置文件:cat /etc/sysconfig/iptables 2.然后查看 iptables 配置文件是否生效:iptables -L,结果如下,很显然和上面的配置 ...
- Android - Zxing实现二维码的扫描与生成
Zxing: Zxing是一个开放源码,用java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口.可以实现使用手机内置摄像头完成条形码的扫描以及解码. github: ...
- Android上线check_list
Android 上线 check_list 类型 序号 检查项 结果(pass/no) 安装 卸载 1 非Root环境下的安装.卸载 2 Root环境下的安装.卸载 3 安装文件检查,无泄漏用户信息的 ...
- synchronized关键字修饰非静态方法与静态方法的区别
这里我们先创建ObjLock类,并实现Runnable接口.并创建一个Demo类,具有被synchronized关键字修饰的非静态方法与静态方法. 非静态方法 public class ObjLock ...
- 用python编写九九乘法表
for i in range(1,10): for j in range(1,10): if j >i: print(end='') else: print(j,'*',i,'=',i*j,en ...
- OS X快捷键小技巧
退出command+Q,关分页Command+W,刷新Command+R,新开分页Command+T 全屏 ctrl+command+F 每个Mac使用者都知道点击下窗口左上角黄色圆形的按钮就可以最小 ...