Reverse proxy of Nginx

Chapter 1: why to use reverse proxy? What software can be used?

Reverse agent software
Hardware: F5
Software: Nginx HAproxy LVS, etc

Chapter 2: explanation of Nginx reverse proxy parameters

Official case:
location / {
    proxy_pass       http://localhost:8000;
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
}
Interpretation:
proxy_pass       http://172.16.1.7:8080; forward the user's request to the backend nginx server for processing    
proxy_set_header Host      $host;                #Record the user's access address to their own HOST record
proxy_set_header X-Real-IP $remote_addr;        #Record the IP accessed by users to their own IP sector

//Common agent configuration parameters:
proxy_set_header Host $http_host;                    #Record the user's access address to their own HOST record    
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    #Record user access IP to their own X-Forwarded-For sector
proxy_connect_timeout 30;                            
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;

Chapter 3: Nginx reverse proxy configuration

Demand:
web01 8080 web01
LB 80 jumps to 8080 on web01

web server operation:

1. Configure nginx file

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

2. Write test page

echo "web01" > /code/www.html

3. Check and restart Nginx

nginx -t
systemctl restart nginx 

Load server operation

1. Install NGINX

yum -y install nginx  #Install official version

2. Write the parameter configuration file required by the agent

[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. Write agent service configuration file

[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. Check and start Nginx

nginx -t
systemctl start nginx 

windows operation:
Bind hosts file
10.0.0.5 www.mysun.com

Chapter 4: configuration of reverse agent combined with load balancing

1. Address planning

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

Load lb01

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

2. Configure the web server

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. Configure lb server

[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 operation:

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

Tags: Web Server Nginx Windows yum

Posted on Sat, 02 Nov 2019 07:58:28 -0700 by CanMan2004