/api/auth 404 (Not Found) nginx ec2

0

What do I need to change on my nginx/sites-available/default config so that it can find /api/auth? I am new to nginx. I believe the issue is in the nginx configuration for proxying requests to the api. here is my /nginx/sites-available/default config:

  server {
  listen 80 default_server;
  server_name _;

  # react app & front-end files
  location / {
    root /opt/devParty/client/build;
    try_files $uri /index.html;
  }

  # node api reverse proxy
  location /api {
    root /opt/devParty/routes/api;
    try_files $uri /api/auth.js =404;
    add_header 'Access-ControlAllow-Origin' '*';
    proxy_pass http://localhost:4000/;
     }
   }

Here is the file structure on EC2 ubuntu:

devParty/
├── client
│   ├── package-lock.json
│   ├── package.json
│   └── webpack.config.js
├── config
│   ├── db.js
│   └── default.json
├── middleware
│   └── auth.js
├── models
│   ├── Post.js
│   ├── Profile.js
│   └── User.js
├── package-lock.json
├── package.json
├── routes
│   └── api
└── server.js

And my server.js file:

const { application } = require('express')
const express = require('express')
const app = express()
const PORT = process.env.PORT || 4000
const connectDB = require('./config/db')
const path = require('path')

// Connect Database
connectDB()

// Init Midddleware
// Allows us to get data in req.body on users.js
app.use(express.json({ extended: false }))

// app.get('/', (req, res) => res.send('API Running'))

// Define Routes
app.use('/api/users', require('./routes/api/users'))
app.use('/api/auth', require('./routes/api/auth'))
app.use('/api/profile', require('./routes/api/profile'))
app.use('/api/posts', require('./routes/api/posts'))

// Server status assets in production
if(process.env.NODE_ENV === 'production') {
    // Set static foler
    app.use(express.static('client/build'))

    app.get('*', (req, res) => {
        res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html')) })
}

app.listen(PORT, () => console.log(`Server started on port ${PORT}`))
dbms_dd
asked a year ago1126 views
1 Answer
0

It seems like the error is occurring because your Nginx configuration is not correctly proxying requests to your API server. Here's what you can try to fix the issue:

  1. Verify that your API server is running and accessible on localhost:4000. You can do this by running curl http://localhost:4000/api/auth on your EC2 instance's terminal.
  2. In your Nginx configuration, replace root /opt/devParty/routes/api; with proxy_pass http://localhost:4000; to proxy all requests under /api to your API server running on port 4000. Also, remove try_files $uri /api/auth.js =404; from the location /api block.

Your updated Nginx configuration should look like this:

server {
  listen 80 default_server;
  server_name _;

  # react app & front-end files
  location / {
    root /opt/devParty/client/build;
    try_files $uri /index.html;
  }

  # node api reverse proxy
  location /api {
    add_header 'Access-Control-Allow-Origin' '*';
    proxy_pass http://localhost:4000;
  }
}

  1. Save the updated configuration and restart Nginx using the command sudo service nginx restart.
  2. Access your API server by visiting http://<your-ec2-instance-public-ip>/api/auth in your browser.
hash
answered a year ago
  • Thanks for the feedback! I made these changes but the console now shows error: TypeError: a.map is not a function at Profiles.js:22:30 and front-end does not load. instead now there is a blank grey screen. Do you think it's the use of require in server.js: app.use('/api/auth', require('./routes/api/auth')) along with the file structure being /routes/api/auth even though in nginx config the location is location /api ? here is a link to the github repo for my project: https://github.com/codereyes-1/DevSocial

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions