1. Nginx 소개

Nginx는 이벤트 기반 비동기 웹 서버로서, 클라이언트 요청을 처리하기 위해 단일 스레드로 실행되는 구조로,

높은 처리량과 낮은 지연 시간을 제공하며, 많은 클라이언트 요청을 동시에 처리할 수 있다.

2. Linux에서 yum으로 Nginx package 설치 방법

sudo yum update
sudo yum install -y epel-release
sudo yum install -y nginx

3. Nginx 기본 설정 파일 및 소개

1) 기본설정파일 위치: /etc/nginx/nginx.conf, 로그파일위치: /var/log/nginx/access.log

2) 설정파일 구성

  - http 블록: 모든 HTTP 구성을 정의한다. 예를 들어, 서버 블록과 로깅 및 캐시 설정 등이 여기에 포함된다.
  - server 블록: 특정 서버의 구성을 정의한다. 예를 들어, 도메인 이름, 포트 번호, SSL 인증서 및 백엔드 서버 등이 여기에 포함된다.
  - location 블록: 특정 URL 경로에 대한 구성을 정의한다. 예를 들어, 프록시 패스 및 캐시 지시문 등이 여기에 포함된다.

3) 설정파일 초기내용

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

4. nginx 가동 및 중지 스크립트

1) 관리용 스크립트

cat  > start.sh <<EOF
#!/bin/sh
systemctl start nginx
EOF

cat  > stop.sh <<EOF
#!/bin/sh
systemctl stop nginx
EOF

cat  > status.sh <<EOF
#!/bin/sh
echo "########## nginx status ########"
systemctl status nginx

echo "########## nginx process ########"
ps -auxf | grep nginx

echo "########## netstat  ########"
netstat -plnt
EOF

chmod +x start.sh stop.sh status.sh

2) 가동 후 상태 체크

  - nginx가 정상적으로 가동되고, 80포트로 LISTEN되어있는것으로 확인된다.

[root@rocky8: /APP/nginx_binary/bin]# ./start.sh
[root@rocky8: /APP/nginx_binary/bin]# ./status.sh
########## nginx status ########
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2023-03-27 08:25:42 EDT; 4s ago
  Process: 1795 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 1792 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 1790 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 1796 (nginx)
    Tasks: 5 (limit: 49275)
   Memory: 16.4M
   CGroup: /system.slice/nginx.service
           ├─1796 nginx: master process /usr/sbin/nginx
           ├─1797 nginx: worker process
           ├─1798 nginx: worker process
           ├─1799 nginx: worker process
           └─1800 nginx: worker process

Mar 27 08:25:42 rocky8.linuxvmimages.local systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 27 08:25:42 rocky8.linuxvmimages.local nginx[1792]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 27 08:25:42 rocky8.linuxvmimages.local nginx[1792]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 27 08:25:42 rocky8.linuxvmimages.local systemd[1]: Started The nginx HTTP and reverse proxy server.
########## nginx process ########
root        1805  0.0  0.0  12144  1088 pts/0    S+   08:25   0:00                              \_ grep nginx
root        1796  0.0  0.0 120352  2240 ?        Ss   08:25   0:00 nginx: master process /usr/sbin/nginx
nginx       1797  0.0  0.1 153068  8100 ?        S    08:25   0:00  \_ nginx: worker process
nginx       1798  0.0  0.1 153068  8196 ?        S    08:25   0:00  \_ nginx: worker process
nginx       1799  0.0  0.1 153068  8196 ?        S    08:25   0:00  \_ nginx: worker process
nginx       1800  0.0  0.1 153068  8116 ?        S    08:25   0:00  \_ nginx: worker process
########## netstat  ########
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1796/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1069/sshd
tcp6       0      0 :::80                   :::*                    LISTEN      1796/nginx: master
tcp6       0      0 :::22                   :::*                    LISTEN      1069/sshd

 - 80포트로 Test Page 접속한 화면

3) 중지 후 상태 체크

[root@rocky8: /APP/nginx_binary/bin]# ./stop.sh
[root@rocky8: /APP/nginx_binary/bin]# ./status.sh
########## nginx status ########
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

Mar 27 08:29:37 rocky8.linuxvmimages.local systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Mar 27 08:29:37 rocky8.linuxvmimages.local systemd[1]: nginx.service: Succeeded.
Mar 27 08:29:37 rocky8.linuxvmimages.local systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Mar 27 16:51:03 rocky8.linuxvmimages.local systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 27 16:51:03 rocky8.linuxvmimages.local nginx[2024]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 27 16:51:03 rocky8.linuxvmimages.local nginx[2024]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 27 16:51:03 rocky8.linuxvmimages.local systemd[1]: Started The nginx HTTP and reverse proxy server.
Mar 27 16:51:13 rocky8.linuxvmimages.local systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Mar 27 16:51:13 rocky8.linuxvmimages.local systemd[1]: nginx.service: Succeeded.
Mar 27 16:51:13 rocky8.linuxvmimages.local systemd[1]: Stopped The nginx HTTP and reverse proxy server.
########## nginx process ########
root        2050  0.0  0.0  12144  1156 pts/0    S+   16:51   0:00                              \_ grep nginx
########## netstat  ########
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1069/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      1069/sshd

태그: , ,

카테고리:

업데이트:

댓글남기기