环境:web1 web2 nginx+keepalived
1、location简介:
nginx中的location指令是NginxHttpCoreModule中重要指令。Location指令比较简单,也比较常用。Sets a configuration based on a request URL.
Location指令,是用来对url进行匹配的,URL及语法中的/uri/,可以是字符串或正则表达式。如果是正则表达式,则必须指定前缀。location指令根据URL来应用不同的配置。这个指令运行根据不同URL来应用不同的url配置。
官方网址:http://nginx.org/en/docs/http/ngx_http_core_module.html
2、基本语法:
Syntax: | location [ location |
---|---|
Default: | — |
Context: | server , location (位置) |
解释:
[ = ] 精确匹配,如果找到匹配=号的内容,立即停止搜索,并立即处理请求(优先级最高)
[ ~ ] 区分大小写
[ ^~ ] 只匹配字符串,不匹配正则表达式
[ ~* ] 不区分大小写
[ @ ] 指定一个命名的location,一般只用于内部重定向请求。location @name{...}
3、匹配过程:
首先对字符串进行匹配查询,最确切的匹配将被使用。然后,正则表达式的匹配查询开始,匹配第一个结果后会停止搜索。
如果没有正则表达式,将使用字符串的搜索结果。如果字符串和正则都匹配,那么正则优先级较高。
Let’s illustrate the above by an example:
location = / { [ configuration A ]} location / { [ configuration B ]} location /documents/ { [ configuration C ]} location ^~ /images/ { [ configuration D ]} location ~* \.(gif|jpg|jpeg)$ { [ configuration E ]}
The “
/
” request will match configuration A,
the “/index.html
” request will match configuration B,
the “/documents/document.html
” request will match
configuration C,
the “/images/1.gif
” request will match configuration D, and
the “/documents/1.jpg
” request will match configuration E.
4、Proxy_pass指令:
Proxy_pass 指令属于ngx_http_proxy_module 模块,此模块可以将请求转发到另外一台服务器。
proxy_set_header host $host ;
#当后端web服务器上也配置多个虚拟主机时,需要用该Header来区分反向代理那个主机名;
proxy_pass http://http://bbs_server_pool;
#指定反向代理的服务器池
proxy_set_header X-Forwarded-For $remote_addr;
范例:
nginx代理设置:
upstream bbs_real_servers {
#ip_hash;
server 10.10.70.80:80 weight=15;
server 10.10.70.82:80 weight=15;
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
proxy_pass http://bbs_real_servers;
proxy_set_header host $host ;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
#如果后端web服务器上的程序需要获取用户的ip.从该header头获取。需调整web的
web节点设置:
apache:
#202 LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
#注意虚拟主机的日志格式
nginx:
#log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
# ‘$status $body_bytes_sent "$http_referer" ‘
# ‘"$http_user_agent" "$http_x_forwarded_for"‘;
#日志格式:"10.10.70.64" - - [03/Jan/2015:09:20:31 +0800] "GET / HTTP/1.0" 200 14 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"
参数:proxy_set_header :设置由后端的服务器获取用户的主机名或者真实的ip地址。
client_body_buffer_size : 用于指定客户端请求主体缓冲区大小
proxy_connect_timeout:表示与后端服务器连接的超时时间
proxy_send_timeout:表示后端服务器数据传回的超时时间
proxy_buffer_size :设置缓冲区大小
proxy_busy_buffers_size:用于设置系统繁忙是可以使用的proxy——buffers大小
proxy_timp_write_size:指定proxy临时缓存文件的大小。
生产范例:
proxy_redirect off;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#可以使用include包含
5、根据url中的目录地址实现代理转发(动静分离)
准备:
web1 目录blog下创建mkdir static && echo "static 23 " >dynamic/index.html #动态服务器php (mysql读写)
web2 目录blog下创建mkidr dynamic && echo "static 21 " >static/index.html #静态server(图片)
测试要成功
nginx配置范例:
upstream static_pools {
server 10.10.70.82:80 weight=15;
}
upstream dynamic_pools {
server 10.10.70.82:80 weight=15;
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
proxy_pass http://dynamic_pools;
}
location /static/ {
proxy_pass http://static_pools;
}
location /dynamic/ {
proxy_pass http://dynamic_pools;
}
access_log off;
}
#使用include 包含;重启nginxx
访问测试:http://www.etiantian.org/dynamic/
http://www.etiantian.org/static/
6、根据文件扩展名实现代理转发
范例:upstream static_pools { server 10.0.0.22:80 weight=15; } upstream dynamic_pools { server 10.0.0.23:80 weight=15; } server { listen 80; server_name blog.etiantian.org; location / { proxy_pass http://dynamic_pools; include extra/proxy.conf; } location ~ .*.(gif|jpg|jpcg|pnp|swf|css|js)$ { proxy_pass http://static_pools; include extra/proxy.conf; } location ~ .*.(php|php3|php5)$ { proxy_pass http://dynamic_pools; include extra/proxy.conf; } access }
7、 根据浏览器控制代理访问
upstream static_pools {
server 10.0.0.22:80 weight=5;
}
upstream dynamic_pools {
server 10.0.0.23:80 weight=5;
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
if ($http_user_agent ~* "MSIE")
{
proxy_pass http://dynamic_pools;
}
if ($http_user_agent ~* "Firefox")
{
proxy_pass http://static_pools;
}
proxy_pass http://dynamic_pools;
include extra/proxy.conf;
}
access_log off;
}
#if ($http_user_agent ~* "Firefox|MSIE")
#{
#return 403;
#}
8、根据访问设备代理控制
upstream android_pools {
server 10.0.0.22:80 weight=5;
}
upstream iphone_pools {
server 10.0.0.23:80 weight=5;
}
server {
listen 80;
server_name www.etiantian.org;
include extra/proxy.conf;
location / {
if ($http_user_agent ~* "android")
{
proxy_pass http://android_pools;
}
if ($http_user_agent ~* "iphone")
{
proxy_pass http://iphone_pools;
}
proxy_pass http://dynamic_pools;
include extra/proxy.conf;
}
access_log off;
}