我们都知道nginx的性能很高,尤其是作为代理服务器的时候,nginx并发能力、内存占用非常有优势,那么怎样去使用nginx的代理功能呢?
1.什么是nginx
参见nginx系列
2.nginx代理
2.1nginx代理原理
(图片来源见水印)
简单一句话:代理服务器作为中间人,帮用户取得数据。
2.2nginx代理安全吗
2.2.1nginx可以过滤http请求
2.2.2nginx可以限制用户请求(速度、频率等)
2.2.3nginx可以规划整套系统架构提升安全等级
........
2.3nginx代理实现
location / {
proxy_pass https://www.abc.com; #目标地址
proxy_http_version 1.1; #http版本#设置头信息
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_redirect off;
}
常用的proxy属性
proxy_set_header 设置头信息
proxy_pass 目标地址
2.3.2 正则匹配静态资源
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
proxy_pass https://www.abc.com;
expires 30d;
proxy_set_header Host $http_host;
}
location ~ .*\.(js|css)?$
{
proxy_pass https://www.abc.com;
expires 12h;
proxy_set_header Host $http_host;
}