一.反向代理
语法:
Syntax:proxy_pass URL; Default:—— Context:location,if in location,limit_except;
例子:
location ~ /test_proxy$ {
proxy_pass http://127.0.0.1:8080;
}
注意:proxy_pass后面的路径不带url时,其会将location的url传递给后端主机;proxy_pass后面的路径是一个url时,其会将location的url替换为proxy_pass的
如果location定义其uri时使用了正则表达式的模式,或在if语句或limt_execept中使用proxy_pass指令,则proxy_pass之后必须不能使用url; 用户请求时传递的url将直接附加代理到的服务器的之后
二.配置一个正向代理服务器
例子:
location ~ /test_proxy$ { if($http_x_forwarded_for ! ~* "^116\.62\.103\.228"){ return 403; } root /usr/share/nginx/html; index index.html index.htm; }
这里的意思是只允许228这台服务器可以访问。
然后取228这台服务器配置nginx配置
server { listen 80; resolver 8.8.8.8; #指定DNS服务器IP地址,这里写的google location / { proxy_pass http://$http_host$request_uri; #设定代理服务器的协议和地址 } }
nginx实现资源请求
location ~ \.(gif|jpg|png|js|css)$ { #root /home/nginx/images; }