nginx中级应用-续
1、server下配置的每个location,都需要有自己的一套代理配置
即要么加入:
root 某个目录
要么加入
proxy_pass 某个地址;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
否则如果该location被请求匹配,nginx会先读取该location下的root或者proxy_pass,如果没有,则取server下全局的root配置
如果还没有,则使用默认配置(nginx安装目录下的html目录)
如果多个location中使用了同一个配置,可以把配置抽为一个conf文件,之后再include *.conf把配置引进来
官方root说明:http://nginx.org/en/docs/http/ngx_http_core_module.html#root
Syntax:root path;
Default:
root html;
Context:http, server, location, if in location
可用在http,server,location和if中
官方proxy_pass说明:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
| Syntax: | proxy_pass |
|---|---|
| Default: | — |
| Context: | location, if in location, limit_except |
只能用在location和if中
于是就导致了:要想使用expired令浏览器使用缓存机制,则必须要在静态文件的匹配中
要么把root配置到对应的网站文件夹下,要么配置到proxy_pass
配置好之后,成功让浏览器接受到304响应,嘿嘿,下面是配置文件:由此可见,若想在响应头中添加什么值,最好把代理的服务器抽出一个单独的主机配置文件,在location中
#user nobody;
worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt)$
{
#缓存时长
expires 7d;
} #JS和CSS缓存时间设置
location ~ .*\.(js|css)?$
{
expires 1h;
} error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} server {
listen 80;
server_name angular.ailiailia.com;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
} # location /status {
# stub_status on;
# access_log off;
# } location /.(gif|jpg|jpeg|png|bmp|swf)$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8000;
expires 30d;
} location /.(js|css)?$ {
expires 12h;
}
} server {
listen 80;
server_name node.ailiailia.com; location / {
proxy_pass http://localhost:3000/;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
} location /.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
} location /.(js|css)?$ {
expires 12h;
}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }
2、nginx的匹配优先级:
不同于防火墙,前面的规则优先
nginx类似于css,权重高的优先
但是也不全是:
一、 location 的匹配符
1.等于匹配符:=
等于匹配符就是等号,特点可以概括为两点:
精确匹配
不支持正则表达式
2.空匹配符
空匹配符的特点是:
匹配以指定模式开始的 URI
不支持正则表达式
3.正则匹配符:~
正则匹配符是可以使用正则表达式的匹配符。不过这里要强调的是,一般来说~是指:
区分大小写的正则匹配
而~*表示:
不区分大小写的正则匹配
但是对于一些对大小写不敏感的操作系统,这两者没有区别。另外一个就是^~,其表示以指定模式开始的正则匹配。
4.内部访问符:@
一般用于错误页面等,这个暂不讨论。
二、匹配符优先级
1.=
2.空匹配符,满足精确匹配时
3.^~
4.~或~*
5.空匹配符,满足以指定模式开始时的匹配时
(location =) > (location 完整路径 >) >(location ^~ 路径) >(location ~* 正则) >(location 路径)
只要匹配到,其它的都会忽略,然后返回到改匹配。
同级匹配符按从上到下的优先级排列
location 匹配的原型是这样的:location [=|~|~*|^~|@] /uri/ { … }
“=”是精确匹配
“@”是命名的location ,在正常的location 匹配中不会使用,仅仅在内部跳转中才会使用到。
“~”是区分大小写的匹配
“~*”是不区分大小写的匹配
“^~”表示中止正则匹配(这个平时没太注意)
在一个请求中,匹配的顺序是这样的。先使用所有location 来匹配URI的开始部分,最精确匹配的(形象点说,就是即配置字符数最多的)为最后匹配结果;然后进行正则表达式的匹配,按照配置文件中的顺序来进行匹配,如果有一个匹配成功,则结束正则匹配,且最后匹配结果为此location ,否则,最后结果为先前最精确匹配的的那个location 。
之前有提到过”^~”,它配置在非正则匹配中,表示,如果最精确匹配的loction为此location ,则立即返回该location 作为结果,而不进行下一步的正则匹配,这样,就此可以不必要进入到正则匹配当中,以加快匹配速度。
还有”=”,它是最精确的匹配,而且优先级最高。最先进行带”=”的匹配,如果匹配成功,立马返回。
最后总结下匹配的过程,有四步:
1. 带”=”前缀的先进行匹配,如果找到了,中止查找。
2. 所有其它location 进行非正则的匹配,找到最精确匹配的那个,如果匹配到带”^~”前缀的,则中止查找。
3. 正则查找,按照我们配置文件中配置的location 顺序进行查找。
4. 如果正则查找匹配成功,则使用此正则匹配的location ,否则,使用第二步查找的结果。
这里要特别说明下”=”与”^~”的区别:
“=”在匹配时,则匹配带”=”的location 。而”^~”,则会匹配所有非”=”的非正则location ,只有在确认它是最精确匹配的location 后,才生效。
location 匹配的优先级(与location在配置文件中的顺序无关)
= 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
最后匹配理带有"~"和"~*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。
location 优先级官方文档
- Directives with the = prefix that match the query exactly. If found, searching stops.
- All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
- Regular expressions, in order of definition in the configuration file.
- If #3 yielded a match, that result is used. Else the match from #2 is used.
- =前缀的指令严格匹配这个查询。如果找到,停止搜索。
- 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
- 正则表达式,在配置文件中定义的顺序。
- 如果第3条规则产生匹配的话,结果被使用。否则,如同从第2条规则被使用。
例如
location = / {
# 只匹配"/".
[ configuration A ]
}
location / {
# 匹配任何请求,因为所有请求都是以"/"开始
# 但是更长字符匹配或者正则表达式匹配会优先匹配
[ configuration B ]
}
location ^~ /images/ {
# 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
[ configuration C ]
}
location ~* .(gif|jpg|jpeg)$ {
# 匹配以 gif, jpg, or jpeg结尾的请求.
# 但是所有 /images/ 目录的请求将由 [Configuration C]处理.
[ configuration D ]
}
请求URI例子:
- / -> 符合configuration A
- /documents/document.html -> 符合configuration B
- /images/1.gif -> 符合configuration C
- /documents/1.jpg ->符合 configuration D
@location 例子
error_page 404 = @fetch;
location @fetch(
proxy_pass http://fetch;
)
3、if判别的使用
nginx重定向的IF条件判断
在server和location两种情况下可以使用nginx的IF条件判断,条件可以为以下几种:
正则表达式
如:
匹配判断
~ 为区分大小写匹配; !~为区分大小写不匹配
~* 为不区分大小写匹配;!~为不区分大小写不匹配
例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}
文件和目录判断
-f和!-f判断是否存在文件
-d和!-d判断是否存在目录
-e和!-e判断是否存在文件或目录
-x和!-x判断文件是否可执行
例如下面设定nginx在文件和目录不存在的时候重定向:
if (!-e $request_filename) {
proxy_pass http://127.0.0.1/;
}
return
返回http代码,例如设置nginx防盗链:
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked http://www.jefflei.com/ http://www.leizhenfang.com/;
if ($invalid_referer) {
return 404;
}
}
set
设置nginx变量
flag标记有:
* last 相当于Apache里的[L]标记,表示完成rewrite
* break 终止匹配, 不再匹配后面的规则
* redirect 返回302临时重定向 地址栏会显示跳转后的地址
* permanent 返回301永久重定向 地址栏会显示跳转后的地址
一些可用的全局变量有,可以用做条件判断(待补全)
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
http://m.blog.chinaunix.net/uid-24250828-id-3265253.html
4、执行以下命令,把ngx_http_substitutions_filter_module模块编译进去,主要为了反向绑定域名过滤到页面的URL地址。
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --add-module=/root/nginx-0.7.64/ngx_http_substitutions_filter_module
make && make install
nginx中级应用-续的更多相关文章
- nginx中级应用
1.安装监控模块 Nginx中的stub_status模块主要用于查看Nginx的一些状态信息. 本模块默认是不会编译进Nginx的,如果你要使用该模块,则要在编译安装Nginx时指定: . /con ...
- 原创|1分钟搞定 Nginx 版本的平滑升级与回滚
Nginx无论是对于运维.开发.还是测试来说,都是日常工作需要掌握的一个知识点,之前也写过不少关于Nginx相关的文章: Nginx服务介绍与安装 Nginx服务配置文件介绍 Nginx配置虚拟主机 ...
- 原创|强大!Nginx 配置在线一键生成“神器”
Nginx作为一个轻量级的HTTP服务器,相比Apache优势也是比较明显的,在性能上它占用资源少,能支持更高更多的并发连接,从而达到提高访问效率;在功能上它是一款非常优秀的代理服务器与负载均衡服务器 ...
- nginx配置神器
原文 https://mp.weixin.qq.com/s/zFEk7XzHj3xPReDXEnQxcQ https://nginxconfig.io/ Nginx作为一个轻量级的HTTP服务器,相比 ...
- Linux命令及架构部署大全
1.Linux系统基础知识 Linux 基础优化配置 Linux系统根目录结构介绍 linux系统重要子目录介绍 Linux基础命令(之一)详解 Linux基础命令(之二)详解 Linux文件系统 L ...
- 运维工程师打怪升级进阶之路 V2.0
在此之前,发布过两个版本: 运维工程师打怪升级之路 V1.0 版本发布 运维工程师打怪升级必经之路 V1.0.1 很多读者伙伴们反应总结的很系统.很全面,无论是0基础初学者,还是有基础的入门者,或者是 ...
- nginx的基础应用(续)
nginx的基础应用(续) 一.简介 上一篇文章我们介绍了nginx的基础应用,其中讲到了nginx作为代理服务器的使用,但是漏了一个重要的,也是使用非常普遍的特性--负载均衡.今天,我们将这段内容补 ...
- Nginx|构建简单的文件服务器(mac) 续-FastDFS安装(mac)|文件存储方案
目录 Nginx|构建简单的文件服务器(mac) 1 所需安装包 2 安装fastdfs-nginx-module-master 3 安装Nginx Nginx|构建简单的文件服务器(mac) 续上文 ...
- CentOS6.5 Nginx优化编译配置[续]
继续上文CentOS6.5 Nginx优化编译配置本文记录有关Nginx系统环境的一些细节设置,有关Nginx性能调整除了配置文件吻合服务器硬件之前就是关闭不必要的服务.磁盘操作.文件描述符.内核调整 ...
随机推荐
- c++ 字符输入读取
cin.clear()重置输入流 cin.get()锁住屏幕直到获取输入 while(cin) cin.get(ch) 方法返回的是一个cin对象,istream类提供了可以将istream对象转换为 ...
- Flask - 第一篇
首先,要看你学没学过Django 如果学过Django 的同学,请从头看到尾,如果没有学过Django的同学,并且不想学习Django的同学,轻饶过第一部分 一. Python 现阶段三大主流Web框 ...
- .net Reactor之exe、dll文件混淆
.net Reactor之exe.dll文件混淆 .net Reactor的主要功能: 1.是对dll文件.exe文件进行反编译混淆 2.对dll进行内部加锁,限制其使用的固定机器.固定时间.部署次数 ...
- sqlserver,oracle,mysql等的driver驱动,url怎么写
oracle driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521 ...
- 前端开发之CSS入门篇
一.CSS介绍和语法 二.CSS引入方式 三.基本选择器 四.高级选择器 五.伪类选择器 六.伪元素选择器 1️⃣ CSS介绍和语法 1. CSS的介绍 (1)为什么需要CSS? 使用css的目的就 ...
- MySQL内置功能之视图、触发器和存储过程
主要内容: 一.视图 二.触发器 三.存储过程 1️⃣ 视图 一.关于视图的理解 1.1.何谓视图? 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名], 用户使 ...
- Android 自定义组件之 带有悬浮header的listview
最近做项目遇到一个需求,要做一个带有悬浮header的listview,即,当listview滑动时,header消失,静止时header浮现. 这个需求看似简单,实际做起来还是会遇到不少的困难,特此 ...
- 【uva1658 算法竞赛入门经典】海军上将【费用流】
题意 给出一个v(3<=v<=1000)个点e(3<=e<=10000)条边的有向加权图,求1-v的两条不相交(除了起点和终点外没有公共点)的路径,使得权和最小. 分析 费用流 ...
- Listview getItemViewType的使用
ListView中有两个可以用来让ListView可以在视图中显示多种布局的方法,分别是getItemType和getViewTypeCount 其中 getItemViewType返回的是有参数po ...
- 276. Paint Fence篱笆涂色
[抄题]: There is a fence with n posts, each post can be painted with one of the k colors. You have to ...