反向代理:它是无感知的,只需要将请求发给反向代理服务器,反向代理去后端获取数据响应给客户端,暴露的是代理url,隐藏了真实的ip地址。
例如:使用 nginx 反向代理 www.123.com 直接跳转到127.0.0.1:8080;
通过修改本地 host 文件,将 www.123.com 映射到 127.0.0.1;
在 nginx.conf 配置文件中增加如下配置:
#负载均衡指向的web服务器连接池
upstream www_server_pools {
server 10.211.55.10 weight=1;
server 10.211.55.11 weight=1;
}
server {
listen 80;
server_name www.123.com;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm index.jsp;
}
}
大多数反向代理服务器同时提供负载均衡的功能,管理一组Web服务器,将请求根据负载均衡算法转发到不同Web服务器上,部署简单。
Nginx使用跨域也是非常强大:
location /pub/(.+) { if ($http_origin ~ <允许的域(正则匹配)>) { add_header 'Access-Control-Allow-Origin' "$http_origin"; add_header 'Access-Control-Allow-Credentials' "true"; if ($request_method = "OPTIONS") { add_header 'Access-Control-Max-Age' 86400; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE'; add_header 'Access-Control-Allow-Headers' 'reqid, nid, host, x-real-ip, x-forwarded-ip, event-type, event-id, accept, content-type'; add_header 'Content-Length' 0; add_header 'Content-Type' 'text/plain, charset=utf-8'; return 204; } } # 正常nginx配置 ...... }
Nginx怀可以解决高并发问题,原理