3.安装配置Nginx web服务器

1.安装Nginx

  先安装pcre库、openssl

yum install pcre pcre-devel openssl openssl-devel -y

2.安装Nginx

  下载地址http://www.nginx.org

 1 mkdir -p /home/ben/tools    ##建一个软件存放目录
 2 useradd nginx -s /sbin/nologin -M
 3 tar zxvf nginx-1.6.3.tar.gz
 4 cd nginx-1.6.3
 5 ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module
 6 make && make install
 7 ln -s /application/nginx-1.6.3 /application/nginx
 8 ./configure --help        ##编译时的可选参数。
 9 /application/nginx/sbin/nginx -V        ##查看编译的数
10 /application/nginx/sbin/nginx -t        ##启动前检查配置文件语法
11 /application/nginx/sbin/nginx            ##启动nginx
12 lsof -i:80

 3.配置Nginx

  3.1.单虚拟主机

  3.2.多域名虚拟主机

    在http{}区块内,另起一个server区段。 

server {
        listen       80;
        server_name  www.fox.org;
            location / {
                root   html/www;
                index      index.html    index.htm;
        }
   }
server {
        listen       80;
        server_name  bbs.fox.org;
        location / {
                root   html/bbs;
                index      index.html    index.htm;
        }
   }

  改为以下配置更为合理

  主配置nginx.conf改为

http {
.........
include extra/www.conf;
include extra/bbs.conf;
}

  vim extra/www.conf

server {
        listen       80;
        server_name  www.fox.org;
        access_log  logs/access_www.log  main;
        location / {
                root   html/www;
                autoindex on;        ##自动寻找首页文件,当找不到首页文件,会展示目录结构。
        }
   }

  vim extra/bbs.conf

server {
        listen       80;
        server_name  bbs.fox.org;
        access_log  logs/access_bbs.log  main;
        location / {
            root   html/bbs;
            index  index.html index.htm;
          }
        }

  3.3.多IP的虚拟主机

    添加2个IP 

1 ip addr add 10.0.0.9/24 dev eth0
2 ip addr add 10.0.0.10/24 dev eth0
server {
         listen       10.0.0.9:80;
         server_name  bbs.fox.org;  ##可以使用IP地址10.0.0.9访问
         location / {
                 root   html/bbs;
                 index      index.html    index.htm;
         }
   }

server {
         listen       10.0.0.10:80;
         server_name  www.fox.org;  ##可以使用IP地址10.0.0.10访问
         location / {
                 root   html/www;
                 index      index.html    index.htm;
         }
   }

  3.4.虚拟主机别名

server {
        listen       80;
        server_name  www.fox.org fox.org;
        access_log  logs/access_www.log  main;
        location / {
                root   html/www;
                autoindex on;
        }
   }

   3.5.状态信息

    编译时需要添加上--with-http_stub_status_module模块。

    方式一:

    增加一个虚拟主机配置

cat >>/application/nginx/conf/extra/status.conf<<EOF
server{
  listen 80;
  server_name status.fox.org;
  location / {
        stub_status on;
        access_log off;
    allow 10.146.3.137;
    deny all;
        }
}

sed -i '15 i include extra/status.conf;' nginx.conf

  http://status.fox.org

  

  active connections     ##nginx正处理的活动连接个数

  server    ##nginx从启动到现在共成功处理了1313482个连接

  accepts         ##nginx从启动到现在共成功创建了1313482次握手

  ##没有丢失数,accepts-server=0

  handled requests        ##总共处理1313495次请求

  reading         ##为nginx读取到客户端header信息数。

  writing          ##为nginx返回给客户端的header信息数。

  waiting         ##为nginx已经处理完,正等候下一次请求指令的驻留连接。在开启keep-alive的情况下,这个值=active-reading-wriging

    方式二:

      任意一个虚拟主机里为server标签增加配置:    

location /nginx_status {
        stub_status on;
        access_log off;
            allow 10.146.3.137;
            deny all;
        }
}

  3.6.日志切割

    脚本实现:把正在写入的日志改名,再重载nginx,生成新的日志。

vim cut_nginx_log.sh
#!/bin/bash
Dateformat=`date +%Y%m%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1
[ -f ${Logname}.log ]||exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload

crontab -e

00 00 * * * /bin/bash /server/script/cut_nginx_log.sh > /dev/null 2>&1

 

  3.7.location作用

    根据用户请求的URI来执行不同的应用。即依据用户请求的URL进行匹配,访问。

    语法:

    location [=|~|~*|^~] uri {

    ...........

    }

    ~   :区分大小写

    ~*  :不区分大小写

    !  :对匹配取反

    ^~  :在进行正常的字符串匹配检查之后,不做正则表达式的检查。如/static/20%/aa可以被規則^~ /static/ /aa匹配到,/static/a.html       可以被^~ /static/匹配

    匹配优先级:

顺序 location规则 说明
1 location = / 精确匹配
2 location ^~/images/ 匹配常规字符串,不做正则检查
3 location ~*\.(gif|jpg|jpeg)$ 正则匹配
4 location /documents/ 匹配常规字符串,如果有正则,优先正则匹配
5 location / 最后匹配

    例:

location = / {
        [ configuration A ]
    }    
location / {
        [ configuration B ]
    }    
location /documents/ {
        [ configuration C ]
    }
location ^~ /images/ {        ##匹配任何以/images/开头的查询并且停止搜索,任何正则表达式匹配将不会被检查。
        [ configuration D ]
    }
location ~* \.(gif|jpg|jpeg)$ {
        [ configuration E ]
    }

    不同的URI对应的配置

请求URI 完整URL 匹配的配置
/ http://www.fox.org/ configuration A
/index.html http://www.fox.org/ B
/documents/document.html http://www.fox.org/documents/document.html C
/images/1.gif http://http://www.fox.org/images/1.gif D
/documents/1.jpg http://http://www.fox.org/documents/1.jpg E

  例:

vim www.conf
server {
        listen       80;
        server_name  www.fox.org fox.org;
        access_log  logs/access_www.log  main;
        location / {
                return 401;
                }
        location = / {
                return 402;
                }
        location /documents/ {
                return 403;
                }
        location ^~ /images/ {
                return 404;
                }
        location ~* \.(gif|jpg|jpeg)$ {
                return 501;
                }
           }

      依 据匹配的优先顺序,用户请求的URI,返回的结果如下

/或空,即http://www.fox.org/ 或http://www.fox.org 402
/index.html 或任意不匹配其它location即http://www.fox.org/index.html或http://www.fox.org/任意 401
/documents/document.html,即http://www.fox.org/document/document.html等非gif|jpg|jpeg结尾 403
/images/1.gif,即http://www.fox.org/images/1.gif 404
/documents/1.jpg 501

 4.rewrite

    URL地址重写

    4.1.语法:

    rewrite regex replacement [flag];

    使用位置:server、location、if

    例:

rewrite ^/(.*)  http://www.fox.org/$1 permanent;

    ^/(.*)表示匹配所有,匹配成功后跳转到http://www.fox.org/$1,$1是取前面regex括号里的内容,

    permanent是永久301重定向标记,即跳转到http://www.fox.org/$1地址上。

 

    4.2.常用正则-regex

      (pattern)  :匹配括号内的pattern,并可以在后面获取对应的匹配,常用$0...$9属性获取小括号中的匹配内容,如(abcd) $0匹配到a

      (abc|bcd|cde),表示这一段是abc、bcd、cde三者之一均可,顺序也必须一致
      (abc)?,表示这一组要么一起出现,要么不出现,出现则按此组内的顺序出现
      (?:abc)表示找到这样abc这样一组,但不记录,不保存到$变量中,否则可以通过$x取第几个括号所匹配到的项,

      比如:(aaa) (bbb)(ccc)(?:ddd)(eee),可以用$1获取(aaa)匹配到的内容,而$3则获取到了(ccc)匹配到的内容,

      而$4则获取的 是由(eee)匹配到的内容,因为前一对括号没有保存变量
      a(?=bbb): 顺序环视 表示a后面必须紧跟3个连续的b
     (?i:xxxx) 不区分大小写

      (?s:.*) 跨行匹配.可以匹配回车符

      (ab){1,3},就表示ab一起连续出现最少1次,最多3次

      ab{1,3},就表示a,后面紧跟的b出现最少1次,最多3次

      [0-3],表示找到这一个位置上的字符只能是0到3这四个数字,与(abc|bcd|cde)的作用比较类似,但圆括号可以匹配多个连续的字

        符,而一对方括号只能匹配单个字符
      [^0-3],表示找到这一个位置上的字符只能是除了0到3之外的所有字符

      (\s*)表示连续空格的字符串。

      []是定义匹配的字符范围。比如 [a-zA-Z0-9] 表示相应位置的字符要匹配英文字符和数字。[\s*]表示空格或者*号。

      {}一般用来表示匹配的长度,比如 \s{3} 表示匹配三个空格,\s[1,3]表示匹配一到三个空格。

      (0-9) 匹配 '0-9′ 本身。 [0-9]* 匹配数字(注意后面有 *,可以为空)[0-9]+ 匹配数字(注意后面有 +,不可以为空){1-9} 写法错

      误。

      [0-9]{0,9} 表示长度为 0 到 9 的数字字符串。

 

    4.3.rewrite结尾的flag标记  

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

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

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

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

        last和break用来实现URL重写,浏览器地址栏的URL地址不变,但在服务器端访问的程序及路径发生了变化。

        redirect和permanent用来实现URL跳转,浏览器地址栏会显示跳转后的URL。

        使用alias指令时必须用last标记,使用proxy_pass指令时要用break标记。

        

        last与break说明:

          在根location(即location/{})中或server{}标签中编写rewrite规则建议用last标记。

          在普通localtion(如:location/ben/{}或if{})中编写rewrite规则,建议用break标记。

         301请求是可以缓存的, 即通过看status code,可以发现后面写着from cache。

         301表示旧地址A的资源已经被永久地移除了(这个资源不可访问了),搜索引擎在抓取新内容的同时也将旧的网址交换为重定向之后的网址;

         302表示旧地址A的资源还在(仍然可以访问),这个重定向只是临时地从旧地址A跳转到地址B,搜索引擎会抓取新的内容而保存旧的网址。

      4.3.1.rewrite应用场景  

      • 调整用户浏览的URL,看起来更规范。
      • 把动态URL伪装成静态地址,方便搜索引警收录。
      • 网站换新域名后,让旧域名的访问跳转到新的域名上,如,京东360buy换成jd.com.
      • 根据特殊变量、目录、客户端的信息进行URL跳转等。

      4.3.2.rewrite301跳转

        功能与别名类似,实现fox.org和www.fox.org访问同一个地址。

        

server {
        listen       80;
        server_name     fox.org;
        rewrite ^/(.*) http://www.fox.org/$1 permanent;
        access_log  logs/access_www.log  main;
        location / {
                root   html/www;
                autoindex on;
        }
   }

    server {
        listen  80;
        server_name     www.fox.org;
        location / {
                root html/www;
                index   index.html index.htm;
        }
  }

      4.3.3.不同域名的URL跳转

        访问http://blog.fox.org时跳转到http://www.fox.org/blog/ben.html,先设置好http://www.fox.org/blog/ben.html有结果输出

server {
        listen       80;
        server_name     blog.fox.org;
        location / {
                root   html/blog;
                index   index.html index.htm;
        }

        if ( $http_host ~* *^(.*)\.fox\.org$* ) {
                set $domain $1;
                rewrite ^(.*) http://www.fox.org/$domain/ben.html break;
        }
   }

      4.3.4.访问www.fox.org/bbs/时后跳转到http://bbs.fox.org

        www站点配置

server {
        listen  80;
        server_name     www.fox.org fox.org;
        location / {
                root html/www;
                index   index.html index.htm;
        }
        rewrite ^(.*)/bbs/ http://bbs.fox.org break;
        access_log logs/access_www.log main gzip buffer=32k flush=5s;
  }

        bbs站点配置

server {
        listen       80;
        server_name  bbs.fox.org;
        access_log  logs/access_bbs.log  main;
        location / {
            root   html/bbs;
            index  index.html index.htm;
          }
        }

 5.访问认证

  

server {
        listen  80;
        server_name     www.fox.org fox.org;
        location / {
                root html/www;
                index   index.html index.htm;
                auth_basic      "ben training";
                auth_basic_user_file    /application/nginx/conf/htpasswd;
        }
        access_log logs/access_www.log main gzip buffer=32k flush=5s;
  }

  配置账号密码

htpasswd -bc /application/nginx/conf/htpasswd bb 123456
chmod 400 /application/nginx/conf/htpasswd
chown nginx /application/nginx/conf/htpasswd

也可以手动编辑

vim /application/nginx/conf/htpasswd
    bb:sdkjs;sdfas    ###加密的密码串用openssl passwd 密码生成。
posted @ 2020-08-19 14:27  天涯160  阅读(199)  评论(0)    收藏  举报