[Nginx] 톰캣 연동 및 [emerg] directive & control process 오류 해결

Julie Mingu·2023년 6월 2일
0
post-thumbnail

우분투에서 Nginx와 Tomcat 연동하는 방법

  • EC2 Ubuntu
  • Termius
  • Nginx 1.18.0
  • Tomcat apache-tomcat-10.1.9 (10버전)

유용한 설정 코드들

  • 설치
    sudo apt install nginx vim -y

  • 설정파일 오픈
    sudo vim /etc/nginx/sites-available/default

  • 수정

location / {
        proxy_pass http://localhost:8080;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
}



server {
        listen 80;
        server_name localhost;

        location / {
            proxy_pass http://localhost:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
   
  • 톰캣 설치 (wget)
    wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.1.9/bin/apache-tomcat-10.1.9.tar.gz
  • 압축풀기
    tar -xvf apache-tomcat-10.1.9.tar.gz
  • 실행
    /usr/local/tomcat/apache-tomcat-10.1.9/bin/startup.sh
  • 포트확인
    netstat -an | grep 8080
  • 재실행 및 확인
    sudo systemctl daemon-reload
    sudo systemctl --now enable tomcat
    sudo systemctl status tomcat --no-pager -l



nginx 자주 나는 오류

  1. nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:86
  1. nginx: [emerg] unknown directive " " in ~~

  2. nginx.service: Control process exited, code=exited, status=1/FAILURE

  3. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

거의 모든 종류의 nginx 오류를 만나보았답니다. 😇




기본적인 해결방법

  1. sudo vim /etc/nginx/sites-available/default
    파일에 내용 덮어씌울 때 오타가 있거나,
    보통 괄호 또는 따옴표(;)를 생략했을 가능성

  2. EC2 인스턴스 편집 - 8080 포트 추가

  3. Ubuntu 서버에서 root 권한이 아닌데 명령어 치고있는 경우 - 꼭 sudo su 하기

하지만.. 인터넷에 검색해보시는 분들은 보통 다 해보셨을 거에요. ^^;;



나의 해결 과정

✔✔✔ 🔥중요🔥 별표 다섯개!!!! ✔✔✔

1. etc/nginx/nginx.conf.save 파일 발견

  • 오류메시지는 nginx.conf 파일에 문법오류가 있다고 해서 그 설정파일을 눈이 사팔뜨기가 될때까지 쳐다봤지만 문제는 없었다.

  • cd etc 하면서 폴더와 파일을 하나씩 꼼꼼하게 확인해봤다.

  • conf.save 라는 헷갈리게 생긴 파일이 있었는데 해당 파일이 어떻게 생겼는지는 모르겠지만 거기엔 중괄호 문제가 있었다.

  • 스왑파일들도 문제가 된다. swap file 삭제하면서 꼭 쓰잘데기없는 파일들도 삭제해주자.

  • sudo rm 경로/스왑파일명

  • 불필요한 파일들을 삭제해주니 오류 메시지가 뜨지않았다.

2. Apache를 다운로드 하셨다면.. 포트충돌

  • Apache2를 다운받아놨다면 8080 포트가 충돌된다.
  • 반드시 둘 중 하나는 삭제해야한다!
  • 현재 실행중인 다른 웹서버를 확인해보자.
    sudo lsof -i :80
  • sudo systemctl stop apache2
  • sudo systemctl start nginx

3. 재시작하면 의외로 많은 오류들이 사라진다.

  • sudo service nginx restart


nginx로 고생하는 많은 분들에게 위로를 건넵니다..

profile
(੭˙ ˘ ˙)੭🌱 자란다, 잘한다!

0개의 댓글