3 Reverse proxy with Nginx
Perflyst edited this page 2020-03-23 12:15:37 +01:00

To expose the locally running service to the internet you need to configure a reverse proxy. Depending on how you run the service (Docker, Go binary) you need to change the port in the config below.

You also need to obtain a SSL certificate from for example Let's Encrypt with certbot.

IPv4 and IPv6 is intentionally separate, otherwise it is not possible to forward the IPv6 address from the client to the backend. If you do not have IPv6 please remove the IPv6 blocks from the virtual host below.

# IPv4 plain HTTP
server {
  listen 80;

  server_name librespeed.example.org;

  access_log /dev/null;
  error_log /dev/null;

  location / {
    proxy_pass http://127.0.0.1:<port>;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  client_max_body_size 21M;
}

# IPv6 plain HTTP
server {
  listen [::]:80;

  server_name librespeed.example.org;

  access_log /dev/null;
  error_log /dev/null;

  location / {
    proxy_pass http://[::]:<port>;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  client_max_body_size 21M;
}



# IPv4 encrypted HTTPS
server {
  listen 443 ssl http2;

  server_name librespeed.example.org;

  access_log /dev/null;
  error_log /dev/null;

  ssl_certificate     /path/to/fullchain.pem;
  ssl_certificate_key /path/to/privkey.pem;
  ssl_trusted_certificate /path/to/chain.pem;

  location / {
    proxy_pass http://127.0.0.1:<port>;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  client_max_body_size 21M;
}

# IPv6 encrypted HTTPS
server {
  listen [::]:443 ssl http2;

  server_name librespeed.example.org;

  access_log /dev/null;
  error_log /dev/null;

  ssl_certificate     /path/to/fullchain.pem;
  ssl_certificate_key /path/to/privkey.pem;
  ssl_trusted_certificate /path/to/chain.pem;

  location / {
    proxy_pass http://[::]:<port>;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  client_max_body_size 21M;
}