[nginx] 시작 - 소개하기

Hyeseong·2022년 5월 25일
0

nginx

목록 보기
1/1


Nginx

"engine-ex"로 발음되는 Nginx는 오픈 소스 웹 서버로, 웹 서버로서의 초기 성공 이후 지금은 역방향 프록시 , HTTP 캐시 및 로드 밸런서로도 사용됩니다.

Nginx를 사용하는 유명 회사로는 Autodesk, Atlassian, Intuit, T-Mobile, GitLab, DuckDuckGo, Microsoft, IBM, Google, Adobe, Salesforce, VMWare, Xerox, LinkedIn, Cisco, Facebook, Target, Citrix Systems, Twitter, Apple이 있습니다. , Intel,출처.

Nginx는 원래 Igor Sysoev에 의해 만들어졌으며 2004년 10월에 첫 공개 릴리스가 되었습니다. Igor는 처음에 10,000개의 동시 연결을 처리하는 성능 문제와 관련된 문제인 C10k 문제 에 대한 해결책으로 소프트웨어를 생각했습니다.


How does Nginx Work?

Nginx는 낮은 메모리 사용량 과 높은 동시성을 제공하도록 구축되었습니다. 각 웹 요청에 대해 새 프로세스를 생성하는 대신 Nginx는 요청이 단일 스레드에서 처리되는 비동기식 이벤트 중심 접근 방식을 사용합니다.

Nginx를 사용하면 하나의 마스터 프로세스가 여러 워커 프로세스를 제어할 수 있습니다. 마스터는 워커 프로세스를 유지 관리하고 워커는 실제 처리를 수행합니다. Nginx는 비동기식이므로 다른 요청을 차단하지 않고 워커가 각 요청을 동시에 실행할 수 있습니다.

일반적인 기능

  • 캐싱을 이용한 리버스 프록시
  • IPv6
  • 부하분산
  • 캐싱을 이용한 FastCGI 지원
  • 웹소켓
  • 정적파일, 인덱스 파일 및 자동 인덱싱 처리
  • SNI가 있는 TLS/SSL

Nginx vs Apache

아래 그래프에서 nginx 사용률이 전반적으로 1등을 하고 있는 모습을 볼 수 있습니다.

구글 트랜드를 통해 사람들의 관심사를 살펴보면 Apache가 확연히 감소하고 있다는 점이 보입니다.


Web Server 실행중인지 확인 하기

대부분의 웹사이트에서 serverHTTP 헤더를 확인하여 Nginx 또는 Apache인지 확인할 수 있습니다. Chrome Devtools에서 네트워크 탭을 실행하여 HTTP 헤더를 볼 수 있습니다. 또는 Pingdom 또는 GTmetrix 와 같은 도구에서 헤더를 확인할 수 있습니다 .

그러나 HTTP 헤더가 항상 기본 웹 서버를 표시하는 것은 아닙니다. 예를 들어 WordPress 사이트가 Cloudflare와 같은 프록시 서비스 뒤에 있는 경우 serverHTTP 헤더는 대신 cloudflare라고 표시됩니다.


들어가기

Docker로 Nginx 시작하기

pull nginx 이미지

docker pull nginx

nginx container 기동

docker run -it --rm -d -p 8000:80 --name website nginx

container 정지

docker stop website

설치 확인

# 첫 째, 컨테이너 내부에서 명령어 실행
nginx -v
# 둘 째, 컨테이너 외부! 호스트에서 명령어 실행
 docker top website
# 셋 째, curl 클라이언트 툴 이용
curl localhost:8000
# 넷 째, 크롬및 사파리 브라우저에 접속
localhost:8000를 주소창에 입력하여 접속한다. 

잠깐 맛보는 service 유틸리티

  • service nginx start
  • service nginx stop

그렇다면 systemctl vs service 차이는?

service는 wrapper script입니다.
여기서 scriptsms systemd service의 start, stop, status와 관련된 일들을 할 수 있도록 해줍니다.

내부적으로 service script가 initctl 또는 systemctl 또는 /etc/init.d를 사용합니다. 복잡한 일을 할때는 직접 systemctl`등을 이용하지만 start, stop, status 관련 작업은 service script가 더 낫다고 합니다.
예) socket과 관련된 systemd service인 경우 먼저 socket을 stop해줍니다.

참고 - https://askubuntu.com/questions/903354/difference-between-systemctl-and-service-commands

nginx 명령어

nginx사용을 위한 헬프 명령어입니다.

root@a0cb11fd9540:/# nginx -h
nginx version: nginx/1.21.6
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /etc/nginx/)
  -e filename   : set error log file (default: /var/log/nginx/error.log)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

Dockerfile

FROM nginx
COPY ./html/index.html /usr/share/nginx/index.html

docker-compose.yml

version: "3.8"

services:
  nginx:
    build:
      context: .
      dockerfile: ./Dockerfile
    image: nginx_mastery_image
    container_name: nginx_container
    ports:
      - 8000:80
    volumes:
      - ./html/:/usr/share/nginx/html

프로젝트 디렉토리

.
├── Dockerfile
├── README.md
├── docker-compose.yml
└── html
    └── index.html

컴포즈 업!

$ docker-compose up -d
$ docker-compose ps
     Name                    Command               State          Ports        
-------------------------------------------------------------------------------
nginx_container   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:8000->80/tcp
$ ddocker-compose images
   Container          Repository         Tag       Image Id       Size  
------------------------------------------------------------------------
nginx_container   nginx_mastery_image   latest   989b7fd874be   134.5 MB
profile
어제보다 오늘 그리고 오늘 보다 내일...

0개의 댓글