代理概述

正向代理代理的对象是客户端,反向代理代理的是服务端,Nginx即可以实现正向代理,也可以实现反向代理。
正向代理
image.png
反向代理
image.png

正向代理案例

理论上Nginx可以支持正向代理上网,但是在实验中,域名访问有问题,尚未解决,只能使用ip上网

  • nginx配置 ```nginx

server { resolver 8.8.8.8; listen 8078; location / { proxy_pass $scheme://$http_host$request_uri; } }

  1. - 浏览器配置
  2. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/22353188/1629265001086-ae081026-01f1-4052-a7be-e705c96e0763.png#height=485&id=Zdw7m&margin=%5Bobject%20Object%5D&name=image.png&originHeight=485&originWidth=487&originalType=binary&ratio=1&size=30515&status=done&style=none&width=487)
  3. - 通过ip访问
  4. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/22353188/1629265031017-fafd6a89-4c47-4136-b0ce-a975aef09293.png#height=310&id=S9Q8Y&margin=%5Bobject%20Object%5D&name=image.png&originHeight=414&originWidth=1103&originalType=binary&ratio=1&size=90338&status=done&style=none&width=827)
  5. - 总结
  6. 客户端访问服务端,但是如果使用了代理,那么服务端能看到的只是代理发送过去的请求,也即$remote_addr变成代理服务器的ip
  7. <a name="fut7M"></a>
  8. # 反向代理的配置
  9. Nginx反向代理模块的指令是由`ngx_http_proxy_module`模块进行解析,该模块在安装Nginx的时候已经自己加装到Nginx中了。
  10. <a name="ph3GB"></a>
  11. ## proxy_pass指令
  12. 用来设置被代理服务器地址,可以是主机名称、IP地址加端口号形式;<br />URL:为要设置的被代理服务器地址,包含传输协议(`http`,`https://`)、主机名称或IP地址加端口号、URI等要素。
  13. | **语法** | **proxy_pass URL;** |
  14. | --- | --- |
  15. | 默认值 | |
  16. | 位置 | location |
  17. - 案例
  18. ```nginx
  19. # 172.41.100.15 nginx配置
  20. server {
  21. listen 8078;
  22. location / {
  23. proxy_pass http://172.41.100.14:8090;
  24. }
  25. location /proxy {
  26. proxy_pass http://172.41.100.14:8090;
  27. }
  28. location /proxy2 {
  29. proxy_pass http://172.41.100.14:8090/;
  30. }
  31. }
  32. # 172.41.100.14 nginx配置
  33. server {
  34. listen 8090;
  35. location / {
  36. root html;
  37. index index.html;
  38. }
  39. }
  • 结果
  1. 访问http://172.41.100.15:8078

页面正常展示
image.png

  1. 访问http://172.41.100.15:8078/proxy

页面报404,因为配置的路径没有加/,nginx去172.41.100.14服务器的nginx安装目录下的html/proxy找index.html页面

  1. 2021/08/18 13:53:15 [error] 18911#0: *55 open() "/usr/local/nginx/html/proxy" failed (2: No such file or directory), client: 172.41.100.15, server: , request: "GET /proxy HTTP/1.0", host: "172.41.100.14:8090"

image.png

  1. 访问http://172.41.100.15:8078/proxy2

页面正常展示
image.png

proxy_set_header指令

用于更改Nginx服务器接收到的客户端请求的请求头信息,然后将新的请求头发送给代理的服务器

语法 proxy_set_header field value;
默认值 proxy_set_header Host $proxy_host;proxy_set_header Connection close;
位置 http、server、location
  • 案例 ```nginx

    172.41.100.15 nginx配置

    server {

    防止中文乱码

    charset utf-8; listen 8078; location / { proxy_set_header songhongwei 宋宏伟好帅; proxy_pass http://172.41.100.14:8090; } }

172.41.100.14 nginx配置

server {

防止中文乱码

charset utf-8; listen 8090; location / { default_type text/plain; return 200 $http_songhongwei; } }

  1. - 访问`http://172.41.100.15:8078/`效果
  2. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/22353188/1629266581313-1529a644-efd7-441f-a4e5-69362213f96f.png#height=132&id=hXE5c&margin=%5Bobject%20Object%5D&name=image.png&originHeight=132&originWidth=574&originalType=binary&ratio=1&size=7046&status=done&style=none&width=574)
  3. <a name="F0wbF"></a>
  4. ## proxy_redirect指令
  5. 用来重置头信息中的"Location""Refresh"的值,目的是为了隐藏服务端信息,否则客户端可以获取服务端相关信息。<br />当上游服务器返回的响应是重定向或刷新请求(如HTTP响应码是301或者302)时,proxy_redirect可以重设HTTP头部的locationrefresh字段。
  6. | **语法** | **proxy_redirect redirect replacement;**<br />**proxy_redirect default;**<br />**proxy_redirect off;** |
  7. | --- | --- |
  8. | 默认值 | proxy_redirect default; |
  9. | 位置 | httpserverlocation |
  10. - proxy_redirect redirect replacement
  11. `redirect:目标,Location的值`<br />`replacement:要替换的值`
  12. - proxy_redirect default;
  13. `将location块的uri变量作为replacement,`<br />`将proxy_pass变量作为redirect进行替换`
  14. - proxy_redirect off;
  15. `关闭proxy_redirect的功能`
  16. - 案例
  17. ```nginx
  18. # 172.41.100.15 nginx 配置
  19. server{
  20. listen 8079;
  21. server_name localhost;
  22. location /{
  23. proxy_pass http://172.41.100.14:8090;
  24. proxy_redirect http://172.41.100.14 http://172.41.100.15;
  25. }
  26. location /proxy {
  27. proxy_pass http://172.41.100.14:8090;
  28. }
  29. }
  30. server {
  31. listen 80;
  32. server_name localhost;
  33. location / {
  34. proxy_pass http://172.41.100.14;
  35. }
  36. }
  37. # 172.41.100.14 nginx配置
  38. server {
  39. server_name localhost;
  40. listen 8090;
  41. location / {
  42. return 302 http://172.41.100.14;
  43. }
  44. }
  45. server {
  46. listen 80;
  47. server_name localhost;
  48. location /{
  49. root html;
  50. index index.html;
  51. }
  52. }
  1. 访问http://172.41.100.15:8079/proxy

地址栏被重定向为http://172.41.100.14/,响应头的Location返回了服务端的信息。
image.png

  1. 访问http://172.41.100.15:8079/

地址栏被重定向为http://172.41.100.15/,响应头的Location未返回服务端的信息。
image.png

反向代理配置案例

image.png

代理的服务器内容不一致

如果服务器1、服务器2和服务器3的内容不一样,那我们可以根据用户请求来分发到不同的服务器。

  1. # 172.41.100.15 nginx配置,用不同的location来区分请求,这里用端口来区分服务器,实际生产可能对应多台不同的nginx主机
  2. server {
  3. listen 8082;
  4. server_name localhost;
  5. location /server1 {
  6. proxy_pass http://172.41.100.14:9001/;
  7. }
  8. location /server2 {
  9. proxy_pass http://172.41.100.14:9002/;
  10. }
  11. location /server3 {
  12. proxy_pass http://172.41.100.14:9003/;
  13. }
  14. }
  15. # 172.41.100.14 nginx配置
  16. server {
  17. listen 9001;
  18. server_name localhost;
  19. default_type text/html;
  20. return 200 '<h1>172.41.100.14:9001</h1>';
  21. }
  22. server {
  23. listen 9002;
  24. server_name localhost;
  25. default_type text/html;
  26. return 200 '<h1>172.41.100.14:9002</h1>';
  27. }
  28. server {
  29. listen 9003;
  30. server_name localhost;
  31. default_type text/html;
  32. return 200 '<h1>172.41.100.14:9003</h1>';
  33. }
  • 访问http://172.41.100.15:8082/server1 返回172.41.100.14:9001
  • 访问http://172.41.100.15:8082/server2返回172.41.100.14:9002
  • 访问http://172.41.100.15:8082/server3返回172.41.100.14:9003

    代理的服务器内容一致

    代理服务内容一致,nginx相当于负载均衡器,这里先给出一个例子,具体的在负载均衡里详细说明 ```nginx

    172.41.100.15 nginx配置,用不同的location来区分请求,这里用端口来区分服务器,实际生产可能对应多台不同的nginx主机

    upstream backend{
    1. server 172.41.100.14:9001;
    2. server 172.41.100.14:9002;
    3. server 172.41.100.14:9003;
    } server {
    1. listen 8083;
    2. server_name localhost;
    3. location /{
    4. proxy_pass http://backend;
    5. }
    }

172.41.100.14 nginx配置

server { listen 9001; server_name localhost; default_type text/html; return 200 ‘

172.41.100.14:9001

‘; } server { listen 9002; server_name localhost; default_type text/html; return 200 ‘

172.41.100.14:9002

‘; } server { listen 9003; server_name localhost; default_type text/html; return 200 ‘

172.41.100.14:9003

‘; } ```

  • 访问http://172.41.100.15:8083/并不断刷新,可以看到页面的内容在变化

image.pngimage.pngimage.png