0

i have /var/www/html/ with index.html

i have /var/www/ai_backend with server.js

server.js is running on port 5000. so if i curl from this machine http://localhost:5000 i get response.

but if i send post request from index.html to server_ip/api i'm getting error

POST http://server_ip/api 404 (Not Found)

this is my nginx conf file

server {
        listen 80 default_server;
        listen [::]:80 default_server;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name <server_ip>; # i have real ip in config file

    location / {
            # serve static frontend first
            try_files $uri $uri/ /index.html;
    }

    # location ~*^/(api|posts|products) {
    location /api {
            proxy_pass http://localhost:5000;
            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;
    }

}

1 Answer 1

0

Your configuration looks mostly correct, but there are a few things you can check:

  • Make sure that your backend service is running and listening on localhost:5000.
  • Check that you have reloaded nginx after making changes to the configuration file. You can do this by running sudo systemctl reload nginx on your server.
  • Check the nginx error logs to see if there are any relevant error messages that could explain why the proxying is not working as expected. You can find the logs in /var/log/nginx/error.log.
  • Make sure that your frontend is sending the POST request to the correct URL. Based on your configuration, it should be sending the request to http://<server_ip>/api. If you're using a relative URL like /api, it may not work correctly.
  • Check the access logs in /var/log/nginx/access.log to see if the request is reaching nginx and if it's being routed to the backend service correctly. You should see a line in the log with the HTTP status code and the URL that was requested.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .