DEV Community

Fega Suseno
Fega Suseno

Posted on • Edited on

Reverse Proxy Nginx Ubuntu 20.04

Requirement

  • Web Server Running (Apache2 on IP 10.10.10.1:80)
  • Reverse Proxy (Nginx on IP 10.10.10.2)

Assumption

It is assumed that a web server is already running. In this example, the web server runs on IP 10.10.10.1:80.
The reverse proxy server will be running on a different VM or server with IP 10.10.10.2.

Installation

Install Nginx

apt install nginx
Enter fullscreen mode Exit fullscreen mode

Unlink default config nginx

unlink /etc/nginx/sites-enabled/default
Enter fullscreen mode Exit fullscreen mode

Create file reverse-proxy.conf in /etc/nginx/sites-availible

touch /etc/nginx/sites-available/reverse-proxy.conf
Enter fullscreen mode Exit fullscreen mode

Add the following configuration to the file:

server {
    listen 80;
   # listen 443 ssl;
    server_name www.domain.com;
    access_log /var/log/nginx/reverse-proxy-access.log;
    error_log /var/log/nginx/reverse-proxy-error.log;
   # ssl on;
       # include snippets/ssl-defaults.conf;
       # include snippets/proxy-defaults.conf;

    location / {
        #Set header Proxynya
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Accept-Encoding "";

         #Tujuan permintan diteruskan
        proxy_pass http://10.10.10.1/;
    }
}

Enter fullscreen mode Exit fullscreen mode

Then, create a symbolic link for reverse-proxy.conf in sites-enabled:

ln /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf
Enter fullscreen mode Exit fullscreen mode

Check if the configuration is correct:

nginx -t
Enter fullscreen mode Exit fullscreen mode

Restart Nginx and enable it to start automatically on boot:

systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode
systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

Test the setup by accessing the reverse proxy server's IP.
If the configuration is correct, you should see the web server from Server 1.

Happy trying!

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more