is it ok to deploy 2 applications in a single droplet?

September 29, 2018 2k views
Ubuntu 16.04 Nginx Deployment Node.js

I have two apps, api and frontend, I have uploaded in the same droplet, the frontend app is running on the port 3000 and the api is running on the 8000, my nginx config is:

{.....
    # root /var/www/html;
    root /home/testing-client/dist;

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

    server_name _;

    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;

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

but in my frontend app throw error to connect with api in localhost:8000

4 Answers

Yes it is possible. Have you checked whether the API end is listening on port 8000 from inside the server?

try: telnet localhost 8000

You might need to install telnet (yum install telnet or apt-get install telnet). You also want to check the API’s error log for potential issues.

  • I applied a call with curl inside the same droplet and it returns the data, but from the application that runs on port 3000 I get an error

    • the api I have it running with pm2 on port 8000, you can see on http://104.248.49.211/ in the console

      • I see what the culprit is. In your bundle.js, you have a the API_URL set to

        API_URL:"http://localhost:8000
        

        Because this runs client side (in my browser), my browser attempts to connect to “localhost:8000” on my PC. You’ll need to set the API_URL to your server like this:

        API_URL: "http://104.248.49.211:8000"
        

        But you also need to poke a hole in your firewall for incoming port 8000. You can also add an Nginx vhost for port 8000 and proxy_pass the requests back and forth to the pm2.

        Try it without Nginx and if it works as expected get Nginx to relay traffic to/from that port/app.

        • thanks!, I suspected that but I did not want the api to be visible from the outside or internet, only for the client app

the steps that I followed were:

  1. Connect with ssh to my droplet
  2. clone two repos -api with nodejs: https://github.com/TheEnmanuel23/testing
  3. client app with react: https://github.com/TheEnmanuel23/CompleteTestGit
  4. install pm2 and nginx
  5. install packages of my 2 apps
  6. enable firewall with sudo ufw allow ‘Nginx HTTP’
  7. config nginx, I have added two lines:
    • root /home/CompleteTestGit/dist;
  • location / { proxy_pass http://localhost:3000; # First attempt to serve request as file, then # # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; }

my client app on port 3000 is working but no get data of the api on port 8000

@enmisac Are you get any success?

Have another answer? Share your knowledge.