본문 바로가기

공부방/Nginx

노드 설치

1. 리눅스 OS version 확인


 

cat /etc/redhat-release

 

 

2. nvm 설치

 

 wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash

 

3. reboot 또는 소스 재시작한 뒤 버전 확인

 

#reboot
source ~/.bashrc
nvm --version

 

 

4. 원하는 노드 버전을 설치한다

 

nvm install 14.16.0

 

 

5. Nginx 설치

/etc/yum.repos.d 경로에 nginx.repo 파일을 추가하고 내용은 다음과 같이 작성한다.

Nginx 설치는 root 계정으로 진행한다.

 

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

 

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

 

6. Nginx 설치

 

yum install nginx

 

설치 확인

 

NJS 설치

yum install nginx-module-njs

 

njs loadmodule 

/etc/nginx/nginx.conf

load_module modules/ngx_http_js_module.so;

#user  nginx;
user jirancomms;
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;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

 

서버 세팅

 

nginx 프로젝트 컨피그 설정 추가

/etc/nginx/conf.d/alimiseo.coolmessenger.com.conf

js_path "/etc/nginx/njs/";
js_import main from try-alimi.js;
resolver 1.1.1.1;
#js_var $env dev;

server {
    listen       80;
    server_name  alimiseo.coolmessenger.com;

    #charset koi8-r;
    access_log  /var/log/nginx/alimiseo.coolmessenger.com.access.log  main;
    error_log  /var/log/nginx/alimiseo.coolmessenger.com.com.error.log  debug;

    location / {
        js_content main.alimtongService;
    }
}

server {
    listen       443 ssl;
    server_name  alimiseo.coolmessenger.com;

    #charset koi8-r;
    access_log  /var/log/nginx/alimiseo.coolmessenger.com.access.ssl.log  main;
    error_log  /var/log/nginx/alimiseo.coolmessenger.com.error.ssl.log  debug;

    ssl_certificate /home/jirancomms/ssl/2022/coolmessenger/Wildcard.coolmessenger.com_pem.pem;
    ssl_certificate_key     /home/jirancomms/ssl/2022/coolmessenger/KeyFile_Wildcard.coolmessenger.com_pem.key;
    ssl_protocols  TLSv1.2;
    ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-S
    ssl_session_timeout 5m;

    location / {
        js_content main.alimtongService;
    }
}

 

 

njs try-alimi.js 추가

/etc/nginx/try-alimi.js

// @ts-ignore
const fs = require('fs');
async function alimiService(r) {
    return service(r, '/home/jirancomms/www/www.coolalimi.com');
}
async function alimtongService(r) {
    return service(r, '/home/jirancomms/www/www.coolalimi.com');
}
async function service(r, dir) {
    // const dir = '/home/niney/coolalimi';
    // const dir = '/home/jirancomms/coolalimi';
    try {
        const file = fs.readFileSync(dir + r.uri);
        r.return(200, file.toString());
        return;
    }
    catch (e) {
        // r.error("try not exists file : " + r.uri);
    }
    try {
        let title = /content="쿨알리미"/gi;
        let description = /content="우리학교 소통의 시작"/gi;
        let keywords = /content="알림장, 학교알리미, 쿨알리미"/gi;
        let defaultImage = /content="\/\/update.coolmessenger.com\/_ImageServer\/alimi-info\/sns.png"/gi;
        let data = fs.readFileSync(dir + '/index.html').toString();
        if (r.uri.includes('/bbs/detail')) {
            const urlSplit = r.uri.split('/');
            const streamId = urlSplit[urlSplit.length - 1];
            let envPrefix = '';
            if (r.variables.env) {
                envPrefix = r.variables.env + '-';
            }
            // @ts-ignore
            let reply = await ngx.fetch(`http://${envPrefix}search.coolmessenger.com/api/coolStream/_searchStream/${streamId}`);
            let text = await reply.text();
            const response = JSON.parse(text);
            if (response.result) {
                const streamData = response.data;
                data = data.replace(title, `content=\"${streamData.title}\"`);
                const content = streamData.contentsText.substring(0, 200).replace(/\n/gi, "").replace(/\r\n/gi, "");
                data = data.replace(description, `content=\"${content}\"`);
                if (streamData.firstImage) {
                    data = data.replace(defaultImage, `content=\"${streamData.firstImage}\"`);
                }
            }
        }
        if (r.uri.includes('/alimtalk')) {
            data = data.replace(title, `content=\"쿨알림톡\"`);
            data = data.replace(description, `content=\"문자보다 더 저렴하고 강력하게\"`);
            data = data.replace(keywords, `content=\"알림톡, 공지사항, 가정통신문\"`);
            data = data.replace(defaultImage, `content="\/\/update.coolmessenger.com\/_ImageServer\/alimi-info\/og-alimtalk.png"`);
        }
        if (r.uri.includes('/parent')) {
            data = data.replace(defaultImage, `content="\/\/update.coolmessenger.com\/_ImageServer\/alimi-info\/og-parent.png"`);
        }
        r.status = 200;
        // r.headersOut.foo = 1234;
        r.headersOut['Content-Type'] = "text/html; charset=utf-8";
        // r.headersOut['Content-Length'] = 15;
        r.sendHeader();
        r.send(data);
        r.finish();
    }
    catch (e) {
        r.error(e.message);
        r.return(500, e.message);
    }
}
export default { service, alimiService, alimtongService };

 

ssl 인증서 적용

/home/jirancomms/ssl

2022, 2023 폴더 추가

 

nginx 재시작

service nginx restart

 

hosts 파일 변경하여 접속 확인

C:\Windows\System32\drivers\etc\hosts

vscode로 열었음

 

# 공인 ip / domain 추가
210.122.44.107 alimiseo.coolmessenger.com

ctrl + s -> retry as admin

 

 

coolalimi 프로젝트 이동

/etc/nginx/conf.d/

host에 ip 추가

도메인 확인

 

 

 

'공부방 > Nginx' 카테고리의 다른 글

발표자료  (0) 2023.04.02
Nginx vs apache  (0) 2023.04.02
Tomcat 설정  (0) 2023.01.17
Nginx config 설정  (0) 2023.01.17
Nginx, Tomcat, War  (0) 2023.01.12