利用include功能优化nginx的配置文件

  1. [root@lnmp conf]# cat nginx.conf
  2. worker_processes 1;
  3. events {
  4.     worker_connections 1024;
  5. }
  6. http {
  7.     include mime.types;
  8.     default_type application/octet-stream;
  9.     sendfile on;
  10.     keepalive_timeout 65;
  11.     #nginx vhosts config
  12.     include extra/www.conf;
  13.     include extra/bbs.conf;
  14.     include extra/blog.conf;
  15. }

写配置文件

  1. [root@lnmp conf]# mkdir extra
  2. [root@lnmp conf]# sed -n '18,25p' nginx.conf.base-name >extra/bbs.conf
  3. [root@lnmp conf]# cat extra/bbs.conf
  4.     server {
  5.         listen 80;
  6.         server_name bbs.etiantian.org;
  7.         location / {
  8.             root html/bbs;
  9.             index index.html index.htm;
  10.         }
  11.     }
  12. [root@lnmp conf]# sed -n '26,33p' nginx.conf.base-name >extra/blog.conf
  13. [root@lnmp conf]# cat extra/blog.conf
  14.    server {
  15.         listen 80;
  16.         server_name blog.etiantian.org;
  17.         location / {
  18.             root html/blog;
  19.             index index.html index.htm;
  20.         }
  21.     }
  22. [root@lnmp conf]# cat extra/www.conf
  23.     server {
  24.         listen 80;
  25.         server_name www.etiantian.org;
  26.         location / {
  27.             root html/www;
  28.             index index.html index.htm;
  29.         }
  30.     }

重启服务

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

测试

  1. [root@lnmp conf]# curl www.etiantian.org
  2. www.etiantian.org
  3. [root@lnmp conf]# curl bbs.etiantian.org
  4. bbs.etiantian.org
  5. [root@lnmp conf]# curl blog.etiantian.org
  6. blog.etiantian.org

nginx虚拟主机别名的配置

  1. [root@lnmp conf]# cat extra/www.conf
  2.     server {
  3.         listen 80;
  4.         server_name www.etiantian.org etiantian.org;
  5.         location / {
  6.             root html/www;
  7.             index index.html index.htm;
  8.         }
  9.     }
  10. [root@lnmp conf]# cat /etc/hosts
  11. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  12. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  13.  
  14. 192.168.31.132 server
  15. 192.168.31.133 lamp
  16. 192.168.31.134 lnmp www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org
  17. 192.168.31.136 backup
  18. [root@lnmp conf]# /application/nginx/sbin/nginx -t
  19. nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
  20. nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  21. [root@lnmp conf]# /application/nginx/sbin/nginx -s reload
  22. [root@lnmp conf]# curl etiantian.org
  23. www.etiantian.org
  24. [root@lnmp conf]# curl www.etiantian.org
  25. www.etiantian.org

启动报错:

  1. [root@lnmp ~]# /application/nginx/sbin/nginx -s reload
  2. nginx: [error] invalid PID number "" in "/application/nginx-1.6.3/logs/nginx.pid"

解决办法:

  1. [root@lnmp ~]# /application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
  2. [root@lnmp ~]# /application/nginx/sbin/nginx -s reload

nginx状态信息配置

  1. [root@lnmp extra]# cat status.conf
  2. #status
  3. server{
  4.     listen 80;
  5.     server_name status.etiantian.org;
  6.     location / {
  7.         stub_status on;
  8.         access_log off;
  9.         }
  10.  
  11.     }

添加包含

  1. include extra/status.conf;

重启nginx,浏览器访问

第一个server表示nginx启动到现在共处理了多少个连接。

第二个accepts表示nginx启动到现在共创建了多少次握手,请求丢失次数=(握手次数-连接次数)。

第三个handled request表示总共处理了多少次请求。

Reading:nginx读取到客户端的header信息数。

writing:nginx返回给客户端的header信息素。

waiting:nginx已经处理完正在等候下一次请求指令的驻留连接,开启keep-alive的情况下,这个值等于active-(reading+writing)。

nginx错误日志

常见的错误日志级别有[debug|info|notice|warn|error|crit|alert|emerg],级别越高记录的信息越少,生产场景一般是warm|error|crit这三个级别之一,注意不要配置info等级较低的级别,会带来磁盘I/O消耗。

error_log的默认值为:error_log logs/error.log error

  1. [root@lnmp conf]# cat nginx.conf
  2. worker_processes 1;
  3.  
  4. error_log logs/error.log error;
  5. events {
  6.     worker_connections 1024;
  7. }
  8. http {
  9.     include mime.types;
  10.     default_type application/octet-stream;
  11.     sendfile on;
  12.     keepalive_timeout 65;
  13.     #nginx vhosts config
  14.     include extra/www.conf;
  15.     include extra/bbs.conf;
  16.     include extra/blog.conf;
  17. }

可以放置的标签段为:main,http,server,location。

访问日志

log_format:用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)。

access_log:用来指定日志文件的路径及使用的何种日志格式记录日志。

  1. [root@lnmp conf]# cat nginx.conf
  2. worker_processes 1;
  3.  
  4. error_log logs/error.log error;
  5. events {
  6.     worker_connections 1024;
  7. }
  8. http {
  9.     include mime.types;
  10.     default_type application/octet-stream;
  11.     sendfile on;
  12.     keepalive_timeout 65;
  13.  
  14.     log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15.                       '$status $body_bytes_sent "$http_referer" '
  16.                       '"$http_user_agent" "$http_x_forwarded_for"';
  17.  
  18.  
  19.     #nginx vhosts config
  20.     include extra/www.conf;
  21.     include extra/bbs.conf;
  22.     include extra/blog.conf;
  23. }

$remote_addr:记录访问网站的客户端地址。

$http_x_forwarded_for:当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器上也要进行相关的x_forwarded_for设置。

$remote_user:远程客户端用户名称。

$time_local:记录访问时间与时区。

$request:用户的http请求起始行信息。

$status:http状态码,记录请求返回的状态,例如:200、404、301等。

$body_bytes_sent:服务器发送给客户端的响应body字节数。

$http_referer:记录此次请求是从哪个链接访问过来的,可以根据referer进行防盗链设置。

$http_user_agent:记录客户端访问信息,例如:浏览器、手机客户端等。

access_log off;这里的off表示不记录访问日志。

默认配置:access_log logs/access.log combined;

放置位置:http,server,location,if in location,limit_except。

  1. [root@lnmp conf]# cat extra/www.conf
  2.     server {
  3.         listen 80;
  4.         server_name www.etiantian.org etiantian.org;
  5.         location / {
  6.             root html/www;
  7.             index index.html index.htm;
  8.         }
  9.         access_log logs/access_www.log main;
  10.     }

重启nginx

  1. [root@lnmp conf]# /application/nginx/sbin/nginx -t
  2. nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
  3. nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  4. [root@lnmp conf]# /application/nginx/sbin/nginx -s reload
  5. [root@lnmp conf]# tail -f ../logs/access_www.log #访问日志
  6. 192.168.31.1 - - [16/Feb/2017:23:35:38 +0800] "GET / HTTP/1.1" 200 18 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" "-"
  7. 192.168.31.1 - - [16/Feb/2017:23:35:38 +0800] "GET /favicon.ico HTTP/1.1" 404 570 "http://www.etiantian.org/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" "-"

日志轮询

  1. [root@lnmp scripts]# cat cut_nginx_log.sh
  2. #!/bin/sh
  3. Dateformat=`date +%Y%m%d`
  4. Basedir="/application/nginx"
  5. Nginxlogdir="$Basedir/logs"
  6. Logname="access_www"
  7. [ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1
  8. [ -f ${Logname}.log ]||exit 1
  9. /bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
  10. $Basedir/sbin/nginx -s reload

执行

  1. [root@lnmp scripts]# sh cut_nginx_log.sh
  2. [root@lnmp scripts]# ll /application/nginx/logs/
  3. total 24
  4. -rw-r--r--. 1 root root 978 Feb 16 23:35 20170216_access_www.log

设置定时任务

  1. #nginx log cut
  2. 00 00 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

nginx常用日志收集及分析工具有rsyslog、awstats、flume、ELK、storm等。

rewrite

rewrite指令语法

指令语法:rewrite regex replacement [flag];

默认值:none

应用位置:server,location,if。

  1. [root@lnmp conf]# cat extra/www.conf
  2.     server {
  3.         listen 80;
  4.         server_name etiantian.org;
  5.         rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
  6.     }
  7.  
  8.     server {
  9.         listen 80;
  10.         server_name www.etiantian.org;
  11.         location / {
  12.             root html/www;
  13.             index index.html index.htm;
  14.         }
  15.         access_log logs/access_www.log main;
  16.     }

创建oldboy.html然后进行访问

  1. [root@lnmp www]# curl etiantian.org/oldboy.html -I
  2. HTTP/1.1 301 Moved Permanently
  3. Server: nginx/1.6.3
  4. Date: Thu, 16 Feb 2017 16:30:47 GMT
  5. Content-Type: text/html
  6. Content-Length: 184
  7. Connection: keep-alive
  8. Location: http://www.etiantian.org/oldboy.html

rewrite指令最后一项参数flag标记说明

last:本条规则匹配完成后,继续向下匹配新的location URI规则。

break:本条规则匹配完成即终止。不再匹配后面的任何规则。

redirect:返回302临时重定向,浏览器地址栏会显示跳转后的URL地址。

permanent:返回301永久重定向,浏览器地址栏会显示后的URL地址。

Nginx(二)的更多相关文章

  1. nginx(二)----ubuntu14.04下启动或重启和关闭nginx

    /** * lihaibo * 文章内容都是根据自己工作情况实践得出. *如有错误,请指正 *转载请注明出处 */ 一.启动 /usr/local/nginx/sbin/nginx或者cd /usr/ ...

  2. 网关中间件-Nginx(二)

    网关中间件-Nginx(一) 第一部分我们主要介绍如下几点: 1.nginx的基本概念 2.nginx结合业务场景实现负载均衡 3.常见问题的举例 这一部分主要介绍Nginx中限流,缓存,动静分离,以 ...

  3. Nginx (二) Nginx的反向代理负载均衡以及日志切割

    Nginx是一个高并发,高性能的服务器,可以进行反向代理以及网站的负载均衡.这些功能的运用都在配置文件中,也就是Nginx安装目录下的conf/nginx.conf. nginx.conf 1. 先来 ...

  4. 快速掌握Nginx(二) —— Nginx的Location和Rewrite

    1 location详解 1.location匹配规则 Nginx中location的作用是根据Url来决定怎么处理用户请求(转发请求给其他服务器处理或者查找本地文件进行处理).location支持正 ...

  5. nginx(二)nginx的安装

    下载 nginx官网下载地址 把源码解压缩之后,在终端里运行如下命令: ./configure make make install 默认情况下,Nginx 会被安装在 /usr/local/nginx ...

  6. Nginx(二) nginx 无法启动

    有时候在客户端输入:nginx 但是终端会输出以下,显示启动失败 nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already i ...

  7. Nginx(二):虚拟主机配置

    什么是虚拟主机? 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的Intemet服务器功 ...

  8. 《nginx 二》深入理解nginx的各项配置

    Nginx应用场景 1.http服务器.Nginx是一个http服务可以独立提供http服务.可以做网页静态服务器. 2.虚拟主机.可以实现在一台服务器虚拟出多个网站,例如个人网站使用的虚拟机. 3. ...

  9. Nginx(二) 反向代理&负载均衡

    1.反向代理 当我们请求一个网站时,nginx会决定由哪台服务器提供服务,就是反向代理. nginx只做请求的转发,后台有多个tomcat服务器提供服务,nginx的功能就是把请求转发给后面的服务器, ...

  10. nginx(二)

    nginx rewrite ​ Nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向.rewrite只能放在server{},location{},if{}中,并且 ...

随机推荐

  1. Introduction of filter in servlet

    官方给出的Filter的定义是在请求一个资源或者从一个资源返回信息的时候执行过滤操作的插件.我们使用过滤起最多的场景估计就是在请求和返回时候的字符集转换,或者权限控制,比如一个用户没有登录不能请求某些 ...

  2. kali 2018.2版本运行破解版burpsuite时候的问题。

    最近重装了kali虚拟机,装完之后把burp拷到里面发现运行不了了,折腾了下才解决,问题主要是由于java环境造成的. 系统默认是以java10运行burp的,但是java10好像是不支持    -X ...

  3. 互联网媒体类型 MIME Type

    参考:https://zh.wikipedia.org/wiki/%E4%BA%92%E8%81%94%E7%BD%91%E5%AA%92%E4%BD%93%E7%B1%BB%E5%9E%8B 互联网 ...

  4. [Java] 例外處裡 try/catch & throws

    public class CheckException { public static void main(String[] args) { File file = new File("xx ...

  5. Windows下将文件打包压缩成 .tar.gz格式

    1.下载 “7-ZIP”,安装完成后进入需要打包的文件夹 2. 右击选择“添加到压缩包” 3.压缩格式:tar 4. 得到.tar文件,将其打包 5. 压缩格式为:gzip 6. 得到tar.gz格式 ...

  6. Ubuntu 14.10 下Hive配置

    1 系统环境 Ubuntu 14.10 JDK-7 Hadoop 2.6.0 2 安装步骤 2.1 下载Hive 我第一次安装的时候,下载的是Hive-1.2.1,配置好之后,总是报错 [ERROR] ...

  7. Python【每日一问】03

    问:请给出下列代码的执行结果,并解释 a = dict.fromkeys([6, 7, 8], ["testing", {"name": "ken&q ...

  8. django之两个使用模板的例子

    from django.db import models # Create your models here. class Book(models.Model): title=models.CharF ...

  9. MSB8013

    解决方案: 去掉勾选

  10. asp.net 微信JsSDK

    有时间再整理吧 using System; using System.Collections.Generic; using System.Linq; using System.Web; using S ...