nginx實現反向代理

[tcp]php

nginx實現反向代理

nginx常見的代理模型

  • 正向代理

img

  • 反向代理

img

區別

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

nginx反向代理配置語法

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

實戰案例:

部署web服務器反向代理

環境準備

外網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

1.web01部署網站

#準備配置文件
[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

2.在lb01上安裝nginx

[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;
#
#       }
}
相關文章
相關標籤/搜索