[tcp]php
1.区别在于形式上服务的”对象”不同html
2.正向代理代理的对象是客户端,为客户端服务nginx
3.反向代理代理的对象是服务端,为服务端服务web
反向代理模式与Nginx代理模块总结如表格所示vim
反向代理模式 | Nginx配置模块 |
---|---|
http、websocket、https | ngx_http_proxy_module |
fastcgi | ngx_http_fastcgi_module |
uwsgi | ngx_http_uwsgi_module |
grpc | ngx_http_v2_module |
url
跳转修改返回Location
[不经常使用]后端
参考下载站点:http://test.driverzeng.com/Nginx_File/浏览器
Syntax: proxy_redirect default; proxy_redirect off;proxy_redirect redirect replacement; Default: proxy_redirect default; Context: http, server, location
外网IP | 内网IP | 主机名 |
---|---|---|
10.0.0.5 | 172.16.1.5 | lb01 |
10.0.0.7 | 172.16.1.7 | web01 |
10.0.0.8 | 172.16.1.8 | web02 |
#准备配置文件 [root@web01 ~]# vim /etc/nginx/conf.d/proxy.conf server { listen 80; server_name proxy.drz.com; location / { root /code/proxy; index index.html; } } #建立站点目录 [root@web01 ~]# mkdir /code/proxy #部署代码 [root@web01 ~]# echo 'web01...' > /code/proxy/index.html #重启nginx [root@web01 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 ~]# nginx -s reload
[root@lb01 php]# rpm -ivh nginx-1.16.1-1.el7.ngx.x86_64.rpm [root@lb01 php]#yum -y install nginx
1)配置代理bash
[root@lb01 ~]# vim /etc/nginx/conf.d/daili.conf server { listen 80; server_name proxy.drz.com; location / { proxy_pass http://10.0.0.7; } }
2)建立www用户服务器
3)修改启动用户websocket
4)启动nginx
[root@lb01 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb01 ~]# nginx -s reload
5)访问输入域名浏览器
10.0.0.1请求10.0.0.5的时候使用的是域名
10.0.0.5请求10.0.0.7的时候使用的是IP:port
当访问80端口的时候,没有域名的状况下,默认会去找排在最上面的那个配置文件。
因此咱们须要解决这个问题,保留住最开始的请求头部信息。
[root@lb01 ~]# vim /etc/nginx/conf.d/daili.conf server { listen 80; server_name proxy.drz.com; location / { proxy_pass http://172.16.1.7; proxy_set_header Host $http_host; #请求头部 proxy_http_version 1.1;#长连接 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr;#主配置文件"$http_x_real_ip" #记录客户端来源的ip proxy_connect_timeout 60s;#客户端访问代理超时时间 proxy_read_timeout 60s;#代理响应后端web超时时间 proxy_send_timeout 60s;#web返回代理超时时间 proxy_buffering on; #nignx会把后端返回的内容先放到缓冲区当中,而后再返回给客户端,边收边传, 不是所有接收完再传给客户端 proxy_buffer_size 8k;#设置nginx代理保存用户头信息的缓冲区大小 proxy_buffers 8 8k;#缓冲区大小 } } ~ cd /etc/nginx/ vim proxy_gramms proxy_set_header HOST $http_host; proxy_http_version 1.1; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 60s; proxy_read_timeout 60s; proxy_send_timeout 60s; proxy_buffering on; proxy_buffer_size 8k; proxy_buffers 8 8k; server { listen 80; server_name proxy.drz.com; location / { proxy_pass http://10.0.0.7; include proxy_params; } # location /xxx { # proxy_pass http://10.0.0.7; # include proxy_params; # # } }