Question

Cannot POST to /api location using nginx

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!


Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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?