1、基于URL的location 负责均衡配置
- 后端集群中的web服务器,必须要有对应的目录和文件才能被访问到
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream default_pool {
server 10.0.0.7:80 weight=1;
server 10.0.0.8:80 weight=1;
}
upstream bbs_pool {
server 10.0.0.7:8080 weight=1;
server 10.0.0.8:8080 weight=1;
}
upstream blog_pool {
server 10.0.0.7:8081 weight=1;
server 10.0.0.8:8081 weight=1;
}
server {
listen 80;
server_name www.etiantian.org;
# 直接访问网站根目录,跳转到后端服务器的对应网站根目录
location / {
proxy_pass http://default_pool;
}
# 访问http://www.etiantian.org/static/.*,跳转到集群池中的某一台服务器中,这台服务器也要有相同的域名和/static/静态目录才能访问到数据
location /static/ {
proxy_pass http://bbs_pool;
}
location /upload/ { //访问动态RUL,跳转到动态服务器池
proxy_pass http://blog_pool;
}
}
}
1.1、基于url的location,的反向代理
server {
listen 80;
server_name www.bbb.org;
location /pic/ {
proxy_pass http://www.accp.org;
}
}
访问http://www.bbb.org/pic/test.jpg 跳转到http://www.accp.org/pic/test.jpg
注意:另一边必须也要有 /pic/test.jpg才能被访问到
2、基于扩展名的location
- 后端集群中的web服务器,必须要有对应的目录和文件才能被访问到
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www_pool {
server 10.0.0.7:80 weight=1;
server 10.0.0.8:80 weight=1;
}
upstream bbs_pool {
server 10.0.0.7:8080 weight=1;
server 10.0.0.8:8080 weight=1;
}
upstream blog_pool {
server 10.0.0.7:8081 weight=1;
server 10.0.0.8:8081 weight=1;
}
server {
listen 80;
server_name www.etiantian.org;
# 访问http://www.etiantian.org/test.jpg 则跳转到集群池中的某天服务器,这台服务器也必须配置一样的域名并且有相同的test.jpg才能被访问
location ~ .*\.(gif|jpg|png|css|js)$ { //匹配图片后缀时,跳转到图片服务器池
proxy_pass http://www_pool;
}
location ~ .*\.(html|htm|ht)$ { //匹配html..时,跳转到静态服务器池
proxy_pass http://bbs_pool;
}
location ~ .*\.(jsp|php) { //匹配jsp|php时,跳转到动态服务器池
proxy_pass http://blog_pool;
}
}
}
3、基于本地与远端location
server {
listen 80;
server_name www.etiantian.org;
location ~ .*\.(gif|jpg|png|css|js)$ { //匹配jpg|gif时,跳转到图片服务器池
proxy_pass http://www_pool;
}
location ~ .*\.(html|htm|ht)$ { //匹配html时,访问本地站点目录
root /data/bbs;
index index.html;
}
}
(1)访问静态文件gif、jpg等都在本地打开,存放的目录为/usr/local/nginx/html,保存时间为30天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
root /usr/local/nginx/html;
expires 30d;
}
(2)访问动态文件,都抛向后端
location ~ (\.jsp)|(\.do)$ {
proxy_pass http://10.1.88.168:8080;
}
####################################
server
{
listen 80
server_name xxxxxxx;
location / {
#rewrite ^/ http://211.155.82.174:80 permanent;
proxy_pass http://211.155.82.174:80;
#proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
4、某公司location使用规则
server {
listen 80;
listen [::]:80;
server_name beta.deepcloudsdp.com saas2.deepcloudsdp.com; # 域名
root /project/redcore/webRoot/public; # 本地站点目录
index index.html; # 默认访问页面
proxy_set_header X-Real-IP $remote_addr;
location ~* /client/v[345]/push/sse { # 当访问http://beta.deepclousdsp.com/client/v3/push/see/xxx
rewrite /client/v[345]/push(.*) $1 break; # 跳转到http://beta.deepclousdsp.com/client/v3/pushxxx 之后会break就不再让下走了
proxy_pass http://172.17.123.5:8181;
proxy_set_header Connection '';
proxy_http_version 1.1;
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 600s;
proxy_connect_timeout 600;
}
5、常用的location规则
(1) /表示直接访问域名则访问站点目录
server {
listen 80;
server_name www.wenqiang.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
(2)当访问http://www.wenqiang.com/index.html 时跳转到 http://www.test.com/index.html(前提示这两个url都真实存在)
server {
listen 80;
server_name www.wenqiang.com;
location / {
proxy_pass http://www.test.com;
}
}
(3) 当访问http://www.wenqiang.com/b/index.html时 跳转到http://www.test.com/b/index.html(前提示这两个url都真实存在)
server {
listen 80;
server_name www.wenqiang.com;
location /b {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://www.test.com;
}
}
(4) 当访问http://www.test.com/b/c/时,系统会去/usr/share/nginx/html/b/c/下面查找index.html,找到则返回结果
server {
listen 80;
server_name www.test.com;
location /b/c/ {
root /usr/share/nginx/html;
index index.html;
}
}
# curl www.test.com/b/c/ # 注意最后要加/
test
(5) 匹配到http://www.wnqiang.com/c/目录时或http://www.wnqiang.com/c/xxx时, 跳转到 http://www.test.com/c/index.html或http://www.test.com/c/xxx(前提是c目录必须存在且跳转的url也存在)
# 匹配任何以 /images/ 开头的任何查询并且停止搜索
server {
listen 80;
server_name www.wenqiang.com;
location ^~ /c/ {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://www.test.com;
}
}