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!

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.

×
1 answer

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?

  • Hi, Tom!

    No, that is correct. I do that to redirect all the traffic preceded by /api/ to the backend app.

    • Hi @ricsasa201

      With your given Nginx configuration, when your react app requests the link https://validdomain.com/api/admins/login, your backend will receive the link http://localhost:4000/api/admins/login instead of http://localhost:4000/admins/login. You should test again with curl to ensure that your backend can understand http://localhost:4000/api/admins/login as well. If not, that is why you get the error Cannot POST /api/admins/login

    • Glad you have resolved it.

      Tom

Submit an Answer