GithubHelp home page GithubHelp logo

franciscoknebel / nginx-reverseproxy Goto Github PK

View Code? Open in Web Editor NEW
47.0 3.0 11.0 296 KB

A simple implementation of a multidomain nginx reverse proxy, using Node apps.

License: MIT License

JavaScript 74.00% HTML 26.00%
proxy nginx-reverseproxy proxy-server nginx nginx-proxy

nginx-reverseproxy's Introduction

nginx-reverseproxy

A simple reverse proxy for hosting multiple apps on the same server. The proxy listens on port 80 (or 443, if using the HTTP2 implementation) and, depending on the domain provided, redirects the user to a specific port, where one Node app is listening.

This implementation mantains applications independent between themselves, making it so one's bad behaviour won't influence the other. Apps can also be paused, restarted or updated in an independent form. Changes to the proxy server won't affect the application behavior, as it will still stay online even if the server is not reachable.

Usage

  1. Install nginx on your server (examples using apt):
  sudo apt-get install nginx
  1. Edit the default server block configuration file:
  sudo nano /etc/nginx/sites-available/default
  1. Delete everything on the file, and include your new proxy server:
  • If you want the regular HTTP, copy the file nginx/default.
  • Otherwise, if you want the HTTP2 version (recommended), use the file nginx/default-http2. Please view the tutorial to setup HTTPS in the Setting up HTTPS section, as it is needed to run HTTP2.

This example creates proxies for three apps, each running on a different domain (check the value of server_name in each server block), and each running on a different proxy_pass port, where your app will be running.

Make sure each app runs on the same port as the one you choose on the proxy_pass.

  1. Once you are sure your apps are running on the correct ports, restart the nginx service and it should be working as expected:
sudo service nginx restart

Setting up HTTPS

  1. Generate your dhparam.pem file, running in the terminal
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048

Create a SSL certificate using Let's Encrypt

How to create a certificate, complete guide.

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

Using Certbot is easier and simpler to configure https://certbot.eff.org/lets-encrypt/ubuntuartful-nginx.

  1. Pause Nginx:
sudo service nginx stop
  1. Install Certbot
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx 
  1. Create certificate for each site (regular domain and with www):
sudo certbot --nginx
  1. Follow the steps on the screen. Certbot is a smart bot :)

  2. Restart Nginx and everything should be working:

sudo service nginx restart

6.1 To renew the certificates manually

sudo certbot renew --dry-run

6.2 To renew automatically

sudo crontab -e

Insert the next two lines.

30 2 * * 1 /usr/bin/certbot renew --dry-run
35 2 * * 1 /bin/systemctl reload nginx

To test your nginx configuration, you can use the following:

sudo nginx -t

This wil check your configuration for correct syntax and then try to open files referred in configuration.


Extra
  • To make it so the user can't access your app ports directly (example1.com:8001), you need to add a hostname to your app.listen instruction. That way, the proxy will be the only one who can access those ports, so it can redirect to the user.
var listener = app.listen(port, 'localhost', function() {
    console.log("Listening on port " + listener.address().port);
});
Original question can be found at DigitalOcean.

License

MIT License. Click here for more information.


nginx-reverseproxy's People

Contributors

franciscoknebel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nginx-reverseproxy's Issues

Update to HTTP2 with HTTPS option

  • Tutorial on how to create a free certificate for HTTPS.
  • Change README.md to explain all the prerequisites.
  • Update nginx configuration to function with HTTP2 and HTTPS.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.