0

I'm trying to deploy a MERN app to a DigitalOcean droplet using Nginx (I've used a connection string to connect to a MongoDB atlas instance). The frontend static files seem to successfully work, but I face two issues when it comes to the backend (a Nodejs/express app). First, I get a connect() failed (111: connection refused) for every request from the frontend to the backend.

2021/07/12 23:54:32 [error] 5101#5101: *394 connect() failed (111: Connection refused) while 
connecting to upstream, client: "My Ip", server: "example.com", request: "GET 
/api/images HTTP/1.1", upstream: "http://127.0.0.1:5000/images", host: "example.com", 
referrer: "https://example.com/"

Second, I face a 404 Not Found nginx/1.14.0 (Ubuntu) error while trying to check the routes through the Postman. Not sure both of these are the same problem.

Here's my Nginx configuration in /etc/nginx/sites-available/default:

server {
    root var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name example.com www.example.com;

    location /api/ {
        proxy_pass http://localhost:5000/;
    }

    location / {
        root /home/${my_non_root_user}/frontend/deploy;
        try_files $uri /index.html;
    }

    listen [::]:443 SSL ipv6only=on; #managed by Certbot
    listen 443 SSL; #managed by Certbot
    ssl_certificate /etc/letsencrypt/live/${example.com}/fullchain.pem; #managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/${example.com}/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 = www.example.com) {
        return 301 https://$host$request_uri;
    } #managed by Certbot

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

        listen 80 default_server;
        listen [::]:80 default_server;

        server_name example.com www.example.com;
    return 404; #managed by Certbot
}

This is how my backend server.js looks like:

import express from 'express'
import path from 'path'
import dotenv from 'dotenv'
import connectDB from './config/db.js'

import memberRoutes from './routes/memberRoutes.js'
import messageRoutes from './routes/messageRoutes.js'
import fileRoutes from './routes/fileRoutes.js'

dotenv.config()

connectDB()

const app = express()

app.use(express.json())

// routes
app.use('/api/members', memberRoutes)
app.use('/api/messages', messageRoutes)
app.use('/api/files', fileRoutes)

const __dirname = path.resolve()
app.use('/files', express.static(path.join(__dirname, '/files')))

const PORT = process.env.PORT || 5000

app.listen(
  PORT,
  console.log(`Server running in ${process.env.NODE_ENV} mode on port 
  ${PORT}`)
)

When I make a request in Postman, to {{URL}}/messages (No difference it is a GET, or POST), where {{URL}} is THE_IP_TO_DROPLET, I get this output:

<HTML>

  <head>
    <title>404 Not Found</title>
  </head>

  <body bgcolor="white">
    <center>
      <h1>404 Not Found</h1>
    </center>
    <hr>
    <center>nginx/1.14.0 (Ubuntu)</center>
  </body>

</html>

Do you have any suggestion? Totally stuck in this for the whole weekend!

Thank you.

a_arbshhi
  • 11
  • 1
  • 4

0 Answers0