Nginx做为代理服务.正向代理:代理对象为客户端.反向代理:代理对象为服务端.
配置语法:php
Syntax:proxy_pass URL
Default:--
Context:location、if in location、limit_except
配置实例:html
#server1 server { ... listen 8080; server_name localhost; ... location / { root /opt/htdocs/html; index index.html index.htm index.php; } } #server2 server { listen 80; server_name localhost; ... location ~/reg$ { proxy_pass http://127.0.0.1:8080; // 反向代理8080 } }
若是咱们只容许某一个特定的ip访问,则可要考虑使用正向代理来实现。nginx
客户端服务配置实例:浏览器
server { listen 80; server_name www.mantis.me; ... location / { if ($http_x_forwarded_for !~* "114\.249\.225\.223") { // 只容许114.249.225.223访问 return 403; } } }
114.249.225.233服务器配置:服务器
server { listen 80; server_name www.mantis.me; ... resolver 8.8.8.8; // dns location / { proxy_pass http://$http_host$request_uri; } }
客户端使用代理工具配置代理服务器,例如mac系统自带、google扩展工具SwitchySharp等,配置相应的http代理服务器地址。工具
在浏览器输入www.mantis.me便可访问。google