Nginx實踐篇(4)- Nginx代理服務 - 正向代理和反向代理

1、代理簡介

1. 代理

代理

2. Nginx代理服務

Nginx代理服務

3. 正向代理和反向代理

區別在於代理的對象不同。html

正向代理代理的對象是客戶端

正向代理代理的對象是客戶端

反向代理代理的對象是服務端

反向代理代理的對象是服務端

4. Nginx代理模塊 ngx_http_proxy_module

語法

Syntax:    proxy_pass URL;
Default:    —
Context:    location, if in location, limit_except

URL支持:nginx

  1. http:http://localhost:8000/uri/
  2. https:https://192.168.1.111:8000/uri/
  3. socket:http://unix:/tmp/backend.socket:/uri/

2、反向代理實例

1. 建立真實要訪問的服務配置:vim conf.d/real_server.conf

server {
    # 監聽8080端口
    listen 8080;

    location / {
        # 配置訪問根目錄爲 /vagrant/proxy
        root /vagrant/proxy;
    }
}

2. 建立反向代理配置 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;
    }
}

3. nginx -s reload 從新載入nginx配置文件

4. 建立 /vagrant/proxy/fx_proxy.html 文件

  • vim /vagrant/proxy/fx_proxy.html
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>反向代理</title>
    </head>
    <body>
        <h1>反向代理</h1>
    </body>
</html>

5. 使用 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                           :::*

6. 使用 curl進行訪問測試

  • 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>

3、正向代理實例

正向代理須在有公網IP的正式的服務器上測試。
筆者遠程服務器的IP地址爲:39.106.178.166,測試用的域名爲 zx_proxy.ws65535.top

1. 在服務器建立真實要訪問的服務配置: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;
    }
}

2. nginx -s reload 從新載入nginx配置文件

3. 在本地使用瀏覽器訪問 http://zx_proxy.ws65535.top/,返回 403 Forbidden,說明訪問被拒絕

enter description here

4. 在服務器建立代理服務配置: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;
    }
}

5. nginx -s reload 從新載入nginx配置文件

6. 瀏覽器配置代理(如下是Windows10的代理配置方式,其餘操做系統自行配置)

  • 控制面板 -> 網絡和Internet -> 代理 -> 手動設置代理

enter description here

7. 設置代理後在本地使用瀏覽器訪問 http://zx_proxy.ws65535.top/,能夠正常訪問

enter description here

相關文章
相關標籤/搜索