Search code examples
typescriptdockernginxdocker-composenestjs

Nestjs doesn't handle request from nginx proxy_pass using docker-compose


I'm facing an issue using Nestjs with an request coming from proxy_pass by nginx with docker-compose.

I have a nestjs service that listen on the port 3000 with the following endpoints

  • get /api/users/:id
  • get /api/users
  • post /api/users

I have the following nginx configuration

events {
   worker_connections 1024;
}
http {

    upstream ticketing_users {
        server ticketing_users:3000;
    }

    server {
        listen 80;
        server_name ticketing.dev;

        location ~ ^/api/users(.*)$ {
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            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-Host $server_name;
            proxy_intercept_errors on;
            proxy_pass http://ticketing_users/;
        }

        access_log /var/logs/nginx/access_log access;
        error_log  /var/logs/nginx/error.log error;
    }
}

Here is my nginx config in docker-compose.yaml :

reverse-proxy:
    image: nginx
    volumes:
      - ./docker/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx:/var/logs/nginx
    networks:
      - ticketing.dev
    ports:
      - 8080:80
    depends_on:
      - ticketing_users
    links:
      - ticketing_users

Here is my main.ts :

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(UsersModule, {
    cors: true,
    logger: ['verbose', 'debug', 'error', 'warn']
  });
  app.set('trust proxy', true);
  app.use(morgan('combined'));
  app.setGlobalPrefix('api');
  await app.listen(3000);
}
bootstrap();

and my controller (generated with cli so it's declared in the module) :

@Controller('users')
export class UsersController {
  @Get(':id')
  public findById(@Param('id') id: string): string {
    return id;
  }
}

And i've set ticketing.dev in my host file. But when i'm querying for exemple get http://ticketing.dev:8080/api/users/1234 with postman i always get the same result, i tried to put some console.logs but nothing appears :

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

But morgan middleware handle correctly the request and is showing :

GET /api/users/test 404 0.496 ms - 77

What am i doing wrong ?


Solution

  • I don't really remember why (it's been a long time since) but here is how I was able to fix out my issue:

    events {
       worker_connections 1024;
    }
    http {
    
        upstream ticketing_users {
            server ticketing_users:3000;
        }
    
        server {
            listen 80;
            server_name ticketing.dev;
    
            location ~ ^/api/users(\/.*)?$ {
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                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-Host $server_name;
                proxy_pass http://users_server/api/users$1;
            }
    
            underscores_in_headers on;
            proxy_intercept_errors on;
            access_log /var/logs/nginx/access_log access;
            error_log  /var/logs/nginx/error.log error;
        }
    }