nginx.conf推送反向代理

location /proxy_path/ {
    proxy_pass url;
    client_max_body_size 100m;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Authorization $http_authorization;
    proxy_set_header Host "url";
    proxy_set_header Referer "url";
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
    add_header "Access-Control-Allow-Headers" "authorization,Authorization, Origin, X-Requested-With, Content-Type, Accept";
    add_header "Access-Control-Expose-Headers" "authorization,Authorization, Origin, X-Requested-With, Content-Type, Accept, X-WP-Total, X-WP-TotalPages";
    } 

location

根据URI进行不同的定位。
1.location的基础语法

location [=|~|~*|^~] patt {

}
location uri含义
location =/uri=开头表示精确匹配,只有完全匹配才能生效,并且匹配成功后立即停止其他匹配,然后处理此请求。
location ^~/uri~^开头对表示缀匹配,并在正则之前(不匹配表达式),
location ~ pattern~开头表示区分大小的正则匹配。
location ~*~*开头表示不区分大小的正则匹配。
location /uri不带任何修饰符,表示前缀匹配,并且在正则匹配之后
location /通用匹配,未匹配任何location的请求都会匹配,相当于swich中的default。

注意:
1. 普通字符串的匹配与顺序无关,与长短有关。
2. 正则匹配与顺序有关。

2.location匹配情况:
(1)精确匹配和^~普通字符串前缀匹配

 location =/test/ {
  add_header Content-Type 'text/html; charset=utf-8';
  default_type text/html;
  return 200 '=/uri/匹配';
  }
  location ^~/test/ {
  add_header Content-Type 'text/html; charset=utf-8';
  default_type text/html;
  return 200 '^~/uri/匹配';
  }
  请求url=localhost:port/test/ 返回'=/uri匹配'
  请求url=localhost:port/test/*(aa) 返回'^~/uri/匹配'
  总结:先执行精准匹配先匹配,再执行^~/uri/匹配

(2)精确匹配和/uri/普通前缀匹配

location =/test/ {
  add_header Content-Type 'text/html; charset=utf-8';
  default_type text/html;
  return 200 '=/uri/匹配';
  }

 location /test/{
  add_header Content-Type 'text/html; charset=utf-8';
  default_type text/html;
  return 200 '/uri/匹配';
  }
  请求url=localhost:port/test/ 返回'=/uri/匹配'
  请求url=localhost:port/test/*(aa) 返回'/uri/匹配'
  总结:(2)和(1)可以实现相同uri匹配。

(3)^~/uri/前缀匹配和/uri前缀匹配


  location ^~/test/ {
  add_header Content-Type 'text/html; charset=utf-8';
  default_type text/html;
  return 200 '^~/uri/匹配';
  }
  location /test/{
  add_header Content-Type 'text/html; charset=utf-8';
  default_type text/html;
  return 200 '/uri/匹配';
  }
  重启nginx 出现duplicate(重复) location "/test/" 错误。

(4)普通字符串的匹配和顺序无关与长度有关。

location /test/aaa/ {
   add_header Content-Type 'text/html; charset=utf-8';
   default_type text/html;
   return 200 '/uri/匹配 文件位置1';
  }
  location /test/aaa/bb {
   add_header Content-Type 'text/html; charset=utf-8';
   default_type text/html;
   return 200 '/uri/匹配 文件位置2';
  }
 请求url=localhost:port/test/aaa/bb/返回'/uri/匹配 文件位置2'
 请求url=localhost:port/test/aaa/返回'/uri/匹配 文件位置1'

(5) 正则表达匹配与字符串长度无关与配置文件的定义顺序有关

 location ~/test/aaa/{
   add_header Content-Type 'text/html; charset=utf-8';
   default_type text/html;
   return 200 '~/uri/匹配 文件位置1';
  }
  location ~/test/aaa/bbb/{
   add_header Content-Type 'text/html; charset=utf-8';
   default_type text/html;
   return 200 '~/uri/匹配 文件位置2';
  }
   请求url=localhost:port/test/aaa/bbb/返回'~/uri/匹配 文件位置1',而'~/uri/匹配 文件位置2'不会被请求到。

(6) ^~前缀匹配和正则匹配

 location ^~/test/ {
  add_header Content-Type 'text/html; charset=utf-8';
  default_type text/html;
  return 200 '^~/uri/匹配';
  }
 location ~/test/aaa/{
   add_header Content-Type 'text/html; charset=utf-8';
   default_type text/html;
   return 200 '~/uri/匹配 文件位置1';
  }
请求url=localhost:port/test/aaa/返回'^~/uri/匹配'

(7) ~/uri/正则匹配和/uri/前缀匹配

 location /test/aaa/ {
   add_header Content-Type 'text/html; charset=utf-8';
   default_type text/html;
   return 200 '/uri/匹配 文件位置1';
  }
  location ~/test/aaa/{
   add_header Content-Type 'text/html; charset=utf-8';
   default_type text/html;
   return 200 '~/uri/匹配 文件位置1';
  }
请求url=localhost:port/test/aaa/bbb/返回'~/uri/匹配 文件位置1'

3.location匹配顺序
(1)先精准匹配,匹配成功立即返回。
(2)字符串匹配与字符串长度有关,^~匹配成功立即返回,/uri/匹配在正则之后。
(3)正则匹配从上到下匹配,匹配成功立即返回。

proxy_pass

模块:ngx_http_proxy_module
语法: proxy_pass URL;
场景: location, if in location, limit_excep
说明: 设置后端代理服务器的协议(protocol)和地址(address),以及location中可以匹配的一个可选的URI。协议可以是”http”或”https”。地址可以是一个域名或ip地址和端口,或者一个 unix-domain socket 路径。

1.proxy_pass后,后端服务器的url(request_uri)情况分析

    # 情形A
    # 访问 http://www.test.com/testa/aaaa
    # 后端的request_uri为: /testa/aaaa
    location ^~ /testa/ {
        proxy_pass http://127.0.0.1:8801;
    }
    # 情形B
    # 访问 http://www.test.com/testb/bbbb
    # 后端的request_uri为: /bbbb
    location ^~ /testb/ {
        proxy_pass http://127.0.0.1:8801/;
    }
    # 情形C
    # 下面这段location是正确的
    location ~ /testc {
        proxy_pass http://127.0.0.1:8801;
    }
    # 情形D
    # 下面这段location是错误的
        location ~ /testd {
        proxy_pass http://127.0.0.1:8801/;   # 记住,location为正则表达式时,不能这样写!!!
    }

    # 情形E
    # 访问 http://www.test.com/ccc/bbbb
    # 后端的request_uri为: /aaa/ccc/bbbb
    location /ccc/ {
        proxy_pass http://127.0.0.1:8801/aaa$request_uri;
    }

    # 情形F
    # 访问 http://www.test.com/namea/ddd
    # 后端的request_uri为: /yongfu?namea=ddd
    location /namea/ {
        rewrite    /namea/([^/]+) /yongfu?namea=$1 break;
        proxy_pass http://127.0.0.1:8801;
    }

    # 情形G
    # 访问 http://www.test.com/nameb/eee
    # 后端的request_uri为: /yongfu?nameb=eee
    location /nameb/ {
        rewrite    /nameb/([^/]+) /yongfu?nameb=$1 break;
        proxy_pass http://127.0.0.1:8801/;
    }

总结: proxy_pass URL,URL中是否含有URI,如果不包含,nginx服务器不会改变原地址的URI,如果包含了URI,则nginx服务器会使用新的URI替换原来的URI,会把匹配的路径代理走。

proxy_set_header

定义和添加转发给代理服务器的请求头。该值可以包含文本、变量和它们的组合。在没有定义proxy_set_header时会继承之前定义的值,默认情况只有两个字段被重新定义:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;

header_Keyheader_value作用
X-Real-IP$remote_addr当使用nginx反向代理服务器后,在web后端通过request.getRemoteAddr()(本质获取$remote_addr,该变量封装的是nginx的地址)取得的是nginx的地址。如果想获取客户端的地址可以通过nginx的$remote_addre获取客户端的ip地址,然后后端通过request.getHeader(“X-real-ip”)获取。
X-Forwarded-For$proxy_add_x_forwarded_for增加一个$proxy_add_x_forwarded_for到X-Forwarded-For,X-Forwarded-For默认是空的($http_x_forwarded_for变量就是Forwarded-For变量),$proxy_add_x_forwarded_for变量包含客户端请求头中的”X-Forwarded-For”,与$remote_addr两部分,用逗号隔开。

websocket反向代理配置

location / {
   #添加websocket代理
   proxy_pass http://127.0.0.1:8080;
   proxy_http_version 1.1;
   proxy_set_header upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
}

websocket是html5提出的一个协议规范,该协议本质上是基于tcp的协议,先通过http协议发起请求进行握手然后创建一个实时通信的TCP连接。

header_Keyheader_value作用
upgradewebsocket将通讯协议从http协议升级到websocket协议

跨域与同源

同源政策是由netscape公司于95年引入浏览器。”同源”指的是协议、域名和端口三者相同,主要目的是防止网站恶意窃取其他网站用户的信息,非同源共有三种行为收到限制:1.Cookie、Localstorage和IndexDB无法读取。2.DOM无法获取。3.AJAX请求不能发送。

add_header

语法:add_header name value [always];
name是添加的头名称,value是对应的值,always可选(下面会详细说明)
场景:http, server, location, if in location
说明:用来设置response header

在不添加always的情况下,add_header只对响应码为200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), 308 (1.13.0)这些生效(括号内是nginx版本)。也就是说当服务端返回响应异常,响应码不是上述之一的话,即使nginx有配跨域头信息,浏览器仍然会显示跨域错误。原因就是因为nginx对异常响应码添加add_header无效。

实际工作中往往前端需要捕获服务端异常响应,这时在nginx跨域add_header上加上always,使nginx对任何响应码生效。

header_Keyheader_value作用
Access-Control-Allow-Origin*则允许所有域名的脚本访问该资源
Access-Control-Allow-MethodsGET, POST, OPTIONS, PUT, DELETE服务器允许客户端使用 POST, OPTIONS, PUT, DELETE 方法发起请求。OPTIONS 是 HTTP/1.1 协议中定义的方法,用以从服务器获取更多信息
Access-Control-Allow-Headersauthorization,Authorization, Origin, X-Requested-With, Content-Type, Accept用于 preflight request (预检请求)中,列出了将会在正式请求的 Access-Control-Expose-Headers 字段中出现的首部信息。
Access-Control-Allow-Originauthorization,Authorization, Origin, X-Requested-With, Content-Type, Accept, X-WP-Total, X-WP-TotalPages响应报头指示哪些报头可以公开为通过列出他们的名字的响应的一部分
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值