Proxy Server

Set Up Reverse Proxy Server

Now that your application is running, and listening on a private IP address, you need to set up a way for your users to access it. We will set up an Nginx web server as a reverse proxy for this purpose. This tutorial will set up an Nginx server from scratch. If you already have an Nginx server setup, you can just copy the location block into the server block of your choice (make sure the location does not conflict with any of your web server's existing content).
On the web server, let's update the apt-get package lists with this command:
  • sudo apt-get update
Then install Nginx using apt-get:
  • sudo apt-get install nginx
Now open the default server block configuration file for editing:
  • sudo vi /etc/nginx/sites-available/default
Delete everything in the file and insert the following configuration. Be sure to substitute your own domain name for the server_name directive (or IP address if you don't have a domain set up), and the app server private IP address for the APP_PRIVATE_IP_ADDRESS. Additionally, change the port (8080) if your application is set to listen on a different port:

Delete everything in the file and insert the following configuration. Be sure to substitute your own domain name for the server_name directive (or IP address if you don't have a domain set up), and the app server private IP address for the APP_PRIVATE_IP_ADDRESS. Additionally, change the port (8080) if your application is set to listen on a different port:

/etc/nginx/sites-available/default
server {
    listen 80;

    server_name example.com;

    location / {
        proxy_pass http://APP_PRIVATE_IP_ADDRESS:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
This configures the web server to respond to requests at its root. Assuming our server is available at example.com, accessing http://example.com/ via a web browser would send the request to the application server's private IP address on port 8080, which would be received and replied to by the Node.js application.


Once you are done adding the location blocks for your applications, save and exit.
On the web server, restart Nginx:
  • sudo service nginx restart
You can add additional location blocks to the same server block to provide access to other applications on the same web server. For example, if you were also running another Node.js application on the app server on port 8081, you could add this location block to allow access to it via http://example.com/app2:
Nginx Configuration — Additional Locations
location /app2 { 
    proxy_pass http://APP_PRIVATE_IP_ADDRESS:8081
    proxy_http_version 1.1; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host; 
    proxy_cache_bypass $http_upgrade; 
 } 
Once you are done adding the location blocks for your applications, save and exit.
On the web server, restart Nginx:

  • sudo service nginx restart

Comentarios

Entradas más populares de este blog

Fix Audio Windows on MAC

Tkinter tkFileDialog module

Using real data types in VHDL