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;
include extra/dmtest2.conf;
include 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;
}
原文地址:https://www.cnblogs.com/Mr-Ding/p/9535843.html