Linux ‐ Caddy Nginx - erikpa1/MyExperience GitHub Wiki

Nginx

Docker

sudo apt update //checks packages
sudo apt upgrade //updates them

curl -ssl https://get.docker.com | sh

Raw Nginx

sudo apt install nginx

systemctl status nginx // see status
// to exit press q

sudo systemctl stop nginx
sudo systemctl start nginx
sudo systemctl restart nginx // hard reset
sudo systemctl reload nginx // graceful reset
sudo systemctl disable nginx // disable on startup

Change default config

code /etc/nginx/sites-available/default #open in visual studio code
sudo nano /etc/nginx/sites-available/default

server {
    listen 80;
    server_name your_domain_or_ip;

    location / {
        try_files $uri $uri/ =404;
    }

    # Your new API route rule
    location /api/ {
        proxy_pass http://localhost:3000; # Change 3000 to your app's port
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Debug nginx

sudo nginx -t
systemctl status nginx.service
journalctl -xeu nginx.service | tail -30

List existing nginx configs:

nginx -t          # shows which config file is in use
ls /etc/nginx/sites-available/    # Debian/Ubuntu
cd /etc/nginx/sites-available/    # Debian/Ubuntu

sudo micro <NGING_CONFIG_FILE>

sudo systemctl reload nginx

Nginx 502.html

Create folder for HTML file:

mkdir -p /var/www/errors
cp 502.html /var/www/errors/
chmod 755 /var/www/errors
chmod 644 /var/www/errors/502.html

For every server add:

error_page 502 503 504 /502.html;

location = /502.html {
    root /var/www/errors;
    internal;
}

Restart nginx:

nginx -t && systemctl reload nginx

Caddy

sudo docker pull caddy

mkdir caddy-proxy
cd caddy-proxy

nano docker-compose.yml
nano Caddyfile
..

Add to file:

services:
  caddy:
    image: caddy:2
    restart: unless-stopped
    extra_hosts:
      - "host.docker.internal:host-gateway"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - caddy_data:/data
      - caddy_config:/config

volumes:
  caddy_data:
  caddy_config:

Caddyfile:


<DOMAIN_NAME> {
    reverse_proxy host.docker.internal:8080
}

Than run:

sudo docker-compose up -d
sudo docker-compose logs -f caddy

For Caddyfile reload use>

sudo docker-compose down
sudo docker-compose up -d
sudo docker-compose logs -f caddy

Check hosts folder:

sudo docker exec -it caddy-proxy_caddy_1 cat /etc/hosts