Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect subdomain to port [nginx/flask]

Tags:

nginx

I know that this is a common question, and there are answers for the same, but the reason I ask this question is because I do not know how to approach the solution. Depending on the way I decide to do it, the solution I can pick changes. Anyways,

I have an AWS EC2 instance. My DNS is handled by Route53 and I own example.com. Currently, on my instance, there are two services running:

example.com:80 [nginx/php/wordpress]
example.com:8142 [flask]

What I want to do is, make app.example.com point to example.com:8142. How exactly do I go about doing this? I am pretty sure that I will have to point app.example.com to the same IP as example.com, since it is the same box that will be serving it. And, nginx will be the first one to handle these requests at port 80. Is there a way with which I can make nginx forward all requests to localhost:8142?

Is there a better way that I can solve this problem?

like image 841
Rohan Prabhu Avatar asked May 14 '14 08:05

Rohan Prabhu


3 Answers

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;
    }   
}
like image 192
Cole Tierney Avatar answered Nov 16 '22 04:11

Cole Tierney


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>
like image 35
5p0ng3b0b Avatar answered Nov 16 '22 05:11

5p0ng3b0b


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

like image 1
hassanzadeh.sd Avatar answered Nov 16 '22 04:11

hassanzadeh.sd