Search code examples
javascriptnode.jslinuxapache2opensuse

How do I use NGINX or Apache2 on openSUSE to re-route my port 80 to localhost:3000 so I can run my Node.js app


As described by the problem statement, I am running openSUSE (leap 42.3). I have a node app running on localhost:3000 which I would like to publish it (make it available to people outside my network). I already have a domain and currently, the files are being served by apache2 on port 80. I have found tons of similar problems online and solutions for them, but none are specific to mine (I think it is because of the operating system). Can anyone give me step by step solution to what I must do?

The first solution I found told me to change the configuration file and this is what I have right now:

<VirtualHost *:80>
   ServerName test.mytestsitebyrichard.com
   ServerAlias *.test.mytestsitebyrichard.com
   DocumentRoot /srv/www/htdocs
  #ProxyRequests on <--currently commented but this is what online tutorials told me to do. However, it is crashing the apache2
  #ProxyPass /cs/ http://localhost:3000/
</VirtualHost>

Do I need to enable anything? I have enabled the ProxyPass and ProxyPassReverse from the configuration menu. Any help would be appreciated. Thank you.

Note please refer to the screenshots below: enter image description here enter image description here enter image description here


Solution

  • You can achieve this in Nginx with Reverse Proxy.

    In your /etc/nginx/sites-enabled/ directory add a new configuration file (e.g. myapp.conf) with following configuration:

    server {
        listen 80;
        server_name yoururl.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;
        }   
    }