通过域名来分配所访问的资源:
worker_processes 1;
events {
worker_connections 65535;
}
http {
server {
listen 80;
server_name www.aaa.com;
location / {
root html/www;
index index.html index.html;
}
}
server {
listen 80;
server_name bbs.aaa.com;
location / {
root html/bbs;
index index.html index.html;
}
}
}
在所访问的服务器内的nginx/html/目录下,建立www和bbs文件夹,内设访问页面index.html即可。
通过location匹配访问资源:
location用法:
location [=|~|~*|^~] uri { ……..}
其中“location =/documents/”表示精准匹配,只能匹配=后面的内容
“location ^~/documents/”表示匹配常规字符串,及时后面有正则匹配,也不进行正则匹配
“location ~*\.(gif|jpg)$”不区分大小写的正则匹配
“location ~\.(gif|jpg)$”区分大小写的正则匹配
“location /documents/”匹配常规字符,如果有正则匹配,则优先正则匹配
location = / {
return 401;
}
location = /documents/ {
return 402;
}
location ^~/documents/ {
return 403;
}
location /images/ {
return 405;
}
location ~* \.(gif|jpg|iepg)$ {
return 406;
}
通过rewrite匹配访问资源:
rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用,例如 http://seanlook.com/a/we/index.php?id=1&u=str只对/a/we/index.php重写。
语法rewrite regex replacement [flag];
如果相对域名或参数字符串起作用,可以使用全局变量匹配,也可以使用proxy_pass反向代理。
表明看rewrite和location功能有点像,都能实现跳转,主要区别在于rewrite是在同一域名内更改获取资源的路径,而location是对一类路径做控制访问或反向代理,可以proxy_pass到其他机器。很多情况下rewrite也会写在location里,它们的执行顺序是:
- 执行server块的rewrite指令
- 执行location匹配
- 执行选定的location中的rewrite指令
如果其中某步URI被重写,则重新循环执行1-3,直到找到真实存在的文件;循环超过10次,则返回500 Internal Server Error错误。
在server中的应用:
浏览器中输入bbs.aaa.com后,永久跳转到www.aaa.com,后面带着的内容不变
server {
listen 80;
server_name bbs.aaa.com;
rewrite ^/(.*)http://www.aaa.com/$1 permanent;
location / {
root html/bbs;
index index.htmlindex.html;
}
其中 ^/(.*)代表匹配所有,$1表示取前面括号中的内容,结尾permanent;显示跳转后url地址。
在if中应用:
重写带参数的url:
将bbs.aaa.com/local_pid/querylocalpidinfo.do?uid=888&pid=999&port=855的带参数的url,重写变化域名并且去掉结尾参数。即变成www.aaa.com/ local_pid/querylocalpidinfo.do?uid=888&pid=999,下面是具体配置信息。
server {
listen 80;
server_name bbs.aaa.com;
if ($args ~* port=855 )
{
Rewrite ^/local_pid/queryLocalPidInfo.dohttp://www.aaa.com/local_pid/queryLocalPidInfo.do?uid=$arg_uid&pid=$arg_pid? break;
}
通过proxy_pass 反向代理
Proxy_pass 指令属于ngx_http_proxy_module模块的,此模块请求转发到另一个服务器,在实际工作中,会通过location功能匹配制定的url,然后把符合匹配的url通过proxy_pass抛转到定义好的upstream节点池。
下面实例,将www.aaa.com抛转到定义好的172.18.3.186中;
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www_server_pools{
server 172.18.3.186 weight=1;
}
server {
listen 80;
server_name www.aaa.com;
location / {
proxy_passhttp://www_server_pools;
}
附:全局变量信息表
- $args : #这个变量等于请求行中的参数,同$query_string
- $content_length : 请求头中的Content-length字段。
- $content_type : 请求头中的Content-Type字段。
- $document_root : 当前请求在root指令中指定的值。
- $host : 请求主机头字段,否则为服务器名称。
- $http_user_agent : 客户端agent信息
- $http_cookie : 客户端cookie信息
- $limit_rate : 这个变量可以限制连接速率。
- $request_method : 客户端请求的动作,通常为GET或POST。
- $remote_addr : 客户端的IP地址。
- $remote_port : 客户端的端口。
- $remote_user : 已经经过Auth Basic Module验证的用户名。
- $request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。
- $scheme : HTTP方法(如http,https)。
- $server_protocol : 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
- $server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。
- $server_name : 服务器名称。
- $server_port : 请求到达服务器的端口号。
- $request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
- $uri : 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
- $document_uri : 与$uri相同。