2.7.4. Proxying traffic to web applications in Python and Go
For business hosting only
The ability to run web applications is only available on businesshosting and is included in the price of the tariff. At change of tariff on shared hosting, web applications will stop working.On the hosting, the ability to proxy HTTP traffic to a local IP address is available, thanks to which you can run applications in Python and Go.
Scheme of work
We give you a unique local IP address of the form 127.*.*.*
, where *
— any number from 0 to 255, and the default port — 3000
. You use these values in your application either by specifying them in the run command or directly in the code.
Request processing order:
- The request goes to the public server on port 80 or 443.
- Based on the hostname, redirects the application to the local IP address.
- The application receives the data, processes it, and returns a response.
The real IP address of the visitor is passed in the header X-REAL-IP
.
In the presence of dedicated IP you can additionally accept requests for it to any port. This allows one application to receive traffic to the site and at the same time receive another type of traffic on any port of the dedicated IP. You can also run the application without proxying requests to the site - take a port on a dedicated IP and process different connections only on it.
Application launch
- V site settings switch «Webserver» on «Traffic proxying» and save the changes:
- Place your application files on the hosting in the right directory.
- In chapter «My sites» in the site menu, click «Web application settings».
- Specify the directory and command to run your application:
- «Launch directory» — the path to the directory from which your application should be launched. If left blank, it will use site root directory.
- «Run command» is the command to run your application. In the run command or in the code of the application itself, use the local IP address and port from the block «Proxy HTTP traffic to the application»:
- Save the changes and test the application:
In the block «Application logs» you can monitor the logs in real time during the launch and operation of the application.
Application launch examples
Attention!
In all commands, instead ofexample.com
use your domain address instead sub
is the name of your subdomain.
Django
Running Django 4.2 on Python 3.10:
- In chapter «Linux configuration» install Python version 3.10 and save the changes.
- Connect to hosting via SSH.
- Install Django:
pip install --user Django
- Change to the subdomain directory where your project files will be located:
cd example.com/sub
- Create a new project named
project
:django-admin startproject project .
- Apply migrations:
python manage.py migrate
- Edit your project settings file
project/settings.py
and in lineALLOWED_HOSTS = []
indicate in square brackets in single quotes your website address'sub.example.com'
. - On the web application settings page, specify the launch command:
python3.10 manage.py runserver 127.*.*.*:3000
In the team instead of
127.*.*.*
and3000
use the IP address and port from the web application settings page. - Save your changes, run the application and test the site.
If successful, the site should display a page with the text «The install worked successfully! Congratulations!».
Flask
Running Flask 2.3 on Python 3.10:
- In chapter «Linux configuration» install Python version 3.10 and save the changes.
- Connect to hosting via SSH.
- Change to the subdomain directory where your project files will be located:
cd example.com/sub
- Create a Python virtual environment:
python -m venv .venv
- Activate the created virtual environment:
. .venv/bin/activate
- Install Flask:
pip install --user Flask
- Deactivate the virtual environment:
deactivate
- Create a file
hello.py
with content like this (example «A Minimal Application» of official documentation):from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<p>Hello, World!</p>"
- On the web application settings page, specify the launch command:
python3.10 -m flask --app hello.py run --host 127.*.*.* --port 3000
In the team instead of
127.*.*.*
and3000
use the IP address and port from the web application settings page. - Save your changes, run the application and test the site.
If successful, the site will display the text «Hello, World!».
Go
Running a simple Go 1.20 application:
- In chapter «Linux configuration» install Go version 1.20 and save the changes.
- Connect to hosting via SSH.
- Change to the subdomain directory where your project files will be located:
cd example.com/sub
- Create a file
simple.go
with content like this (example «Introducing the net/http package (an interlude)» of official documentation):package main import ( "fmt" "log" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) } func main() { http.HandleFunc("/", handler) log.Fatal(http.ListenAndServe("127.*.*.*:3000", nil)) }
In line
log.Fatal(http.ListenAndServe("127.*.*.*:3000", nil))
insteadof127.*.*.*
and3000
use the IP address and port from the web application settings page. - Compile the generated file:
go build simple.go
- Set permissions on the compiled file
750
:chmod 750 simple
- On the web application settings page, specify the launch command:
./simple
- Save your changes, run the application and test the site.
If successful, the site should display the text «Hi there, I love!» with the title of the page you are referring to.