Report this

What is the reason for this report?

Cannot POST to /api location using nginx

Posted on September 14, 2020

Hey, everyone!

I have a site running on the 443 port using create-react-app and Express for the backend.

I have the following /etc/nginx/sites-enabled/default file

(Using validdomain.com to protect the privacy of my customers)

server {
    listen 80;
    server_name validdomain.com;
    return 301 https://validdomain.com$request_uri;
}

server {
    listen 443 ssl;
    server_name validdomain.com;

    ssl_certificate /etc/letsencrypt/live/validdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/validdomain.com/privkey.pem;

    access_log /var/log/nginx/data-access.log combined;

    # serve the static files
    location / {
        root /home/developer_account/customer-management/customer-management-fe/build;
        try_files $uri /index.html;
    }

    location /api/ {
        proxy_pass http://localhost:4000;

        proxy_ssl_session_reuse off;
        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;
        proxy_redirect off;
    }
}

When I hit https://validdomain.com/ the location under the / index works perfect (this is the react app)

However, when I hit https://validdomain.com/api/admins/login or any other valid Express route, I get an error on the form:

Cannot POST /api/admins/login

When I ssh on my dropplet and use curl setting the proper params, and hit to http://localhost:4000/admins/login, it works perfect, including all the other Express routes I have!

I’m using pm2 (running without issues) and I’m not seeing any error on the pm2 service error.log

I’m using an Ubuntu 18.04 dropplet.

Could anyone point me on the right direction?

Thanks a lot!



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi @ricsasa201

I see that you use https://validdomain.com/api/admins/login in your react app but you used http://localhost:4000/admins/login to test with curl. Is it a typo?

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.