Question

Express js ip canonicalization

Posted August 21, 2021 134 views
Node.js

Im using MERN stack to build a web app. and its being hosted on a vps, and connected to a domain name. the problem is that there is two versions of the app. one on the ip adress of the vps and the other on the domain name. and this is not good for SEO. and i cant seem to find good resources on ip canonicalization or rediercting to domain name.

normally on an apache server i would use .htaccess but thats for apache. is there an equvialnt to .htaccess for expressjs. ? or any other way i can achive that.

thank you.

express

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hello,

What I would usually do is to use Nginx as a reverse proxy. This will also allow you to install an SSL certificate for your website using Let’s Encrypt and certbot.

You can follow the steps on how to do that here:

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-20-04

Once Nginx is in place, you could use the following server block to do the IP to domain redirection:

server {
    listen 80;
    server_name your_ip;

    return 302 $scheme://yourdomain.com$request_uri;
}

Regards,
Bobby

by Lisa Tagliaferri
by Kathleen Juell
Node.js is an open-source JavaScript runtime environment for building server-side and networking applications. The platform runs on Linux, macOS, FreeBSD, and Windows. Though you can run Node.js applications at the command line, this tutorial will…
  • hi Bobby. thank you for your answer. i followed the steps in the tutorial and got my app to run as a background proccess using pm2. and when i run the command curl http://localhost:3000 the output is indeed from my app. but the nginx revrse proxy is not working, im getting ‘redirected you too many times’ from the borwser. heres is the config:

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        server_name 198.51.100.255;
        return 302 $scheme://mysite.com$request_uri;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            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;
        }
    }
    
    • Hi there,

      The infinite loop that you are referring to is most likely caused by the following 302 redirect that you’ve got in place:

          return 302 $scheme://mysite.com$request_uri;
      

      Have you tried removing it and trying without it? Note that you would need to restart Nginx after the change.

      Regards,
      Bobby