Skip to content

Instantly share code, notes, and snippets.

@osamaqarem
Last active July 20, 2025 15:50
Show Gist options
  • Save osamaqarem/f7f19ccff04c6e9be88d2c4645bb395c to your computer and use it in GitHub Desktop.
Save osamaqarem/f7f19ccff04c6e9be88d2c4645bb395c to your computer and use it in GitHub Desktop.
Nginx on MacOS
  • You have a NodeJS server running at 192.168.100.190:3000 and you want to proxy it from your public IP with nginx.
  • You have Homebrew.

Install nginx

brew install nginx

Commands

Start:

nginx

Stop:

nginx -s stop

Restart:

nginx -s reload

Lint:

nginx -t

Expose Public IP

  • Firewall was already disabled.
  • Changed nginx port to 80 in nginx.conf.
  • Port forward 80 in router settings.
  • Public IP now visible.

Reverse Proxy

  • Add reverse proxy server block for nginx:
server {
        listen 80;
        server_name 192.168.100.190;

        location / {
            proxy_set_header   X-Forwarded-For $remote_addr;
            proxy_set_header   Host $http_host;
            # node app server IP
            proxy_pass         http://192.168.100.190:3000; 
        }
    }

Multi Server/Subdomain Setup nginx.conf

#user  nobody;

# Number of processes should not exceed number of cores #
worker_processes  1;

# MINIMUM (probably too low): worker_connections * 2 file descriptors = 512 #
# No need to multiply by worker_prcocesses as the limit is applied to each worker #
# 1 descriptor for client connection, 1 for proxied server #
# Could be more based on conf. Could be limited by system (ulimit -n) #
worker_rlimit_nofile 1024;

events {
    # Default 1024 #
    worker_connections  256;
}

# Error Log #
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
# Process ID Log #
pid        logs/nginx.pid;

http {
    include       mime.types;
    default_type  application/octet-stream;

    # Access Logs #
    map $request_uri $loggable {
        default                                             1;
        ~*\.(ico|css|js|gif|jpg|jpeg|png|svg|woff|ttf|eot)$ 0;
    }
    # store request body in temp file for debugging #
    # client_body_in_file_only on;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$request_body_file"';
    access_log  logs/access.log  main buffer=32k flush=30m if=$loggable;

    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
        
    server {
        listen 80;
        server_name ekyc-demo.xyz www.ekyc-demo.xyz;
        
        # Max user upload size #
        client_max_body_size 20M;
        # Uploaded file RAM buffer instead of temp file #
        client_body_buffer_size 20M;

        # Forward the client's IP address to the proxied server #   
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;

        location / {
            return 403;
        }
    }
        
    server {
        listen 80;
        server_name app1.ekyc-demo.xyz;
        location / {
            proxy_pass         http://localhost:3000;
        }
    }
        
    server {
        listen 80;
        server_name app2.ekyc-demo.xyz;
        location / {
            proxy_pass         http://localhost:5000;
        }
    }

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    include servers/*;
}

Issues

IP change. How to get a Static Public IP Address?

#!/usr/bin/env sh
IP4=$(dig @resolver1.opendns.com ANY myip.opendns.com +short)
echo "$IP4"
URL="https://dynamicdns.park-your-domain.com/update?host=%40&domain=ekyc-demo.xyz&password=mypassword&ip=""${IP4}"
echo "$URL"
curl --request GET \
  --url $URL

Executed via a Cronjob. Commands:

  • List cronjobs:

crontab -l

crontab -e

Users see old page until refreshing.

SSL

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