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