nginx 1.6.3配置虚拟主机与rewrite-location匹配规则

1、 Nginx 虚拟主机配置(配置文件末尾以分号[;]结尾)

(1) 准备测试目录站点
[root@WEB conf]# cd /application/nginx/conf/
[root@WEB conf]# mkdir extra                                      (创建虚拟主机存放目录)
[root@WEB conf]# mkdir  /application/nginx/html/www               (创建虚拟主机网站根目录)
[root@WEB conf]# mkdir -p /var/www/html                           (创建虚拟主机网站根目录)   
[root@WEB conf]# echo "www" > /application/nginx/html/www/index.html       (www.etiantian.org 默认网页)
[root@WEB conf]# echo "bbs" > /var/www/html/index.html                     (bbs.etiantian.org  默认网页)

(2)配置nginx.conf主配置文件  
[root@WEB conf]# cat nginx.conf
error_log  logs/error.log;                           (指定错误日志)                  
worker_processes  1;                                 (进程数,一般是cpu核数的1-2倍)
events {
    worker_connections  1024;                        (最大并发连接数)
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include extra/*.conf;                           (指定虚拟主机存放位置)
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '              (access日志格式,main:日志的调用值)
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;            (访问日志,通过main调用日志的显示格式,这行必须放到日志的下面)             
}

(3)配置虚拟主机
[root@WEB conf]# cd extra/
[root@WEB extra]# cat www.conf
server {
        listen       80;
        server_name  www.etiantian.org etiantian.org;     域名,可以增加多个别名,用空格分开
        location / {
            root   html/www;                   默认网站根目录(在安装目录的html/www/下)
            index  index.html index.htm;
        }
        access_log  logs/www_access.log  main;            虚拟主机日志,放到server标签里面
}
[root@WEB extra]# cat bbs.conf
server {
        listen       80;
        server_name  bbs.etiantian.org;                   域名
        location / {
            root   /var/www/html;                            自定义默认网站根目录
            index  index.html index.htm;
        }
        access_log  logs/bbs_access.log  main;            虚拟主机日志,放到server标签里面
}
[root@WEB extra]# cat status.conf
server {
        listen       80;
        server_name  status.etiantian.org;               域名
        location / {
            stub_status on;                              开启监控模块(用于监控nginx状态信息)
            access_log   off;                            关闭日志
        }
}

(4) 配置nginx加密访问
[root@WEB extra]# cat www.conf
  server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
            auth_basic           "closed site";                                          开启加密认证
            auth_basic_user_file /application/nginx/conf/htpasswd;         认证文件
        }
        access_log  logs/www_access.log  main;
    }
# 配置认证文件并重启nginx
[root@WEB extra]# yum -y install httpd-tools
[root@WEB extra]# htpasswd -bc /application/nginx/conf/htpasswd oldboy 123456
[root@WEB extra]# cat /application/nginx/conf/htpasswd
oldboy:SDYvt.oRyvO5g
(3) 访问http://www.etiantian.org

2、 Nginx 日志切割

[root@WEB logs]# cd /server/scripts/
[root@WEB scripts]# cat nginx_log.sh
#!/bin/bash
date=`date +%F -d "-1day"`
cd /application/nginx/logs/ && \
mv www_access.log  ${date}_www_access.log
mv bbs_access.log  ${date}_bbs_access.log
> access.log
[root@WEB scripts]# crontab -e
0 0 * * *  /bin/sh /server/scripts/nginx_log.sh > /dev/null 2>&1 

3、Nginx location 匹配规则

(1) 精确匹配,访问www.etiantian.org 则匹配404
# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org
       404
[root@WEB extra]# cat www.conf
server {
        listen       80;
        server_name  www.etiantian.org;
          location = / {
            return 404;
        }

(2)正则匹配,以/images/开头的, images目录必须存在,访问www.etiantian.org/images/则匹配200
# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/images/
   200
location ^~/images/ {
            return 200;
       }

(3) 正则匹配以.gif .jpg .jpeg结尾,访问www.etiantian.org/a.jpg则匹配301
# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/a.jpg
  301
location ~*.(gif|jpg|jpeg)$ {
            return 301;
             }

(4) 常规匹配,如果有正则会优先匹配正则,访问www.etiantian.org/documents/ 则匹配302 
# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/documents/
   302
# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/documents/a.jpg
   301(加上a.jpg正则优先,匹配301
location /documents/ {
            return 302;
        }

(5) 常规匹配(默认匹配)都没有匹配到,则匹配500
# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/test/a.txt
   500
location / {
            return 500;
        }

(6) 表示访问网站根目录的时候会去/var/www/html/目录下去找
      如果匹配http://www.etiantian.org 就会去/var/www/html/目录下找
 location / {       
       root /var/www/html; 
       index index.html;          
        }

(7) 表示访问网站根目录下的blr2目录,会去/var/www/html/blr2/目录下去找
      如果匹配http://www.etiantian.org/blr2  就会去/var/www/html/目录下的 blr2/目录中去找  
      location /blr2 {     #这里的blr2是一个目录
       #注意:这里的网站更目录实际上是/var/www/html/blr2/
        root /var/www/html; 
        index index.html;              
        }

4、nginx rewrite规则

(1) 访问 www.etiantian.org  跳转到 www.baidu.com
[root@WEB extra]# cat www.conf
server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root  html/www;
            index index.html;
        }
        rewrite ^/(.*)      http://www.baidu.com/$1 permanent;   
}
解释:
当匹配www.etiantian.org下的任意内容时,则访问www.baidu.com 下的任意部分,$1表示(.*)
匹配域名下的任意内容,(.*)表示任意。这里可以有多个小括号,$1表示第一个小括号,$2第二个


(2) 访问bbs.etiantian.org/bbs 跳转到 www.baidu.com(前提是bbs.etiantian.org/bbs/index.html必须存在 )
[root@WEB extra]# cat bbs.conf
server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root  html/bbs;
            index index.html;
        }
        rewrite ^(.*)/bbs   http://www.baidu.com  permanent;
    }
解释:
当匹配bbs.etiantian.org/bbs 时,则跳转到www.baidu.com

(3) 访问www.etiantian.org/bbs 跳转到 bbs.etiantian.org/abc/bbs
 server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
        root  html/www;
        index index.html;
        }
        rewrite ^/(.*)  http://bbs.etiantian.org/abc/$1 permanent;
}
解释:
当匹配 www.etiantian.org下的任意内容时,则访问  bbs.etiantian.org/abc/ 下的任意内容


(4) 
server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
        root  html/www;
        index index.html;
        }
        rewrite ^(/download/.*)/media/(.*)\..*$  http://bbs.etiantian.org/$1/mp3/$2.mp3 last;
}
解释:
当访问:www.etiantian.org/download/test1/media/test2.php 时,
跳转到:bbs.etiantian.org/download/test1/mp3/test2.mp3 
$1=(/download/.*)=test1
$2=(.*)=test2

5、nginx location配置不同站点目录

server {
        listen       80;
        server_name  www.etiantian.org;
        location ~ .*\.(gif|jpg|png|css|js)$ {
            root  /data/www;
            index  index.jpg;
        }

        location ~ .*\.(html|htm|ht)$ {
             root /data/bbs;
             index   index.html;
        }

        location ~ .*\.(jsp|php) {
            root  /data/blog;
            index  index.php;
        }
    }

6、Nginx 定义错误页面

注意:如果有虚拟主机的话,需要在虚拟主机添加,虚拟主机优先生效
(1) 以.html的方式跳转

server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page  404 403  /50x.html;                    \\如果出现 404 403 页面,就跳转到网站根目录下的 50x.html (http://www.etiantian.ort/50x.html)
        location = /50x.html {                                    \\当匹配 50x.html
            root   html/www;                                        \\就去网站根目录下寻找50x.html
        }
}

(2) 以图片的方式跳转

server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page  404 403  /404.jpg;                          \\如果出现 404 403 页面,就跳转到网站根目录下的 404.jpg (http://www.etiantian.ort/404.jpg)    
        location = /404.jpg {                                         \\当匹配 404.jpg
            root   html/www;                                           \\就去网站根目录下寻找404.jpg  
        }
}

(3) 以rewrite的方式跳转
server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page  404 403  /404.jpg;                                                        \\如果出现 404 403 页面,就跳转到网站根目录下的 404.jpg (http://www.etiantian.ort/404.jpg),无论404.jpg是否存在                
        location = /404.jpg {                                                                       \\当匹配 404.jpg(无论404.jpg是否存在)
        access_log /data/logs/nginx/server_error.log;                               \\记录日志
        rewrite ^(.*)$ http://www.etiantian.org/50X.html redirect;           \\进行地址重写,将localtion匹配到的所有内容,重写为新的RUL,redirect 表示302临时重定向
        }
}


flag标记有:

* last 相当于Apache里的[L]标记,表示完成rewrite,匹配完,再向下匹配。地址栏会显示跳转后的地址
* break 终止匹配, 不再匹配后面的规则,地址栏会显示跳转后的地址
* redirect 返回302临时重定向,地址栏会显示跳转后的地址
* permanent 返回301永久重定向,地址栏会显示跳转后的地址
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的爱迪生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值