[nextjs]nextjs+domain(ddns)+https(caddy)

๐Ÿ‘‰๐Ÿป Nextjs์„ค์ • ํ”„๋กœ์ ํŠธ ์‹œ์ž‘๊ณผ ํ•จ๊ป˜ ์—ฌ๋Ÿฌ๋ถ„์˜ ๋กœ์ปฌ PC์— https์„ค์ •์„ ํ•ฉ๋‹ˆ๋‹ค.
As part of the Next.js project setup, you will configure HTTPS on your local machine.

๐Ÿ‘‰๐Ÿป ์ด์ „์—๋Š” https์„ค์ •์„ ์œ„ํ•ด์„œ nginx์™€ certbot์„ ์„ค์น˜ํ•ด์„œ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
Previously, I installed and used Nginx and Certbot to set up HTTPS.

๐Ÿ‘‰๐Ÿป ์ด๋ฒˆ์—๋Š” ๊ฐ„๋‹จํžˆ cadddy๋กœ https๋ฅผ ๊ตฌํ˜„ํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.
This time, I plan to implement HTTPS using Caddy.

https://caddyserver.com

๐Ÿ‘‰๐Ÿป ๊ธฐ์กด ๋ฐฉ์‹๋ณด๋‹ค๋Š” ์„ค์ •์ด ๋งค์šฐ ๊ฐ„ํŽธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์€ ํ”„๋กœ์ ํŠธ๋Š” cadddy๋ฅผ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.
I recommend Caddy for small projects because it is much easier to set up than traditional methods.

๐Ÿ‘‰๐Ÿป ์„ฑ๋Šฅ์ด ์ค‘์š”ํ•œ ๊ณณ์€ Nginx+Certbot์„ ์‚ฌ์šฉํ•˜์‹œ๊ณ  ๊ฐ„๋‹จํ•œ ํ”„๋กœ์ ํŠธ๋Š” caddy๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด๋ฉ๋‹ˆ๋‹ค.
์„ฑ๋Šฅ์ด ์ค‘์š”ํ•œ ๊ณณ์€ Nginx+Certbot์„ ์‚ฌ์šฉํ•˜์‹œ๊ณ  ๊ฐ„๋‹จํ•œ ํ”„๋กœ์ ํŠธ๋Š” caddy๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด๋ฉ๋‹ˆ๋‹ค.

๐Ÿ‘‰๐Ÿป ๋„๋ฉ”์ธ์€ DDNS๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
The domain uses DDNS.

๐Ÿ“ Nextjs ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ
Create a Next.js project

npx create-next-app@latest

๐Ÿ“ ๋„๋ฉ”์ธ ํ—ˆ์šฉ / Allow Domain

— next.config.ts์— ์•„๋ž˜์ฒ˜๋Ÿผ ๋„๋ฉ”์ธ์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.
Allow the domain in next.config.ts as shown below.

import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  /* config options here */
allowedDevOrigins: ['host.yourdomain.org', '*.yourdomain.org'],
};

export default nextConfig;

๐Ÿ“ https์„ค์ • / HTTPS configuration

โœ”๏ธ macOS

— Caddy๋Š” ๋ฒ”์šฉ ์›น์„œ๋ฒ„๋กœ ngin๋ณด๋‹คํ›จ์”ฌ ์‰ฝ๊ฒŒ https์„ค์ •์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Caddy is a general-purpose web server that makes setting up HTTPS much easier than Nginx.

— Caddy๊ฐ€ 80์œผ๋กœ ์ ‘์†ํ•˜๋ฉด 3000ํฌํŠธ๋กœ ํฌ์›Œ๋”ฉ ํ•ด์ค๋‹ˆ๋‹ค.
When Caddy receives a request on port 80, it forwards it to port 3000.

brew install caddy

— Caddy file ๋งŒ๋“ค๊ธฐ(myapp11 ํ”„๋กœ์ ํŠธ๋‚ด )
Create a Caddyfile (within the myapp11 project)

# ํŒŒ์ผ์—ด๊ธฐ / Open File
nano Caddyfile

# ์„ค์ • ์ž‘์„ฑ / Create Configuration
aloy-horizon.duckdns.org {
    reverse_proxy localhost:3000
}

#ํŒŒ์ผ ์ €์ •ํ›„ ์ข…๋ฃŒ / Save file and exit (Ctrl + O,Ctrl + x)

— Caddy ์‹คํ–‰ / Run Caddy

caddy fmt --overwrite Caddyfile
caddy run

# background

caddy start
caddy stop

# ์žฌ๋ถ€ํŒ…์‹œ์—๋„ ์ ์šฉํ•˜๋ ค๋ฉด
# To apply the setting even after rebooting

brew services start caddy
brew services stop caddy

โœ”๏ธ Linux

— Caddy ์„ค์น˜ / Installing Caddy

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list

sudo apt update
sudo apt install caddy

— Caddy ์‹คํ–‰ / Run Caddy


# 
sudo mv Caddyfile /etc/caddy/Caddyfile
sudo nano /etc/caddy/Caddyfile

 host.yourdomain.org {
     reverse_proxy localhost:3000
 }

sudo systemctl enable --now caddy
sudo systemctl status caddy

๐Ÿ“ ๋ธŒ๋ผ์šฐ์ € ์ ‘์† / Access Browser

host.yourdomain.org

Leave a Reply