Home > Software engineering >  How to run several Flask applications on the same server?
How to run several Flask applications on the same server?

Time:01-07

I teach a course with over 40 students, who have to create Python web applications with Flask. I would like them to upload their applications to the department server (running Ubuntu). If all of them upload and run their apps, all except the first one will probably get an error that the port (5000 - the default port for Flask) is in use. I can ask each student to pick a random port number. But I would like the apps to be accessible using the students' names, so that, for example:

http://myserver.com/student1

would link to the application of student1.

Is there a way to do it, which can be done by the students themselves when they submit, so that I do not have to do manual work for each submission?

CodePudding user response:

Yes, it is possible to map the URLs of your students' web applications to their respective ports using a reverse proxy. A reverse proxy server sits in front of your web servers and forward all incoming requests to the appropriate URL

For example, you can set up a reverse proxy on your department server to forward incoming requests to the correct student's application based on the URL. For example, a request to http://myserver.com/student1 could be forwarded to http://localhost:5001, where student1's application is running.

There are several ways to set up a reverse proxy on Ubuntu, but one way to do it is using Nginx. Here is an example configuration file for Nginx that you could use:

server {
listen 80;
server_name myserver.com;

location /student1 {
    proxy_pass http://localhost:5001;
}

location /student2 {
    proxy_pass http://localhost:5002;
}

With this configuration, requests to http://myserver.com/student1 will be forwarded to http://localhost:5001, and requests to http://myserver.com/student2 will be forwarded to http://localhost:5002, and so on.

For more information on how to set up a reverse proxy server check the following link https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

CodePudding user response:

Another possibility is to reverse proxy Unix sockets instead of using TCP ports and use the name of the student as the the actual socket name. For instance in NGINX each could be a location configuration which would make it easier to identify who's who.

server {
    listen 80 default;

    location /student1/ {
        proxy_pass http://student1/;
    }

    location /student2/ {
        proxy_pass http://student2/;
    }
}

upstream student1 {
    server unix:/home/ubuntu/student1;
}

upstream student1 {
    server unix:/home/ubuntu/student2;
}

...etc.
  • Related