DEV Community

Cover image for Simple reverse proxy using docker/nginx
Ethan
Ethan

Posted on

Simple reverse proxy using docker/nginx

Introduction

Hello! I needed to share a local service that was running in one of my Virtual Machines across my company's VPN. I didn't want to set up the connection inside my virtual machine so for this I used Nginx's reverse proxy.


Requirements

  • Docker
  • Docker-Compose

First I created the default config file for Nginx:

# defaults.conf 
server {
  listen [Company VPN IP]];
  server_name [Company VPN IP]];

  location / { 
    include /etc/nginx/includes/proxy.conf;
    proxy_pass [VPN Service Address]]
    allow all;
  }   

  access_log off;
  error_log off;

  charset UTF-8;
}
Enter fullscreen mode Exit fullscreen mode

Pretty simple file, I also didn't need any of the log files so I turned them off.

Next I had to create the proxy config file:

# includes/proxy.conf
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 X-Forwarded-Proto $scheme;
proxy_buffering off;
proxy_request_buffering off;
proxy_http_version 1.1;
Enter fullscreen mode Exit fullscreen mode

Finally the docker files!

# Dockerfile
FROM nginx

COPY ./includes /etc/nginx/includes/
COPY ./default.conf /etc/nginx/conf.d/default.conf
Enter fullscreen mode Exit fullscreen mode
# docker-compose.xml
version: '3' 
services:
  proxy:
    build: ./
    network_mode: 'host'
Enter fullscreen mode Exit fullscreen mode

Pretty simple, as I wasn't using anything on port 80 I decided to set the network_mode to "host".

Finally to build and start!

docker-compose build && docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

With this I was able to view the service running on my virtual vachine via the company's VPN connection. :)

Discussion (0)