Post

로컬 서버 node.js + nginx 연동 (및 RDS 연동)

node.js + nginx 로컬 서버 연동 (및 RDS 연동)

0. 들어가며

이제 주저리 주저리 설명 안 하겠다. 내가 헤맸던 곳은 조금 더 자세하게만 적을 예정이다. 나는 MacOS 사용하는 중이다. 다른 운영체제면, 설치 명령어를 다른 웹페이지 참고하길 바란다.

1. 파일 설치

1
2
brew install node
brew install nginx

잘 실행되는지 확인 (https://localhost:8080/ 접속시 확인 가능)

1
sudo nginx

2. nginx 설정

편집기로 아래 명령어의 경로 파일을 연다.

1
sudo vim /usr/local/etc/nginx/nginx.conf

만약 저장이 안 된다면, 경로가 없어서이므로, 만들어줘야한다. 저장하지 않고 나와서 아래의 명령어로 경로를 만들어주자.

1
sudo mkdir -p /usr/local/etc/nginx/

그리고 다시 편집기로 들어가, 아래의 코드 넣어주자.

1
2
3
4
5
6
7
8
location / {
    proxy_pass http://localhost:3000;
    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;
}

3. app 파일 생성

Application을 짤 경로 생성

1
2
mkdir ~/myapp
cd ~/myapp

package.json 생성

1
npm init -y

Express 설치

1
npm install express

node.js용 파일 작성 (파일명은 app.js다)

1
2
3
4
5
6
7
8
9
10
11
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello from Node.js!');
});

app.listen(port, () => {
  console.log(`App listening at http://localhost:${port}`);
});

4. nginx 적용 및 시작

새롭게 된 구성 적용 및 nginx 재시작

1
2
sudo nginx -t
sudo nginx -s reload

node.js 실행으로 서버에 파일 연동하기

1
npm node.js

5. 마무리

다음편에 이어진다. (언젠가)

https://sjparkk-dev1og.tistory.com/53

This post is licensed under CC BY 4.0 by the author.