웹사이트 검색

모든 Linux 사용자가 알아야 할 가장 많이 사용되는 Nginx 명령 10가지


Nginx(엔진 x로 발음)는 무료 오픈 소스, 고성능, 확장 가능하고 안정적이며 모든 기능을 갖춘 인기 있는 HTTP 및 역방향 프록시 서버, 메일 프록시 서버 및 일반 TCP/ UDP 프록시 서버.

Nginx는 간단한 구성과 높은 성능으로 인한 낮은 리소스 소비로 잘 알려져 있으며 GitHub 와 같이 웹에서 트래픽이 많은 여러 사이트를 구동하는 데 사용됩니다. , SoundCloud, Dropbox, Netflix, WordPress 등이 있습니다.

또한 읽어 보세요: 모든 Linux 사용자가 알아야 할 3가지 유용한 해킹

이 가이드에서는 개발자나 시스템 관리자가 항상 사용하고 있어야 하는 가장 일반적으로 사용되는 Nginx 서비스 관리 명령 중 일부에 대해 설명합니다. SystemdSysVinit에 대한 명령을 모두 표시합니다.

다음 Nginx 인기 명령 목록은 모두 루트 또는 sudo 사용자로 실행되어야 하며 CentOS와 같은 최신 Linux 배포판에서 작동해야 합니다. , RHEL, Debian, UbuntuFedora.

Nginx 서버 설치

Nginx 웹 서버를 설치하려면 표시된 대로 기본 배포 패키지 관리자를 사용하세요.


sudo yum install epel-release && yum install nginx   [On CentOS/RHEL]
sudo dnf install nginx                               [On Fedora]
sudo apt install nginx                               [On Debian/Ubuntu]

Nginx 버전 확인

Linux 시스템에 설치된 Nginx 웹 서버 버전을 확인하려면 다음 명령을 실행하세요.

nginx -v

nginx version: nginx/1.12.2

위 명령은 단순히 버전 번호를 표시합니다. 버전을 보고 옵션을 구성하려면 표시된 대로 -V 플래그를 사용하십시오.

nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

Nginx 구성 구문 확인

Nginx 서비스를 실제로 시작하기 전에 구성 구문이 올바른지 확인할 수 있습니다. 이는 기존 구성 구조를 변경했거나 새 구성을 추가한 경우 특히 유용합니다.

Nginx 구성을 테스트하려면 다음 명령을 실행하세요.

sudo nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Nginx 구성을 테스트하고 덤프한 후 표시된 대로 -T 플래그를 사용하여 종료할 수 있습니다.

sudo nginx -T
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
configuration file /etc/nginx/nginx.conf:
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/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 {
        }
    }

....

Nginx 서비스 시작

Nginx 서비스를 시작하려면 다음 명령을 실행하세요. 구성 구문이 OK가 아니면 이 프로세스가 실패할 수 있습니다.

sudo systemctl start nginx #systemd
OR
sudo service nginx start   #sysvinit

Nginx 서비스 활성화

이전 명령은 그 동안만 서비스를 시작합니다. 부팅 시 자동 시작을 활성화하려면 다음 명령을 실행합니다.

sudo systemctl enable nginx #systemd
OR
sudo service nginx enable   #sysv init

Nginx 서비스 다시 시작

Nginx 서비스를 다시 시작하려면 서비스를 중지했다가 시작하는 작업입니다.

sudo systemctl restart nginx #systemd
OR
sudo service nginx restart   #sysv init

Nginx 서비스 상태 보기

Nginx 서비스 상태는 다음과 같이 확인할 수 있습니다. 이 명령은 서비스에 대한 런타임 상태 정보를 표시합니다.

sudo systemctl status nginx #systemd
OR
sudo service nginx status   #sysvinit
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@tecmint ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-03-05 05:27:15 EST; 2min 59s ago
 Main PID: 31515 (nginx)
   CGroup: /system.slice/nginx.service
           ├─31515 nginx: master process /usr/sbin/nginx
           └─31516 nginx: worker process

Mar 05 05:27:15 linux-console.net systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 05 05:27:15 linux-console.net nginx[31509]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 05 05:27:15 linux-console.net nginx[31509]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 05 05:27:15 linux-console.net systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Mar 05 05:27:15 linux-console.net systemd[1]: Started The nginx HTTP and reverse proxy server.

Nginx 서비스 다시 로드

Nginx에 구성을 다시 로드하도록 지시하려면 다음 명령을 사용하세요.

sudo systemctl reload nginx #systemd
OR
sudo service nginx reload   #sysvinit

Nginx 서비스 중지

어떤 이유로든 Nginx 서비스를 중지하려면 다음 명령을 사용하세요.

sudo systemctl stop nginx #systemd
OR
sudo service nginx stop   #sysvinit

Nginx 명령 도움말 표시

모든 Nginx 명령 및 옵션에 대한 쉬운 참조 가이드를 얻으려면 다음 명령을 사용하십시오.

systemctl -h nginx
systemctl [OPTIONS...] {COMMAND} ...

Query or send control commands to the systemd manager.

  -h --help           Show this help
     --version        Show package version
     --system         Connect to system manager
  -H --host=[USER@]HOST
                      Operate on remote host
  -M --machine=CONTAINER
                      Operate on local container
  -t --type=TYPE      List units of a particular type
     --state=STATE    List units with particular LOAD or SUB or ACTIVE state
  -p --property=NAME  Show only properties by this name
  -a --all            Show all loaded units/properties, including dead/empty
                      ones. To list all units installed on the system, use
                      the 'list-unit-files' command instead.
  -l --full           Don't ellipsize unit names on output
  -r --recursive      Show unit list of host and local containers
     --reverse        Show reverse dependencies with 'list-dependencies'
     --job-mode=MODE  Specify how to deal with already queued jobs, when
                      queueing a new job
     --show-types     When showing sockets, explicitly show their type
  -i --ignore-inhibitors
...

다음 Nginx 관련 기사를 읽고 싶을 수도 있습니다.

  1. Nginx 웹 서버의 성능을 보호, 강화 및 개선하기 위한 최고의 가이드
  2. Amplify – NGINX 모니터링이 쉬워졌습니다
  3. ngxtop – Linux에서 실시간으로 Nginx 로그 파일 모니터링
  4. 가상 호스트 및 SSL 인증서를 사용하여 Nginx를 설치하는 방법
  5. Linux에서 Nginx 서버 버전을 숨기는 방법

지금은 여기까지입니다! 이 가이드에서는 Nginx 시작, 활성화, 다시 시작 및 중지를 포함하여 가장 일반적으로 사용되는 Nginx 서비스 관리 명령 중 일부를 설명했습니다. 추가할 사항이나 질문이 있는 경우 아래 피드백 양식을 사용하세요.