Nginx on Render

Hello, i have deployed my server node.js recently, and it was working fine (Web Service), but i wanted to know how to setup a nginx in it, I’m new to it so i don’t know exactly how to do it. (I know about the scaling, and pretend to use it in the future as well, but i really wanted to know how to setup nginx, if anyone could help me, ill be grateful :heart:)

Hey,

Most customers usually don’t deploy a reverse proxy when using Render. We use a load balancer that handles horizontal scaling. If you can provide more information about your particular use case, we would be happy to offer further assistance.

Regards.
Jérémy, Render Support

At risk of hijacking the thread but also in case you need an example: trying to deploy a static frontend and an API backend with request proxying.

I know the static site has rewrite configuration rules. But if you’re deploying using blueprints (e.g. using Previews), I’m not aware of any way to dynamically generate a rewrite rule that points to the generated API backend.

Adding an nginx server theoretically solves that problem by allowing you to dynamically inject the service’s host and port in the docker build stage.

See below for example render.yaml. My main challenge is figuring out how to set up nginx server location rules correctly.

services:
  - type: web
    name: frontend
    env: static
    healthCheckPath: /
    buildCommand: yarn build:frontend
    staticPublishPath: frontend/dist

  - type: web
    name: backend
    env: docker
    healthCheckPath: /health
    dockerfilePath: ./Dockerfile.backend
    envVars:
      - key: DATABASE_URL
        fromDatabase:
          name: db
          property: connectionString

  - type: web
    name: nginx
    env: docker
    healthCheckPath: /health
    dockerfilePath: ./nginx/Dockerfile
    envVars:
      - key: FRONTEND_HOST
        fromService:
          type: web
          name: frontend
          property: host
      - key: FRONTEND_PORT
        fromService:
          type: web
          name: frontend
          property: port
      - key: BACKEND_HOST
        fromService:
          type: web
          name: backend
          property: host
      - key: BACKEND_PORT
        fromService:
          type: web
          name: backend
          property: port

databases:
  - name: db
    ipAllowList: []
# Example of front end app container, but with reverse proxy to API.

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen 80;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;

        # Example of reverse proxy, separate front end and back end
        location /api/ {
            resolver 127.0.0.11 valid=30s; # Docker DNS
            proxy_pass http://${BACKEND_HOST}:${BACKEND_PORT}/api/; # Local back end
            proxy_redirect default;
        }
        location /health {
            access_log off;
            add_header 'Content-Type' 'text/plain';
            return 200 "OK\n";
        }
        # Serve the built front end assets
        location / {
            # resolver 127.0.0.11 valid=30s; # Docker DNS
            proxy_pass http://${FRONTEND_HOST}.onrender.com; # Local back end
            proxy_redirect default;
        }
    }
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.