Score:0

1 Gateway 2 Machines 2 Domains 3 Subdomains, How to

ug flag

I had a working configuration in nginx for just one of my websites, but I broke it when I tried to make it work with 2 different domains, one of which has 2 sub domains, all serving different sites or applications. To make matters harder on me, the domain running 2 apps is on a separate machine, and I am trying to proxy requests for that domain to the correct machine on my LAN. See below:

My Architecture

My NGINX config is a disaster, but is as follows:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /home/pi/sites/main;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html domain1_index.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
}

server {

    root /home/pi/sites/main;

    index index.html index.htm index.nginx-debian.html;
    server_name internal.domain1.info; # managed by Certbot


    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/internal.domain1.info/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/internal.domain1.info/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot



}
server {
    if ($host = internal.domain1.info) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 ;
    listen [::]:80 ;
    server_name internal.domain1.info;
    return 404; # managed by Certbot


}

server {
  server_name shiba.com www.shiba.com whispering.shiba.com;
  location / {
    proxy_pass http://<machine2'sIP>:8888;
  }
}

server {
  server_name yelling.shiba.com;
  location / {
    proxy_pass http://<machine2'sIP>:8555;
  }
}

How can I get this to serve websites as specified in my picture? Thanks.

Edit: My proposed new configuration

|sites-available | symlink --> | sites-enabled
   conf1         |             |    conf1
#https website
server {
    root /home/pi/sites/main;

    index index.html index.htm index.nginx-debian.html;
    
    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/internal.domain1.info/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/internal.domain1.info/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
#http website redirect
server {
    if ($host = internal.domain1.info) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80 ;
    listen [::]:80 ;
    server_name internal.domain1.info;
    return 404; # managed by Certbot
}
|sites-available | symlink --> | sites-enabled
   conf2         |             |    conf2
server {
    listen 80 ;
    listen [::]:80 ;
    server_name whispering.shiba.info;

    return 301 http://xxx.xxx.x.xx:8555;
}
|sites-available | symlink --> | sites-enabled
   conf3         |             |    conf3
server {
    listen 80 ;
    listen [::]:80 ;
    server_name yelling.shiba.info;

    return 301 http://xxx.xxx.x.xx:8888;
}
drookie avatar
za flag
You're using default catchall `server{}` along with `server {}` that don't have `listen`. Thats probably why nothing is working properly. And yeah, part of the cause is that your config is a mess. Looks like you hate UNIX and nginx in particular. Everything looks so temporary and demoish.
WhisperingShiba avatar
ug flag
@drookie It is demoish, it is my first config on my first project. I wanted to hack my existing config, but I clearly don't know enough. Do you suggest that I make seprate configs in the sites-available and symlink to sites enabled, or should I have one config with 3 servers for my whole architecture, or 1 config, 1 server and 3 locations? I am having a hard time even searching for the preferred method of setting up a system like this.
Nikita Kipriyanov avatar
za flag
Each server (vitualhost) should be put into its own config file. Never serve target sites with default server; it's should be used only as a place holder for catching misconfiguration. It is not as important if you put files into sites-available and then symlink them, but this is preferred method because it makes accidental config deletion less possible.
WhisperingShiba avatar
ug flag
@NikitaKipriyanov I edited my question with my proposed configurations. My intuition is that this wont work since I am listening on port 80 3 times, and hence nginx won't even start. Additionally, I'm not sure if just asserting server name will direct requests for yelling.shiba.info to xxx.xxx.x.xx:8555. Do I need to do if ($host == yelling.shiba.info> { return 301 http://xxx.xxx.x.xx:PORT } as I show in my example? Thanks.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.