您的位置:首页 > 运维架构 > Nginx

深入浅出Nginx之七【重要知识补充】

2016-03-08 17:04 549 查看
作为Nginx学习的最后,本篇博客简要介绍在Nginx使用过程中的一些尚未提及的重要知识点,包括Nginx的性能监控、限制下载速度、防盗链、rewrite与重定向、location的优先级以及二级域名的配置等方面。

<一>. Nginx性能监控:

1. 查看Nginx的版本:

Shell代码


nginx -v

2. 查看Nginx安装配置的详细模块:

Shell代码


nginx -V

可以看到Nginx安装时,已经安装了用于性能监控的模块--with-http_stub_status_module

3. 配置nginx.conf,允许通过HTTP方式访问性能监控页面:

Shell代码


vi /usr/local/nginx/conf/nginx.conf

添加如下内容:


Shell代码


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

说明: 允许IP地址192.168.142.1访问,拒绝除此之外的其他任何请求,其他请求返回403 [Forbidden]

4.测试验证:

Shell代码


/usr/local/nginx/sbin/nginx -t #校验nginx.conf的合法性
kill -HUP `cat /opt/nginx/logs/nginx.pid` #平滑重启

通过http://www.excelsoft.com/nginx_status地址访问,网页内容如下:

Text/plain代码


Active connections: 2
server accepts handled requests
5 5 31
Reading: 0 Writing: 1 Waiting: 1

<二>. 限制下载速度:

Shell代码


server {
listen 80;
server_name download.excelsoft.com;

location / {
limit_rate 256k;
proxy_pass http://blog_server_cluster; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

access_log /opt/nginx/logs/access.download.log access;
}

说明:

限制单个连接的下载速度:limit_rate 256k;


<三>. Nginx的防盗链:

1. 防盗链:防止其他网站盗用本站“图片”链接,造成服务器的负担.

HTTP协议发起请求的时候,通常会通过消息头部的Referer字段表明当前请求是从哪里链接过来的.

2. 如果来自于其他网站,进行rewrite重定向或者返回403无权限

Shell代码


location ~* \.(gif|jpeg|jpg|png|bmp|swf)$ {
...
valid_referers none blocked www.excelsoft.com excelsoft.com *.excelsoft.com;
if ($invalid_referer) {
return 403;
}
}

说明:


valid_referers:指定合法的链接地址

none:请求头部丢失Referer字段,认为合法

blocked:请求头部有Referer字段,但是被防火墙或者代理服务器删除了它的值

$invalid_referer:非法的Referer字段,禁止访问

<四>. rewrite与重定向:

Shell代码


if (!-f $request_filename) {
rewrite ^/ http://www.excelsoft.com permanent;
}

说明:如果请求的文件不存在,进行重定向

添加了permanent视为永久重定向,返回301状态码 [Moved Permanently]

否则视为临时重定向,返回302状态码 [Moved Temporarily]

<五>. location的优先级:


1. 普通location:优先级别最低

Shell代码


location / {
}

2. 普通location:最大前缀匹配

Java代码


location /image/ {
}

3. 普通location:最大前缀匹配,成功后不在进行正则匹配

Shell代码


location ^~ /image/ {
}

4. 普通location:严格匹配

Shell代码


location = /image/hello.jpg {
root /software/static;
}

5. 正则匹配:优先于“除了以上两种之外”的普通location,“后缀名”忽略大小写,优先顺序和书写顺序相关

Shell代码


location ~* \.(gif|jpeg|jpg|png|bmp|swf)$ {
}

[优先级别] (1) < (2) < (5) < (3) < (4)

<六>. 二级域名与系统子目录:


1. 使用Nginx的rewrite功能可以将二级域名映射为系统的子目录结构,比如访问news.excelsoft.com/news.jsp链接,使用nginx的rewrite功能重写为www.excelsoft.com/news/news.jsp地址,然后再转发给Tomcat

2. 配置nginx.conf:把static.excelsoft.com和download.excelsoft.com虚拟主机放置在前面,将不会和二级域名的虚拟主机进行匹配

Shell代码


server {
listen 80;
server_name static.excelsoft.com;
...
}

server {
listen 80;
server_name download.excelsoft.com;
...
}

进行二级域名的配置:


Shell代码


server {
listen 80;
server_name www.excelsoft.com excelsoft.com *.excelsoft.com;

location / {
if ($http_host ~* (.+).excelsoft.com$) {
set $sub_domain $1;
}
if ($sub_domain != "www") {
rewrite ^/(.*) /$sub_domain/$1 break;
}

proxy_pass http://blog_server_cluster; ...
}
}

到此,Nginx的学习告一段落

,好怀念那个熟悉而又模糊的Tom小猫

,嗯,什么时候好好学习一下!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: