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}`))
2
  • 1
    Please share the nginx access.log for /api/auth. Commented May 2, 2023 at 3:44
  • Here it is: 173.239.218.10 - - [02/May/2023:04:41:24 +0000] "GET /api/profile/me HTTP/1.1" 404 197 "54.224.141.136/dashboard" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" 216.24.210.70 - - [02/May/2023:04:41:24 +0000] "GET /api/auth HTTP/1.1" 404 197 "54.224.141.136/dashboard" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" Commented May 2, 2023 at 4:48

1 Answer 1

0

According to the routes config in the js file:

app.use('/api/auth', require('./routes/api/auth'))

the route is waiting for /api/auth. However, your nginx config file removes the /api prefix. Change it to this:

location /api {
  root /opt/devParty/routes/api;
  try_files $uri /api/auth.js =404;
  add_header 'Access-ControlAllow-Origin' '*';
  proxy_pass http://localhost:4000; # -> remove trailing slash
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks @andromeda I've made these adjustments but i'm still getting 404 at /api/auth. I don't understand, how does my config file remove the /api prefix?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.