How to best use and configure nginx

Hi, I am trying to connect a vite frontend to by npl backend which runs locally in docker containers. Running into CORS issues. It was suggested nginx would be the most systematic, prod-like solution to the problem. I have nginx service up but have trouble configuring the service to properly deal with auth and engine calls. Do you have a sample nginx.conf file for such a setup?

If running from within docker compose environment create nginx/nginx.conf within your repository with following content:

server {
    listen 80;

    location / {
        proxy_pass http://engine:12000/;

        # Handle CORS headers
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization' always;

        # Allow OPTIONS method
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
            add_header 'Content-Length' '0';
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            return 204;
        }
    }
}

and add following service configuration to docker-compose.yml

  nginx-proxy:
    image: nginx:latest
    container_name: nginx-cors-proxy
    ports:
      - "8080:80"
    volumes:
      - ./nginx:/etc/nginx/conf.d
    depends_on:
      engine:
        condition: service_healthy

This should do the trick, and the engine will be available on localhost:8080

note: this configuration will allow any site to access the engine which is ok for local development, for production * should be replaced with the allowed sites.

1 Like