简体   繁体   中英

Cloudflare SSL: Express Not Working After SSL Appli

Please consider my circumstance:

  1. I have created a backend API on port 8880 in Express.js in HTTP
  2. I have created a frontend website in Next.js on port 80, also in HTTP.
  3. I signup with cloudflare they manage my DNS, I get their SSL so my frontend HTTP site becomes HTTPS.
  4. After successfully applying SSL to the frontend, the backend api stops receiving the POST request from login from the frontend.

As a result, I can no longer login to my site because every attempt fails when the POST request attempts to send the JSON payload of the login. How can I fix this SSL brokenness? This worked perfectly fine as HTTP to HTTP. But now, as HTTPS to HTTP, got failure.

Here is what I have tried to solve:

  1. HTTPS to HTTPS - this failed. I tried changing the backend to HTTPS but used a self-signed certificate following this instruction.

My code in app.js (backend):

https.createServer({
    key: fs.readFileSync('server.key'),
    cert: fs.readFileSync('server.cert')
  }, app)
  .listen(port, function () {
    console.log(`Connected on port ${port}`)
  })
  1. Set up my Cloudflare Page Rules - Apparently cloudflare has "Page Rules" settings for your domain so I set mine to domainname.com/api SSL: Flexible. Still failed. Testing in Postman showed I was successfully signing in with https://123.456.789.10:8880/api/signin but not with https://domainname.com/api . I enabled morgan on Express and saw no ping on the /api/signin endpoint.

  2. Switching the backend from HTTPS to HTTP, leaving Page Rules on SSL Flexible - As per this answered question here , I was thinking this will surely work since Cloudflare says "The Flexible SSL option allows a secure HTTPS connection between your visitor and Cloudflare, but forces Cloudflare to connect to your origin web server over unencrypted HTTP. An SSL certificate is not required on your origin web server and your visitors will still see the site as being HTTPS enabled." Doing this also failed.

And so, I am still unable to reach /api/signin either on domainname.com/api/signin or https://123.456.789.10:8880/api/signin or http://123.456.789.10:8880/api/signin . Morgan shows nothing in the console when I attempt signin, whereas it did before.

SOMEBODY out there must have solved setting up a frontend and backend on one IP but two different ports and gotten it to work with cloudflare SSL before. Please help!

I was able to solve this by implementing NGINX and setting the config to take / and /api and make them route to the respective apps running their port numbers. Additionally, I had to set up cors in my Express backend and used dotenv to allow a .env file to persist the FRONTEND_APP environment variable containing the ip address of the frontend Next.js app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM