區別在於代理的對象不同。html
Syntax: proxy_pass URL; Default: — Context: location, if in location, limit_except
URL支持:nginx
http://localhost:8000/uri/
https://192.168.1.111:8000/uri/
http://unix:/tmp/backend.socket:/uri/
vim conf.d/real_server.conf
server { # 監聽8080端口 listen 8080; location / { # 配置訪問根目錄爲 /vagrant/proxy root /vagrant/proxy; } }
vim conf.d/fx_proxy.conf
server { # 監聽80端口 listen 80; server_name localhost; location ~ /fx_proxy.html { # 設置反向代理,將訪問 /fx_proxy.html 的請求轉發到 http://127.0.0.1:8080 proxy_pass http://127.0.0.1:8080; } }
/vagrant/proxy/fx_proxy.html
文件<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>反向代理</title> </head> <body> <h1>反向代理</h1> </body> </html>
ss -tln
查看 80 端口和 8080 端口所有開啓[root~]# ss -tln State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:8080 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 10 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::*
http://127.0.0.1/fx_proxy.html
能夠正常訪問[root~]# curl http://127.0.0.1/fx_proxy.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>反向代理</title> </head> <body> <h1>反向代理</h1> </body> </html>
http://127.0.0.1:8080/fx_proxy.html
能夠正常訪問[root~]# curl http://127.0.0.1:8080/fx_proxy.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>反向代理</title> </head> <body> <h1>反向代理</h1> </body> </html>
正向代理須在有公網IP的正式的服務器上測試。
筆者遠程服務器的IP地址爲:39.106.178.166,測試用的域名爲zx_proxy.ws65535.top
vim conf.d/real_server.conf
server { # 監聽80端口 listen 80; # 域名爲 zx_proxy.ws65535.top; server_name zx_proxy.ws65535.top; location / { # $http_x_forwarded_for 能夠記錄客戶端及全部中間代理的IP # 判斷客戶端IP地址是不是 39.106.178.166,不是則返回403 if ($http_x_forwarded_for !~* "^39\.106\.178\.166") { return 403; } root /usr/share/nginx/html; index index.html; } }
http://zx_proxy.ws65535.top/
,返回 403 Forbidden
,說明訪問被拒絕vim conf.d/zx_proxy.conf
server { # 代理服務監聽的端口(注意,必定要看服務器供應商控制檯的安全組是否開啓了該端口) listen 3389; # 配置DNS,223.5.5.5是阿里雲的DNS resolver 223.5.5.5; # 正向代理配置 location / { proxy_pass http://$http_host$request_uri; } }
http://zx_proxy.ws65535.top/
,能夠正常訪問