Realization of Single Machine Reverse Proxy in Nginx Intranet

  • 2021-11-10 11:13:28
  • OfStack

Directory 1 Nginx Installation 2 Configure Nginx 3 Modify the hosts file 4 Test

Nginx Intranet Stand-alone Reverse Proxy

Ubuntu18.04 Virtual Machine 1 IP: 192.168. 10.10
Ubuntu18.04 Virtual Machine 2 IP: 192.168. 10.11

Test purpose: Deploy Nginx server on virtual machine 1 (192.168. 10.10: 80), access your own domain name through browser, and reverse proxy to intranet virtual machine 2 (192.168. 10.11: 1234).

Virtual Machine 2 should be accessed by browser originally, and the display interface is different from Nginx, such as installing an tomcat.

1 Nginx Installation

As the Nginx server, Virtual Machine 1 is installed as follows:


sudo apt-get install build-essential
sudo apt-get install libtool
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install openssl
sudo apt-get update
sudo apt-get install nginx

systemctl status nginx Verify that the installation was successful.


$ systemctl status nginx
 ●  nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: en
   Active: active (running) since Mon 2021-11-01 10:07:38 CST; 4h 19min ago
     Docs: man:nginx(8)
 Main PID: 8915 (nginx)
    Tasks: 3 (limit: 9461)
   CGroup: /system.slice/nginx.service
            - 8915 nginx: master process /usr/sbin/nginx -g daemon on; master_pro
            - 8916 nginx: worker process
            Off- 8917 nginx: worker process
...

2 Configure Nginx

Virtual Machine 1:

/etc/nginx/nginx. conf is the main configuration file, which has not been studied in detail, but please note that the following contents are uncommented:


include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

After modification, service nginx restart restarts Nginx.

Create/etc/nginx/conf. d/test. conf as follows:


server {
  listen 80; #Nginx Server listening port 
  charset     utf-8;
  server_name test.com; # Domain name 

  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Nginx-Proxy true;

    proxy_pass http://192.168.10.11:1234/; # Reverse proxy IP+ Port 
    proxy_redirect off;
  }
}

proxy_pass http://192.168. 10.10: 1234/; In this line, don't forget to add the last '/' slash 1.

nginx-s reload Update Configuration

Principle of reload:
1 First check whether the configuration syntax is wrong
2 The main process tries to apply the configuration
3 If successful: Start the new worker process and close the old process
4 If Failure: Main Process Configuration Rollback

3 Modify the hosts file

Virtual Machine 1 adds in the/etc/hosts file:

192.168.10.10  test.com

4 Test

Type 192.168. 10.10 in the browser and the text for Nginx is displayed, which is the Nginx effect of Virtual Machine 1 itself.

Enter test. com, which is the application content of Virtual Machine 2, indicating that the reverse proxy is successful.


Related articles: