Redirect subdomain to port [nginx/flask]

You can redirect your domain to a certain port. This depends on the web service you are using -Nginx/Apache. If you are using Nginx, you’ll need to do add a server block to your Nginx’s website config. This can be achieved by using the bellow

location /{
    proxy_pass  http://127.0.0.1:8142/;
}

If you are using Apache, you have two options, the first one is to add a redirection rule in your website’s .htaccess and the second one would be to do it directly in the Apache’s Vhost file. I like using the first option. In your .htaccess file, you can add the following rule

RewriteEngine on

# redirect to 3000 if current port is not 3000 and "some-prefix/" is matched
RewriteRule ^/(.*[^/])/?$ http://blabla:3000/$1/ [R=301,L]

If you want to use Apache’s Vhost file, I’ll recommend going through the following tutorial link


You could add a virtual host for app.example.com that listens on port 80 then proxy pass all requests to flask:

server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://localhost:8142;
    }   
}

This is how you would do it with apache.

$cat /etc/apache2/sites-available/app.conf
<VirtualHost *:80>
    ServerName app.example.com
    ProxyPreserveHost On
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>
    ProxyPass / http://localhost:8142/
    ProxyPassReverse / http://localhost:8142/
</VirtualHost>

Tags:

Nginx