天天看點

Nginx的反向代理(三)

第一章:為什麼要用反向代理,有哪些軟體可以使用?

反向代理軟體

硬體:F5

軟體:Nginx HAproxy LVS等

第二章: Nginx反向代理參數解釋

官方案例:
location / {
    proxy_pass       http://localhost:8000;
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
}           
解釋:
proxy_pass       http://172.16.1.7:8080;        #将使用者的請求轉發給後端的nginx伺服器處理    
proxy_set_header Host      $host;                #将使用者通路的位址記錄到自己的HOST記錄上
proxy_set_header X-Real-IP $remote_addr;        #将使用者通路的IP記錄到自己的IP地段上

常用的代理配置參數:
proxy_set_header Host $http_host;                    #将使用者通路的位址記錄到自己的HOST記錄上    
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    #将使用者通路IP記錄到自己的X-Forwarded-For地段上
proxy_connect_timeout 30;                            
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;           

第三章:Nginx反向代理配置

需求:

web01 8080 web01

lb 80 跳轉到web01上的8080

web伺服器操作:

1.配置nginx檔案

[root@web01 ~]# cat /etc/nginx/conf.d/web01.conf 
server {
  listen 8080;
  server_name www.mysun.com;
  location / {
     root /code;
     index www.html;
  }
}           

2.寫入測試頁面

echo "web01" > /code/www.html           

3.檢查并重新開機Nginx

nginx -t
systemctl restart nginx            

負載伺服器操作

1.安裝NGINX

yum -y install nginx  #安裝官方版本           

2.編寫代理需要的參數配置檔案

[root@lb01 ~]# cat /etc/nginx/proxy_params 
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;           

3.編寫代理服務配置檔案

[root@lb01 ~]# cat /etc/nginx/conf.d/proxy.conf 
server {
   listen 80;
   server_name www.mysun.com;

   location / {
      proxy_pass  http://172.16.1.7:8080;
      include proxy_params;
   }
}           

4.檢查并啟動Nginx

nginx -t
systemctl start nginx            

windows操作:

綁定hosts檔案

10.0.0.5 www.mysun.com

第四章:反向代理結合負載均衡配置

1.位址規劃

web01    www.mysun.com  blog.mysun.com  bbs.mysun.com 
web02   www.mysun.com  blog.mysun.com  bbs.mysun.com            

負載lb01

www.mysun.com

blog.mysun.com

bbs.mysun.com

2.配置web伺服器

cat >/etc/nginx/conf.d/www.conf <<EOF
server {
  listen 80;
  server_name www.mysun.com;
  location / {
     root /code;
     index www.html;
  }
}
EOF

cat >/etc/nginx/conf.d/bbs.conf <<EOF
server {
  listen 80;
  server_name bbs.mysun.com;
  location / {
     root /code;
     index bbs.html;
  }
}
EOF

cat >/etc/nginx/conf.d/blog.conf <<EOF
server {
  listen 80;
  server_name blog.mysun.com;
  location / {
     root /code;
     index blog.html;
  }
}
EOF

echo "$(hostname) bbs" > /code/bbs.html
echo "$(hostname) www" > /code/www.html
echo "$(hostname) blog" > /code/blog.html

nginx -t 
systemctl restart nginx            

3.配置lb伺服器

[root@lb01 ~]# cat /etc/nginx/conf.d/proxy.conf 
upstream web_pools {
   server 172.16.1.7;
   server 172.16.1.8;
}

server {
   listen 80;
   server_name www.mysun.com bbs.mysun.com blog.mysun.com;
   location / {
      proxy_pass  http://web_pools;
      include proxy_params;
   }
}

nginx -t 
systemctl restart nginx            

4.windows操作:

10.0.0.5 www.mysun.com bbs.mysun.com blog.mysun.com

繼續閱讀