[关闭]
@yanglt7 2018-10-21T15:53:53.000000Z 字数 3847 阅读 664

【Web 集群实战】07_Nginx 常用功能配置

Web集群实战


1. 规范优化 Nginx 配置文件

  1. include file | mask;
  1. include mime.type;
  2. include www.conf; // 包含单个文件
  3. include vhosts/*.conf // 包含 vhosts 下所有以 conf 结尾的文件
  1. [root@ylt001 conf]# mkdir extra
  2. [root@ylt001 conf]# /bin/cp nginx.conf_BaseName nginx.conf
  3. [root@ylt001 conf]# sed -n '10,17p' nginx.conf
  4. Server {
  5. listen 80;
  6. server_name www.yangyangyang.org;
  7. location / {
  8. root html/www;
  9. index.html index.htm;
  10. }
  11. }
  12. [root@ylt001 conf]# sed -n '10,17p' nginx.conf >extra/www.conf
  13. [root@ylt001 conf]# sed -n '18,25p' nginx.conf
  14. Server {
  15. listen 80;
  16. server_name bbs.yangyangyang.org;
  17. location / {
  18. root html/bbs;
  19. index.html index.htm;
  20. }
  21. }
  22. [root@ylt001 conf]# sed -n '18,25p' nginx.conf >extra/bbs.conf
  23. [root@ylt001 conf]# sed -n '26,33p' nginx.conf
  24. Server {
  25. listen 80;
  26. server_name blog.yangyangyang.org;
  27. location / {
  28. root html/blog;
  29. index.html index.htm;
  30. }
  31. }
  32. [root@ylt001 conf]# sed -n '26,33p' nginx.conf >extra/blog.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. keepalived_timeout 65;
  10. }
  1. [root@ylt001 conf]# sed -i '10 include extra/www.conf; \ninclude extra/bbs.conf; \ninclude extra/blog.conf;' 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. keepalived_timeout 65;
  10. include extra/www.conf;
  11. include extra/bbs.conf;
  12. include extra/blog.conf;
  13. }
  1. [root@ylt001 conf]# ../sbin/nginx -t
  2. [root@ylt001 conf]# ../sbin/nginx -s reload
  3. [root@ylt001 conf]# tail -1 /etc/hosts
  4. [root@ylt001 conf]# curl www.yangyangyang.org
  5. [root@ylt001 conf]# curl blog.yangyangyang.org
  6. [root@ylt001 conf]# curl bbs.yangyangyang.org

2. Nginx 虚拟主机的别名配置

  1. Server {
  2. listen 80;
  3. server_name www.yangyangyang.org yangyangyang.org;
  4. location / {
  5. root html/www;
  6. index.html index.htm;
  7. }
  8. }
  1. [root@ylt001 conf]# ../sbin/nginx -t
  2. [root@ylt001 conf]# ../sbin/nginx -s reload
  1. [root@ylt001 conf]# vi /etc/hosts
  2. [root@ylt001 conf]# tail -1 /etc/hosts
  3. [root@ylt001 conf]# curl www.yangyangyang.org
  4. [root@ylt001 conf]# curl yangyangyang.org

3. Nginx 状态信息功能

Nginx status 介绍

  1. [root@ylt001 conf]# ../sbin/nginx -V

配置 Nginx status

  1. [root@ylt001 conf]# cat >>/application/nginx/conf/extra/status.conf<<EOF
  2. >##status
  3. >server{
  4. > listen 80;
  5. > server_name status.yangyangyang.org;
  6. > location / {
  7. > stub_status on;
  8. > access_log off;
  9. > }
  10. >}
  11. >EOF
  12. [root@ylt001 conf]# cat extra/status.conf
  13. [root@ylt001 conf]# sed -i '13 i include extra/status.conf;' nginx.conf
  14. [root@ylt001 conf]# cat -n nginx.conf
  15. worker_processes 1;
  16. events {
  17. worker_connections 1024;
  18. }
  19. http {
  20. include mime.types;
  21. default_type application/octet-stream;
  22. sendfile on;
  23. keepalived_timeout 65;
  24. include extra/www.conf;
  25. include extra/bbs.conf;
  26. include extra/blog.conf;
  27. include extra/status.conf
  28. }
  1. [root@ylt001 conf]# ../sbin/nginx -t
  2. [root@ylt001 conf]# ../sbin/nginx -s reload

4. 为 Nginx 增加错误日志(error_log)配置

Nginx 错误日志信息介绍

  1. error_log file level
  2. 关键字 日志文件 错误日志级别
  1. #default: error_log logs/error.log error;
  1. #context: main, http, server, location

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

Nginx 错误日志配置

  1. worker_processes 1;
  2. error_log logs/error.log;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. sendfile on;
  10. keepalived_timeout 65;
  11. include extra/www.conf;
  12. include extra/bbs.conf;
  13. include extra/blog.conf;
  14. include extra/status.conf;
  15. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注