50

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?

Rohan Prabhu
  • 7,180
  • 5
  • 37
  • 71

4 Answers4

96

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;
    }   
}
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
  • 2
    I was trying to figure out how to do this in Apache, to no avail. It was faster for me (on Digital Ocean) to create a new droplet, install the LEMP stack, configure the server, clone my application, and write these 7 lines of code than ALL of the hours I wasted trying to figure it out on Apache. Thank you so much!!! – Jonathan May 20 '15 at 16:15
  • @ColeTierney How can I pass subdomain (app.) to flask while using proxy_pass? – Ali Khosro Mar 08 '18 at 06:11
  • 1
    @AliKhosro You could try adding a custom header: `proxy_set_header X-Subdomain app;` – Cole Tierney Mar 09 '18 at 00:26
  • Could you tell me which file should I add the setting to? – user1024 Apr 05 '20 at 07:29
  • @user1024, it is typically found here: /etc/nginx/nginx.conf Usually that config file will include others which might be a better location for your settings. Depends on the setup. You could also just add your own include statement to nginx.conf and put your file in /etc/nginx. Nginx will look there for included files there. – Cole Tierney Apr 05 '20 at 12:29
  • After doing it, `app.example.com` still point to the `example.com`. Is it because I resolve the `app.example.com` to server IP ? – rosefun Dec 01 '20 at 15:16
  • @rosefun I would start with my exact example. If it works, start incrementally adding back the bits that you need; testing after each change. This should help narrow down the problem. – Cole Tierney Dec 02 '20 at 01:21
  • How does this even work? Once *.example.com is resolved to whatever IP, how can the server know which subdomain was used to access it? – Enrico Borba Feb 08 '22 at 08:14
1

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

hassanzadeh.sd
  • 3,091
  • 1
  • 17
  • 26
0

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>
5p0ng3b0b
  • 511
  • 4
  • 10
0

I have ubuntu 16 and nginx with two NodeJS instances, one for front, one for admin. In, I have: /etc/nginx/sites-available/default

I've added:

server { ... location / { proxy_pass http://127.0.0.1:8001; }

location /admin {
    rewrite ^/admin(.*) /$1 break;
    proxy_pass http://127.0.0.1:8002;
}

location /other {
    rewrite ^/other(.*) /$1 break;
    proxy_pass http://127.0.0.1:8003;
}
...

}

I've used this to have access for admin.

Carnaru Valentin
  • 1,690
  • 17
  • 27