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!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs