一、规范优化nginx配置文件

nginx的主配置文件为nginx.conf,主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目录中,虚拟主机的配置文件按照网站的域名或功能取名,例如www.conf、bbs.conf、blog.conf等。当然,如果虚拟主机的数量不是很多,也可以把多个虚拟主机配置成一个单独的配置文件,仅仅和nginx的主配置文件 nginx.conf分离开即可。

这里使用的参数是include,下面先看看它的语法:

  1. include file | mask

它可以放置在nginx配置中的任何位置。用法示例如下:

  1. include mime.types;
  2. include www.conf;     #包含单个文件;
  3. include vhosts/*.conf    包含vhosts下所有以conf结尾的文件;

下面是nginx配置的实战方案,具体如下:

  1. #以基于域名的虚拟主机为例:
  2.  
  3. [root@nginx conf]# mkdir extra
  4. [root@nginx conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf #过滤包含#号和空行,生成新文件nginx.conf
  5.  
  6. #查看新生成的nginx配置文件
  7. [root@nginx conf]# cat -n nginx.conf
  8. 1 worker_processes 1;
  9. 2 events {
  10. 3 worker_connections 1024;
  11. 4 }
  12. 5 http {
  13. 6 include mime.types;
  14. 7 default_type application/octet-stream;
  15. 8 sendfile on;
  16. 9 keepalive_timeout 65;
  17. 10 server {
  18. 11 listen 80;
  19. 12 server_name localhost;
  20. 13 location / {
  21. 14 root html;
  22. 15 index index.html index.htm;
  23. 16 }
  24. 17 error_page 500 502 503 504 /50x.html;
  25. 18 location = /50x.html {
  26. 19 root html;
  27. 20 }
  28. 21 }
  29. 22 }
  30.  
  31. #把10-21行的虚拟主机配置内容分别写到extra/dmtest1.conf,extra/dmtest2.conf和extra/dmtest3.conf文件中,并做修改
  32. [root@nginx conf]# sed -n '10,21p' nginx.conf >extra/dmtest1.conf
  33. [root@nginx conf]# sed -n '10,21p' nginx.conf >extra/dmtest2.conf
  34. [root@nginx conf]# sed -n '10,21p' nginx.conf >extra/dmtest3.conf
  35.  
  36. [root@nginx conf]# cat extra/dmtest1.conf
  37. server {
  38. listen 80;
  39. server_name www.dmtest1.com;
  40. location / {
  41. root html/dmtest1;        #站点目录修改为html/dmtest1;
  42. index index.html index.htm;
  43. }
  44. error_page 500 502 503 504 /50x.html;
  45. location = /50x.html {
  46. root html;
  47. }
  48. }
  49.  
  50. [root@nginx conf]# cat extra/dmtest2.conf
  51. server {
  52. listen 80;
  53. server_name www.dmtets2.com;
  54. location / {
  55. root html/dmtest2;      #站点目录修改为html/dmtest2;
  56. index index.html index.htm;
  57. }
  58. error_page 500 502 503 504 /50x.html;
  59. location = /50x.html {
  60. root html;
  61. }
  62. }
  63.  
  64. [root@nginx conf]# cat extra/dmtest3.conf
  65. server {
  66. listen 80;
  67. server_name www.dmtest3.com;
  68. location / {
  69. root html/dmtest3;       #站点目录修改为html/dmtest1; 
  70. index index.html index.htm;
  71. }
  72. error_page 500 502 503 504 /50x.html;
  73. location = /50x.html {
  74. root html;
  75. }
  76. }

删除nginx.conf主配置文件中的server{}标签段

  1. [root@nginx conf]# sed -i '10,21d' nginx.conf
  2. [root@nginx conf]# cat nginx.conf
  3. worker_processes 1;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. sendfile on;
  11. keepalive_timeout 65;
  12. }

把虚拟主机独立配置文件dmtest1.conf、dmtest2.conf、dmtest3.conf的信息包含到nginx.conf里,这样就把主配置和各个虚拟主机配置分离了。

  1. #操作前nginx.conf配置文件内容如下:
  2. [root@nginx conf]# cat -n nginx.conf
  3. 1 worker_processes 1;
  4. 2 events {
  5. 3 worker_connections 1024;
  6. 4 }
  7. 5 http {
  8. 6 include mime.types;
  9. 7 default_type application/octet-stream;
  10. 8 sendfile on;
  11. 9 keepalive_timeout 65;
  12. 10 }
  13.  
  14. #执行下面的插入命令:
  15. [root@nginx conf]# sed -i '10 i include extra/dmtest1.conf;\ninclude extra/dmtest2.conf;\ninclude extra/dmtest3.conf;' nginx.conf
  16.  
  17. #查看修改后的配置文件:
  18. [root@nginx conf]# cat nginx.conf
  19. worker_processes 1;
  20. events {
  21. worker_connections 1024;
  22. }
  23. http {
  24. include mime.types;
  25. default_type application/octet-stream;
  26. sendfile on;
  27. keepalive_timeout 65;
  28. include extra/dmtest1.conf;
  29. include extra/dmtest2.conf;
  30. include extra/dmtest3.conf;
  31. }

检查配置文件并重载

  1. [root@nginx conf]# ../sbin/nginx -t
  2. nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
  3. nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful
  4. [root@nginx conf]# systemctl reload nginx

创建虚拟主机站点目录和首页文件

  1. [root@nginx conf]# mkdir -p ../html/{dmtest1,dmtest2,dmtest3}
  2.  
  3. [root@nginx conf]# echo "www.dmtest1.com" >../html/dmtest1/index.html
  4. [root@nginx conf]# echo "www.dmtest2.com" >../html/dmtest2/index.html
  5. [root@nginx conf]# echo "www.dmtest3.com" >../html/dmtest3/index.html

测试

  1. [root@nginx extra]# curl www.dmtest1.com
  2. www.dmtest1.com
  3. [root@nginx extra]# curl www.dmtest2.com
  4. www.dmtest2.com
  5. [root@nginx extra]# curl www.dmtest3.com
  6. www.dmtest3.com

二、nginx虚拟主机的别名配置

虚拟主机别名,就是为虚拟主机设置除了主域名以外的-个或多个域名名字,这样就能够实现用户访问的多个域名对应同一个虚拟主机网站的功能。

以www.dmtest1.com域名的虚拟主机为例,为其增加一个别名dmtest1.com。使其访问时得到的网站内容和www.dmtest1.com一样。

配置如下:

  1. [root@nginx conf]# cat extra/dmtest1.conf
  2. server {
  3. listen 80;
  4. server_name www.dmtest1.com dmtest1.com;
  5. location / {
  6. root html/dmtest1;
  7. index index.html index.htm;
  8. }
  9. error_page 500 502 503 504 /50x.html;
  10. location = /50x.html {
  11. root html;
  12. }
  13. }
  14.  
  15. 重新加载配置文件
  16. [root@nginx conf]# ../sbin/nginx -t
  17. nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
  18. nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful
  19. [root@nginx conf]# systemctl reload nginx
  20.  
  21. Linux下面进行配置域名解析
  22. [root@nginx conf]# tail -3 /etc/hosts
  23. 192.168.200.102 www.dmtest1.com dmtest1.com
  24.  
  25. 测试
  26. [root@nginx conf]# curl dmtest1.com
  27. www.dmtest1.com
  28. [root@nginx conf]# curl www.dmtest1.com
  29. www.dmtest1.com
  30.  
  31. 说明:如果想要在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时是否设定了上述模块:

  1. [root@nginx nginx]# sbin/nginx -V
  2. nginx version: nginx/1.8.1
  3. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
  4. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  5. TLS SNI support enabled
  6. configure arguments: --prefix=/application/nginx-1.8.1 --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
  7.  
  8. 有这个--with-http_stub_status_module模块就对了。

配置nginx status

具体配置过程如下:

  1. [root@nginx nginx]# cat conf/extra/status.conf
  2. server {
  3. listen 80;
  4. server_name status.dmtest1.com dmtest1.com;
  5. location /nginx_status {
  6.    stub_status on;     #打开信息状态开关
  7. access_log off;
  8. }
  9. }
  10.  
  11. [root@nginx nginx]# sed -i '13 i include extra/status.conf;' conf/nginx.conf
  12. [root@nginx nginx]# cat conf/nginx.conf
  13. worker_processes 1;
  14. events {
  15. worker_connections 1024;
  16. }
  17. http {
  18. include mime.types;
  19. default_type application/octet-stream;
  20. sendfile on;
  21. keepalive_timeout 65;
  22. include extra/dmtest1.conf;
  23. include extra/dmtest2.conf;
  24. include extra/dmtest3.conf;
  25. include extra/status.conf;    #不要忘记增加包含文件的配置到主配置文件nginx.conf
  26. }

检查语法并重启服务

  1. [root@nginx nginx]# sbin/nginx -t
  2. nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
  3. nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful
  4. [root@nginx nginx]# systemctl reload nginx

测试结果如下:

  1. Linux下面的测试结果:
  2.  
  3. [root@nginx nginx]# curl status.dmtest1.com
  4. Active connections: 2
  5. server accepts handled requests
  6. 5 5 18
  7. Reading: 0 Writing: 1 Waiting: 1
  8. [root@nginx nginx]# curl dmtest1.com
  9. Active connections: 2
  10. server accepts handled requests
  11. 6 6 19
  12. Reading: 0 Writing: 1 Waiting: 1

Windows下面的测试结果:

也可以使用location的方法来实现状态信息配置,例如在任意一个虚拟主机里,为serer标签增加如下配置:

  1. [root@nginx nginx]# cat conf/extra/dmtest2.conf
  2. server {
  3. listen 80;
  4. server_name www.dmtest2.com;
  5. location / {
  6. root html/dmtest2;
  7. index index.html index.htm;
  8. }
  9. location /nginx_status {
  10. stub_status on;
  11. access_log off;
  12. allow 192.168.200.0/24;
  13. deny all;
  14. }
  15. error_page 500 502 503 504 /50x.html;
  16. location = /50x.html {
  17. root html;
  18. }
  19. }
  20.  
  21. 结果如下:
  22. [root@nginx nginx]# curl www.dmtest2.com/nginx_status
  23. Active connections: 1
  24. server accepts handled requests
  25. 71 71 87
  26. Reading: 0 Writing: 1 Waiting: 0

ngixn status 显示结果详解:

  1. Active connections: 1
  2. server accepts handled requests
  3. 71 71 87
  4. Reading: 0 Writing: 1 Waiting: 0
  5.  
  6. Active connections: 1 表示正在活动的链接数有1个;
  7.  
  8. 其中server表示nginx启动到现在共处理了71个链接;
  9.  
  10. accepts表示nginx启动到现在共创建了71次握手;
  11.  
  12. 请求丢失数=(握手数-链接数),可以看出本次状态显示没有丢失请求;
  13.  
  14. handled requests表示总共处理了87次请求;
  15.  
  16. Reading:为nginx读取到客户端的Header信息数;
  17.  
  18. Writing:为nginx返回给客户端的Header信息数;
  19.  
  20. Waiting:为nginx已经处理完正在等候下一次请求指令的驻留链接。在开启keep-alive的情况下,这个值等于active-(reading+writing
  21.  
  22. 注意:为了安全起见,这个状态信息要防止外部用户查看。

四、为nginx增加错误日志(error_log)配置

nginx软件会把自身运行的故障信息及用户访问的日志信息记录到指定的日志文件里。

nginx错误日志信息介绍:

  1. nginx错误日志信息参数名字为error_log,可以放在Main区块中全局配置,也可以放置不同的虚拟主机中单独记录。
  2.  
  3. error_log的语法格式及参数语法说明如下:
  4. error_log file level;
  5. 关键字 日志文件 错误日志级别
  6.  
  7. 其中,关键字error_log不能改变,日志文件可以指定任意存放日志的目录,错误日志级别常见的有【debug|info|notice|warn|error|crit|alert|emerg】,级别越高,记录的信息越少,生产场景一般是warn|error|crit这三个级别之一,注意不要配置info等较低级别,会带来巨大磁盘I/O消耗。
  8.  
  9. error_log的默认值为:
  10. #default:error_log logs/error.log error;
  11.  
  12. 可以放置的标签段为:
  13. #context:miain,http,server,location

参考资料:http://nginx.org/en/docs/ngx_core_module.html#error_log

nginx错误日志配置:

  1. 编辑nginx的主配置文件 nginx.conf,增加错误日志的配置方法如下:
  2.  
  3. [root@nginx nginx]# cat conf/nginx.conf
  4. worker_processes 1;
  5. error_log logs/error.log error; #一般配置这一行即可;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. include mime.types;
  11. default_type application/octet-stream;
  12. sendfile on;
  13. keepalive_timeout 65;
  14. include extra/dmtest1.conf;
  15. include extra/dmtest2.conf;
  16. include extra/dmtest3.conf;
  17. }

nginx常用功能配置的更多相关文章

  1. Nginx常用功能配置二

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

  2. Nginx常用功能配置一

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

  3. 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 ...

  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. 2.了解nginx常用的配置

    作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-07-10 20:56:10 星期三 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...

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

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

  8. nginx常用服务配置

    一.nginx.conf的配置方式,创建新vhost user nginx; worker_processes ; worker_cpu_affinity ; worker_rlimit_nofile ...

  9. Apache运维中常用功能配置笔记梳理

    Apache 是一款使用量排名第一的 web 服务器,LAMP 中的 A 指的就是它.由于其开源.稳定.安全等特性而被广泛使用.下边记录了使用 Apache 以来经常用到的功能,做此梳理,作为日常运维 ...

随机推荐

  1. docker Mac安装和使用

    1.安装docker brew cask install docker 2.安装后可以用命令查看版本 docker --version 3.build java 项目(jar) docker buil ...

  2. E. The Best among Equals

    http://codeforces.com/gym/101149/problem/E 这题的话,关键是注意到一定是要max score 然后就可以选出一个L最大优先,并且R最大的区间, 扫一次就能得到 ...

  3. Unity AssetBundle笔记

    1.入门: Resources:表示U3D自动将资源打成一个AssetBundle包,所有放在Resources下的文件夹都会打成一个AssetBundle包,资源非常大,Resources文件夹在真 ...

  4. 03.Javascript——入门一些方法记录之Map和Set

    JavaScript的默认对象表示方式{}可以视为其他语言中的Map或Dictionary的数据结构,即一组键值对. 但是JavaScript的对象有个小问题,就是键必须是字符串.但实际上Number ...

  5. linux 的iptables失效解决方法

    1.首先查看iptables配置文件:cat  /etc/sysconfig/iptables 2.然后查看 iptables 配置文件是否生效:iptables  -L,结果如下,很显然和上面的配置 ...

  6. Android - Zxing实现二维码的扫描与生成

    Zxing: Zxing是一个开放源码,用java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口.可以实现使用手机内置摄像头完成条形码的扫描以及解码. github:     ...

  7. Android上线check_list

    Android 上线 check_list 类型 序号 检查项 结果(pass/no) 安装 卸载 1 非Root环境下的安装.卸载 2 Root环境下的安装.卸载 3 安装文件检查,无泄漏用户信息的 ...

  8. synchronized关键字修饰非静态方法与静态方法的区别

    这里我们先创建ObjLock类,并实现Runnable接口.并创建一个Demo类,具有被synchronized关键字修饰的非静态方法与静态方法. 非静态方法 public class ObjLock ...

  9. 用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 ...

  10. OS X快捷键小技巧

    退出command+Q,关分页Command+W,刷新Command+R,新开分页Command+T 全屏 ctrl+command+F 每个Mac使用者都知道点击下窗口左上角黄色圆形的按钮就可以最小 ...