Skip to content

Instantly share code, notes, and snippets.

@juanpablocs
Created October 25, 2025 14:23
Show Gist options
  • Select an option

  • Save juanpablocs/78bba530f1cbaee649bdf8562e54b54e to your computer and use it in GitHub Desktop.

Select an option

Save juanpablocs/78bba530f1cbaee649bdf8562e54b54e to your computer and use it in GitHub Desktop.
Nginx cors with Nest Cors Api

Nginx cors conf

Configure cors for specific domains

Enable cors into main nest api

app.enableCors({
    origin: [process.env.FRONTEND_URL, process.env.DASHBOARD_URL],
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    credentials: true,
  });

Proxy pass nginx location

...
location / {
    if ($request_method = OPTIONS ) {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PATCH' always;
        add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,X-Requested-With' always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Max-Age' 3600 always;
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        return 204;
    }

    # Configure proxy server
    proxy_pass http://localhost:3000;  # Cambia el puerto si es necesario
    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;
}
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment