docker - Backend fails after adding SSL certificate. NestNginx - Stack Overflow

My backend is not working after adding the SSL certificate. The backend worked with http before but aft

My backend is not working after adding the SSL certificate. The backend worked with http before but after adding SSL certificate it stopped working. I thought the problem is with nginx configurations but I think the problem is not in that but something else. My back is developed on nestjs

server {
    listen 443 ssl;
    server_name example www.example;

    ssl_certificate /etc/letsencrypt/live/example/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example/privkey.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-A>

    root /var/www/html;
    index index.html index.htm;

    add_header Strict-Transport-Security "max-age=31536000" always;
    add_header Content-Security-Policy upgrade-insecure-requests;

    # frontend
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    # backend
    location /api/ {
        proxy_pass https://127.0.0.1:5001/; 
        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;
    }
}

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as fs from 'fs';
import * as https from 'https';

async function bootstrap() {
  const httpsOptions = {
    key: fs.readFileSync('/etc/letsencrypt/live/example/privkey.pem'),
    cert: fs.readFileSync('/etc/letsencrypt/live/example/fullchain.pem'),
  };

  const app = await NestFactory.create(AppModule, {
    httpsOptions,
  });

 await app.listen(5001, () => {
  console.log('Server is running on https://localhost:5001');
 });

}
bootstrap();

NGINX LOGS

2025/01/17 17:05:07 [error] 325374#325374: *278 connect() failed (111: Unknown error) while connecting to upstream, client: 149.154.161.248, server: example, request: "GET /api/ HTTP/1.1", upstream: "https://127.0.0.1:5001/", host: "example"
2025/01/17 17:05:17 [error] 325374#325374: *280 connect() failed (111: Unknown error) while connecting to upstream, client: 80.242.211.179, server: example, request: "GET /api/ HTTP/1.1", upstream: "https://127.0.0.1:5001/", host: "example", referrer: "/"
2025/01/17 17:05:22 [error] 325374#325374: *280 connect() failed (111: Unknown error) while connecting to upstream, client: 80.242.211.179, server: example, request: "GET /api/hello HTTP/1.1", upstream: "https://127.0.0.1:5001/hello", host: "example"
2025/01/17 17:06:08 [error] 325374#325374: *284 connect() failed (111: Unknown error) while connecting to upstream, client: 149.154.161.201, server: example, request: "GET /api/ HTTP/1.1", upstream: "https://127.0.0.1:5001/", host: "example"
2025/01/17 17:06:28 [error] 325374#325374: *280 connect() failed (111: Unknown error) while connecting to upstream, client: 80.242.211.179, server: example, request: "GET /api/hello HTTP/1.1", upstream: "https://127.0.0.1:5001/hello", host: "example"
2025/01/17 17:06:28 [error] 325374#325374: *280 connect() failed (111: Unknown error) while connecting to upstream, client: 80.242.211.179, server: example, request: "GET /api/hello HTTP/1.1", upstream: "https://127.0.0.1:5001/hello", host: "example"
2025/01/17 17:06:29 [error] 325374#325374: *280 connect() failed (111: Unknown error) while connecting to upstream, client: 80.242.211.179, server: example, request: "GET /api/hello HTTP/1.1", upstream: "https://127.0.0.1:5001/hello", host: "example"
2025/01/17 17:06:29 [error] 325374#325374: *280 connect() failed (111: Unknown error) while connecting to upstream, client: 80.242.211.179, server: example, request: "GET /api/hello HTTP/1.1", upstream: "https://127.0.0.1:5001/hello", host: "example"
2025/01/17 17:06:29 [error] 325374#325374: *280 connect() failed (111: Unknown error) while connecting to upstream, client: 80.242.211.179, server: example, request: "GET /api/hello HTTP/1.1", upstream: "https://127.0.0.1:5001/hello", host: "example"
2025/01/17 17:07:09 [error] 325374#325374: *297 connect() failed (111: Unknown error) while connecting to upstream, client: 149.154.161.218, server: example, request: "GET /api/ HTTP/1.1", upstream: "https://127.0.0.1:5001/", host: "example"

I checked if the ports and queries are working correctly via curl -v at the end of the query the following message is displayed

{"message":"Cannot GET /","error":"Not Found","statusCode":404}

I've tried changing permissions on ssl certificates to make it work, changed nginx configuration thinking that's the problem but it's the backend that still doesn't work.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745352898a4623943.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信