-1

I'm using a EC2 from AWS with Nginx.

Let's say I have three domain name: domain1.com, domain2.com, domain3.com. Let's also say that I have three rails application on three different port: 3000, 3001, 3002.

All of these domains are linked to my server on the port 80.

Depending on which URL is requested, I want my port 80 to redirect the request to the correct port.

How should I configure my virtual host file? I know that the If block exist but Nginx seem to not recommend it. My plan is to have multiple sites on my EC2.

I can't make server block on my rails app port since it would block the rails server.

1

Depending on which DOMAIN (?) :

server {
    listen 80;
    server_name .domain1.com; # Wildcard domain
    return 301 $scheme://$host:3000$request_uri; 
    # use $host above because we use a wildcard domain
}

And repeat this server block for all your domains and all your rails apps/ports

1

You've said you want your sites on different ports, and that you want them linked to port 80. This isn't clear.

Nginx can listen on port 80 for different websites, you simply configure it with different domains. For example

server {
  server_name www.example.com;
  listen 80;
  return 301 https://www.example.com$request_uri;
}

server {
  server_name www.example.com;
  listen 443 ssl https;
  root /var/www/site;
  // Insert https stuff here
}

server {
  server_name www.example2.com;
  listen 80;
  root /var/www/site2;
}

server {
  server_name www.example3.com;
  listen 80;
  root /var/www/site3;
}

You could also run a load balancer and multiple nginx instances, but that would be pointless.

4
  • My sites are actually Rails application, so I can't declare a server block for them since it would block the Rails server. I want the port 80 to redirect to another port depending on the URL requested. – François Noël Jan 17 '17 at 18:11
  • You should edit your question to provide enough information for people to answer it. Basically though all you'll need is a proxy_pass within a location block for each server. You do need to define server blocks. Read a rails/Nginx tutorial, it should be fairly trivial, come back and ask specific questions if you can't get it working. – Tim Jan 17 '17 at 18:18
  • How does each domain name find the location directory? Every domain name will be different and formatted like any homepage (e.g google.com). – François Noël Jan 17 '17 at 18:21
  • You need to read an Nginx tutorial. – Tim Jan 17 '17 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.