Overview
This article explains how DreamHost's Proxy Server is configured and examples of creating a stand-alone server for Node.js and Python apps.
This Proxy Server can be used to configure an application to run on a non-privileged port without binding the port # to the URL.
Prerequisites
The service is only available on VPS or Dedicated Server hosting plans running either Nginx or Apache.
You must also create an SSH user to configure the examples in this article.
Technical details
DreamHost's Proxy Server is a reverse proxy using Apache mod_proxy.
Ports
-
You can only forward requests to daemons bound to all hosts using ports between 8000-65535. Although ports 80 and 443 cannot be used, the Proxy Server will use your SSL certificate to encrypt all connections.
Firewall and port blocking
-
DreamHost does not employ any sort of firewall or port blocking on its network.
Daemon
-
A daemon is a server process that listens for communication from remote clients. Daemon processes that bind to a port (on which they listen for incoming connections) are visible from the Internet. DreamHost VPS and Dedicated servers permit you to run daemon server processes, but please note they are limited to the server resources (CPU/RAM) of your plan type.
Static assets
-
Static assets (such as CSS, images, video, fonts, and so on) may not load properly due to how the proxy server is configured. To resolve this issue, create a separate subdomain to host these static resources. For example:
- assets.example.com
Then, update the references to these assets in the website you configured with the proxy server to point to their location on the subdomain.
.htaccess rules
-
Due to how Proxy is configured, .htaccess customizations may no longer function (IP management, setting headers, redirects, etc.). If so, the only option is to use your application's software to recreate these rules.
Configuring a Proxy Server
To configure a Proxy Server:
- Navigate to the VPS page.
- To the right of your VPS, click the Manage button.
- Scroll down to the Proxy Server section.
- Enter the following:
-
URL to set up Proxy under: Choose your domain from the dropdown.
path/to/proxy
- Primary directory: To use the primary web directory, leave this field blank.
- Subdirectory: To use a subdirectory, type in its name.
- Port Number to Proxy: Enter the port # you configured your app to use.
-
URL to set up Proxy under: Choose your domain from the dropdown.
- Click the Add Proxy button.
Stand-alone server examples
The following Node.js and Python examples show how to create a simple server to run on port #8002 for you to proxy.
Node.js
- Log into your server via SSH.
- Install a custom version of Node.js.
- In the app directory (example.com), create a file named app.js with the following code. Change the port # in the listen function to control which port the application listens on.
var http = require("http"); var server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("Hello World From Node"); }).listen(8002);
- Start the node server by running the app.js file.
[server]$ node app.js
- Visit the domain (without the port added to the URL) to see the application running.
Python
This example uses Gunicorn, a common standalone server for Python applications.
Prerequisites
Before proceeding, you must first install a custom version of Python. This allows you to create a virtualenv and install custom Python modules locally.
- Log into your server via SSH.
- Activate your virtualenv. When active, the first part of your command prompt changes to the name of your virtualenv.
- Use pip to install Gunicorn:
(venv)[server]$ pip3 install gunicorn
-
Create a Python file to run the site. This example is named myapp.py:
(venv)[server]$ vim myapp.py
- Add the following code to the file:
def app(environ, start_response): data = b"Hello, World From Gunicorn!" start_response("200 OK", [ ("Content-Type", "text/plain"), ("Content-Length", str(len(data))) ]) return iter([data])
- Start Gunicorn by passing in the port # to the following command:
Adding --log-level=debug provides helpful information if an error occurs. You can then share this information when contacting support to help identify the issue.
(venv)[server]$ GUNICORN_CMD_ARGS="--bind=example.com:8002" gunicorn myapp:app --log-level=debug
- Visit the domain and you will see 'Hello, World From Gunicorn!'.