5

I am trying to deploy a next-js app by create-next-app, I have a custom express server like this -

const express = require('express')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const nextApp = next({ dev })
const handle = nextApp.getRequestHandler()

const fs = require('fs')

nextApp.prepare()
.then(() => {
    const server = express ()


    let port = 3000;

    let options = {
        key: fs.readFileSync('some key..', 'utf-8'),
        cert: fs.readFileSync('some cert..', 'utf-8'),
    };


    server.get(
        ...
    )


    let app = https.createServer(options, server)
    .listen((port), function(){
    console.log("Express server listening on port " + port);
    });

})
.catch((ex) => {
    console.error(ex.stack)
    process.exit(1)
})

I want to deploy this as the website when someone types the URL subdomain.maindomain.com so I saved two nginx configuration files like this -

/etc/nginx/sites-available/default AND /etc/nginx/sites-available/subdomain.maindomain.com

the default file contains this

server {

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name maindomain.com www.maindomain.com;

    location / {
            # try_files $uri $uri/ =404;
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }

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

}

and the subdomain.maindomain.com file looks like this

server {
if ($host = www.subdomain.maindomain.com) {
    return 301 https://$host$request_uri;
} # managed by Certbot


if ($host = subdomain.maindomain.com) {
    return 301 https://$host$request_uri;
} # managed by Certbot


    listen 80;
    listen [::]:80;

    root /var/www/subdomain.maindomain.com/somecodefolder/;
    index index.html index.htm index.nginx-debian.html;

    server_name subdomain.maindomain.com www.subdomain.maindomain.com;


    location / {

        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
       # try_files $uri $uri/ =404;
    }

}

if I'm typing https://subdomain.maindomain.com:3000, everything works fine, I see my website running. But when I type https://subdomain.maindomain.com (without the port number) it shows nothing. How can I get the content I want when I type just the url without the port number. I have tried many combinations, but could'nt do. someone please help i've been trying since 2 days.

Prav
  • 610
  • 8
  • 20
  • 1
    Did you try to open port 443 on your host ? What's the result of `sudo netstat -anp | grep listen -i` – Dimitri Kopriwa Jan 06 '19 at 14:18
  • @DimitriKopriwa this is what I get https://imgur.com/a/00SUaAb – Prav Jan 06 '19 at 14:44
  • I opened it in the firewall @DimitriKopriwa – Prav Jan 06 '19 at 14:50
  • 1
    You are trying to access through `https://` so you expect nginx to listen on port `443` as you configured with `listen 443`. It seems that your nginx is not listening on that port. – Dimitri Kopriwa Jan 06 '19 at 14:59
  • I am sorry, I am new to dev-ops, can you please tell me how to enable nginx to listen on port 443? – Prav Jan 06 '19 at 15:09
  • 1
    You did when you configured `listen 443 ssl;` , why is the port not listening I can't tell you, did you run the command as sudo and on the right host ? – Dimitri Kopriwa Jan 06 '19 at 15:13
  • Yes, I am positive about that, I have run this command - sudo yarn run start which does this - set NODE_ENV=production && node server.js server.js has code as I mentioned in my question. – Prav Jan 06 '19 at 15:23
  • 1
    Try to see nginx access/errror log for the vhost that have `server_name maindomain.com www.maindomain.com;` – Dimitri Kopriwa Jan 06 '19 at 15:25
  • And when I ran sudo ufw status, I got this - Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Nginx Full ALLOW Anywhere 22 ALLOW Anywhere 8000 ALLOW Anywhere – Prav Jan 06 '19 at 15:25
  • 3000 ALLOW Anywhere 443 ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Nginx Full (v6) ALLOW Anywhere (v6) 22 (v6) ALLOW Anywhere (v6) 8000 (v6) ALLOW Anywhere (v6) 3000 (v6) ALLOW Anywhere (v6) 443 (v6) ALLOW Anywhere (v6) – Prav Jan 06 '19 at 15:25
  • 1
    Then it seems that port 443 is open, try to verify it with curl `curl -vv -L -H 'Host: maindomain.com ' https://localhost` and `telnet localhost 443` – Dimitri Kopriwa Jan 06 '19 at 15:26
  • hey I tried that, after doing that when i go to url https://subdomain.maindomain.com, I get 502 Bad Gateway --------------------------- nginx/1.10.3 (Ubuntu) – Prav Jan 06 '19 at 15:41
  • @DimitriKopriwa, do you want me to paste the stuff I got when I did curl? – Prav Jan 06 '19 at 15:43
  • When i go to https://subdomain.maindomain.com:3000, I get what I desired – Prav Jan 06 '19 at 15:45
  • @DimitriKopriwa – Prav Jan 06 '19 at 16:16
  • 502 bad gateway means that the the backend service cannot be proxied by nginx. Did you set the Host header correctly? That's the way to let nginx know you are targetting a particular vhost, it's what your browser does when you access a website – Dimitri Kopriwa Jan 07 '19 at 18:15

2 Answers2

4

Try with other applications in order to validate if something is wrong in your application.

The nginx configuration to use domain instead ports are not complex.

Steps

  • npm install
  • node main_domain.js
  • node subdomain.js
  • Check if webs are working:

localhost

  • Add the following lines to your /etc/hosts. This will help us to use domains without the need to pay for a domain in godaddy, namecheap, etc.

127.0.0.1 maindomain.com
127.0.0.1 subdomain.maindomain.com
  • Create a file in /etc/nginx/conf.d called maindomain.com.conf or whatever you want but with .conf

server {
    listen 80;
    server_name maindomain.com;
    
    location / {
        proxy_pass http://localhost:3000/;
    }
}
  • Create a file in /etc/nginx/conf.d called conf.d/subdomain.maindomain.com.conf or whatever you want but with .conf

server {
    listen 80;
    server_name subdomain.maindomain.com;
    
    location / {
        proxy_pass http://localhost:3001/;
    }
}
  • Restart the nginx

service nginx restart
  • And now, you could use a domain instead ip:port

domains

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
2

Try to change from

proxy_pass http://localhost:3000;

Into

proxy_pass http://127.0.0.1:3000;
Joelgullander
  • 1,624
  • 2
  • 20
  • 46