Nginx介绍及安装配置(3)

Nginx基于端口的虚拟主机
基于端口的虚拟主机在生产环境中不多见偶尔会用到一般是为了公司内网提供访问的如OA系统、网站程序后台、CMS发布后台、MySQL的web客户端。使用特殊端口多是从安全上考虑的
配置虚拟主机监听的端口
[root@web01 conf]# vim nginx.conf
 
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen      8001;
        server_name  bbs.linuxidc.com;
        location / {
            root  html/bbs;
            index  index.html index.htm;
        }
    }
    server {
        listen      8002;
        server_name  ;
        location / {
            root  html/www;
            index  index.html index.htm;
        }
    }
    server {
        listen      8003;
        server_name  blog.linuxidc.com;
        location / {
            root  html/blog;
"nginx.conf" 34L, 759C written                             
[root@web01 conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[root@web01 conf]# ../sbin/nginx -s reload
[root@web01 conf]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address              Foreign Address            State      PID/Program name 
tcp        0      0 0.0.0.0:8001                0.0.0.0:*                  LISTEN      3763/nginx       
tcp        0      0 0.0.0.0:8002                0.0.0.0:*                  LISTEN      3763/nginx       
tcp        0      0 0.0.0.0:8003                0.0.0.0:*                  LISTEN      3763/nginx       
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                  LISTEN      1317/sshd         
tcp        0      0 :::22                      :::*                        LISTEN      1317/sshd         
[root@web01 conf]# curl
curl: (6) Couldn't resolve host 'www.linuxidc.com'
[root@web01 conf]# curl :8002

基于Ip的虚拟主机
    server {
        listen      10.0.0.9:80;
        server_name  ;
        location / {
            root  html/www;
            index  index.html index.htm;
        }
    }
Nginx别名配置
利用别名来监控集群下面的RS的URL。可以在监控服务器里面配置hosts里面配置别名解析用来监控
    server {
        listen      80;
        server_name  linuxidc.com
        location / {
            root  html/www;
            index  index.html index.htm;
        }
    }
在hosts解析里面配置linuxidc.com解析重启服务即可
第二种方法就是rewrite重定向
Nginx的配置文件优化
[root@web01 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include      mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include extra/www.conf;
    include extra/blog.conf;
    include extra/bbs.conf;
}
创建extra目录
[root@web01 conf]# mkdir extra
创建配置文件
[root@web01 conf]#sed -n '18,25p' nginx.conf.name >>extra/www.conf
[root@web01 conf]#sed -n '10,17p' nginx.conf.name >extra/bbs.conf
[root@web01 conf]#sed -n '26,33p' nginx.conf.name >>extra/blog.conf
测试结果
[root@web01 conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[root@web01 conf]# ../sbin/nginx -s reload
[root@web01 conf]# curl

[root@web01 conf]# curl blog.linuxidc.com
blog.linuxidc.com
[root@web01 conf]# curl bbs.linuxidc.com
bbs.linuxidc.com
Nginx的状态模块
--with-http_stub_status_module记录nginx的基本访问信息让使用者了解nginx的工作状态
[root@web01 conf]# ll extra/
total 16
-rw-r--r-- 1 root root 185 May 30 15:06 bbs.conf
-rw-r--r-- 1 root root 187 May 30 15:06 blog.conf
-rw-r--r-- 1 root root 168 May 30 15:33 status.conf
-rw-r--r-- 1 root root 199 May 30 15:13
[root@web01 conf]# cat extra/status.conf
  server {
        listen      80;
        server_name  status.linuxidc.com;
        location / {
            stub_status on;
            access_log off;
        }
}
[root@web01 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include      mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include extra/www.conf;
    include extra/blog.conf;
    include extra/bbs.conf;
    include extra/status.conf;
 
}

检查状态信息

第一个server 表示nginx一共处理了43个连接
第二个accepts 表示nginx从启动到现在成功创建了43次握手
请求丢失数=握手数-成功连接数可以看到这里没有丢失
第三个handle request表示总共处理了27次请求
Reading 表示nginx读取客户端的Header信息数
Writing 表示发回给客户端的Header信息数
Waiting 表示已经处理完正在等候下一次请求指令的驻留连接
Nginx增加错误日志
常见的有【debug|info|notice|warn|error|crit|alert|emerg】级别越高信息量越小。工作场景一般是warn|error|crit|这三个级别注意不要配置info等低级别的会带来I/O消耗
error_log默认配置为
#default:error_logs/error.log error;
可放置的标签段为
#context:main,http,server,location
Nginx的访问日志参数
可以查看nginx.conf.default配置文件里面有记录日志的详细格式。

Nginx日志格式中默认的参数配置如下
log_format  main  '$remote_addr - $remote_user [$time_local] "$request"
    '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
 
$server_name虚拟主机名称。
        $remote_addr远程客户端的IP地址。
        -空白用一个“-”占位符替代历史原因导致还存在。
        $remote_user远程客户端用户名称用于记录浏览者进行身份验证时提供的名字如登录百度的用户名scq2099yt如果没有登录就是空白。
        [$time_local]访问的时间与时区比如18/Jul/2012:17:00:01 +0800时间信息最后的"+0800"表示服务器所处时区位于UTC之后的8小时。
        $request请求的URI和HTTP协议这是整个PV日志记录中最有用的信息记录服务器收到一个什么样的请求
        $status记录请求返回的http状态码比如成功是200。
        $uptream_statusupstream状态比如成功是200.
        $body_bytes_sent发送给客户端的文件主体内容的大小比如899可以将日志每条记录中的这个值累加起来以粗略估计服务器吞吐量。
        $http_referer记录从哪个页面链接访问过来的。
        $http_user_agent客户端浏览器信息
        $http_x_forwarded_for客户端的真实ip通常web服务器放在反向代理的后面这样就不能获取到客户的IP地址了通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中可以增加x_forwarded_for信息用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
rewrite练习
http {
    # 定义image日志格式
    log_format imagelog '[$time_local] ' $image_file ' ' $image_type ' ' $body_bytes_sent ' ' $status;
    # 开启重写日志
    rewrite_log on;
server {
        root /home/www;
location / {
                # 重写规则信息
                error_log logs/rewrite.log notice;
                # 注意这里要用‘’单引号引起来避免{}
                rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4;
                # 注意不能在上面这条规则后面加上“last”参数否则下面的set指令不会执行
                set $image_file $3;
                set $image_type $4;
        }
location /data {
                # 指定针对图片的日志格式来分析图片类型和大小
                access_log logs/images.log mian;
                root /data/images;
                # 应用前面定义的变量。判断首先文件在不在不在再判断目录在不在如果还不在就跳转到最后一个url里
                try_files /$arg_file /image404.html;
        }
        location = /image404.html {
                # 图片不存在返回特定的信息
                return 404 "image not found\n";
        }
}
对形如/images/ef/uh7b3/test.png的请求重写到/data?file=test.png于是匹配到location /data先看/data/images/test.png文件存不存在如果存在则正常响应如果不存在则重写tryfiles到新的image404 location直接返回404状态码。
例2
rewrite ^/images/(.*)_(\d+)x(\d+)\.(png|jpg|gif)$ /resizer/$1.$4?width=$2&height=$3? last;
对形如/images/bla_500x400.jpg的文件请求重写到/resizer/bla.jpg?width=500&height=400地址并会继续尝试匹配location。
常用的正则
. 匹配除换行符以外的任意字符
? 重复0次或1次
+ 重复1次或更多次
* 重复0次或更多次
\d 匹配数字
^ 匹配字符串的开始
$ 匹配字符串的介绍
{n} 重复n次
{n,} 重复n次或更多次
[c] 匹配单个字符c
[a-z] 匹配a-z小写字母的任意一个
小括号()之间匹配的内容可以在后面通过$1来引用$2表示的是前面第二个()里的内容。正则里面容易让人困惑的是\转义特殊字符。
flag标志位
last : 相当于Apache的[L]标记表示完成rewrite
break : 停止执行当前虚拟主机的后续rewrite指令集
redirect : 返回302临时重定向地址栏会显示跳转后的地址
permanent : 返回301永久重定向地址栏会显示跳转后的地址
因为301和302不能简单的只返回状态码还必须有重定向的URL这就是return指令无法返回301,302的原因了。
这里 last 和 break 区别有点难以理解
last一般写在server和if中而break一般使用在location中
last不终止重写后的url匹配即新的url会再从server走一遍匹配流程而break终止重写后的匹配
break和last都能组织继续执行后面的rewrite指令

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/b96e4f34a0ef9145bb7e5f26b77d6b4c.html