随笔- 9  文章- 0  评论- 0  阅读- 37029 

 

 

先通过简单的图来说明一下正向代理和反向代理吧~

正向代理

代理其实就是一个中介,A和B本来可以直连,中间插入一个C,C就是中介。刚开始的时候,代理多数是帮助内网client访问外网server用的(比如HTTP代理),从内到外

 

反向代理

"反向"这个词在这儿的意思其实是指方向相反,即代理将来自外网client的请求forward到内网server,从外到内

 

使用nginx实现反向代理

Nginx只做请求的转发,后台有多个http服务器提供服务,nginx的功能就是把请求转发给后面的服务器,决定把请求转发给谁。

 

1.1.1   安装tomcat

在一个虚拟机上创建两个tomcat实例,模拟多个服务器。

1.1.2   需求

通过访问不同的域名访问运行在不同端口的tomcat

www.hudada.com   访问运行8080端口的tomcat

www.helloworld.com   访问运行8081端口的tomcat

1.1.3   域名需要配置host文件:

 

 

 

1.1.4   Nginx的配置

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
upstream tomcatserver1{
  server 192.168.0.11:8080;
  }
 
  upstream tomcatserver2{
  server 192.168.0.11:8081;
  }
 
  server {
      listen       80;
      server_name  www.hudada.com;
 
      #charset koi8-r;
 
      #access_log  logs/host.access.log  main;
 
      location / {
          proxy_pass   http://tomcatserver1;
          index  index.html index.htm;
      }
 
      #error_page  404              /404.html;
 
      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
 
      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      #    proxy_pass   http://127.0.0.1;
      #}
 
      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      #location ~ \.php$ {
      #    root           html;
      #    fastcgi_pass   127.0.0.1:9000;
      #    fastcgi_index  index.php;
      #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
      #    include        fastcgi_params;
      #}
 
      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #    deny  all;
      #}
  }
 
 
  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #    listen       8000;
  #    listen       somename:8080;
  #    server_name  somename  alias  another.alias;
 
  #    location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}
 
 
  # HTTPS server
  #
  #server {
  #    listen       443 ssl;
  #    server_name  localhost;
 
  #    ssl_certificate      cert.pem;
  #    ssl_certificate_key  cert.key;
 
  #    ssl_session_cache    shared:SSL:1m;
  #    ssl_session_timeout  5m;
 
  #    ssl_ciphers  HIGH:!aNULL:!MD5;
  #    ssl_prefer_server_ciphers  on;
 
  #    location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}
 
 
  server {
      listen       80;
      server_name  www.helloworld.com;
 
      #charset koi8-r;
 
      #access_log  logs/host.access.log  main;
 
      location / {
          proxy_pass   http://tomcatserver2;
          index  index.html index.htm;
      }
 
      #error_page  404              /404.html;
 
      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
 
      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      #    proxy_pass   http://127.0.0.1;
      #}
 
      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      #location ~ \.php$ {
      #    root           html;
      #    fastcgi_pass   127.0.0.1:9000;
      #    fastcgi_index  index.php;
      #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
      #    include        fastcgi_params;
      #}
 
      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #    deny  all;
      #}
  }
 
 
  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #    listen       8000;
  #    listen       somename:8080;
  #    server_name  somename  alias  another.alias;
 
  #    location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}
 
 
  # HTTPS server
  #
  #server {
  #    listen       443 ssl;
  #    server_name  localhost;
 
  #    ssl_certificate      cert.pem;
  #    ssl_certificate_key  cert.key;
 
  #    ssl_session_cache    shared:SSL:1m;
  #    ssl_session_timeout  5m;
 
  #    ssl_ciphers  HIGH:!aNULL:!MD5;
  #    ssl_prefer_server_ciphers  on;
 
  #    location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}

 

 

如果在同一个域名下有多台服务器提供服务,此时需要nginx负载均衡。

 

 

只需操作这几个简单的步骤,nginx的反向代理就完成了。

 posted on   骑着毛驴上高速  阅读(592)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· .NET 8.0 + Linux 香橙派,实现高效的 IoT 数据采集与控制解决方案
· .NET中 泛型 + 依赖注入 的实现与应用
点击右上角即可分享
微信分享提示