10 הפקודות הנפוצות ביותר של Nginx שכל משתמש ב-Linux חייב לדעת

נגינקס (הוגן כ"הג'ינג'ין אקס") הוא שרת HTTP ופרוקסי הפוך, שרת פרוקסי דואר ושרת פרוקסי TCP/UDP ג'נרי.

נגינקס מוכר כתוכנה פופולרית בזכות הגדרתו הפשוטה וצריכת המשאבים הנמוכה שלו בשל ביצועיו הגבוהים. הוא משמש להפעלת אתרים בעלי תעבורת גבוהה ברשת, כגון GitHub, SoundCloud, Dropbox, Netflix, WordPress ועוד.

קראו גם: 3 פתרונות שימושיים שכל משתמשי Linux חייבים לדעת

במדריך זה, נסביר מספר מהפקודות הנפוצות ביותר לניהול של שירותי Nginx שכמפתח או מנהל מערכת, עליכם להכיר. נציג פקודות לשני סוגי שירותי: Systemd ו־SysVinit.

כל הפקודות הפופולריות של Nginx חייבות להתבצע כמשתמש root או sudo וצריך לעבוד על כל הפצות Linux מודרניות כמו CentOS, RHEL, Debian, Ubuntu ו־Fedora.

התקנת שרת 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

כדי לבדוק את הגרסה של שרת האינטרנט Nginx שמותקן על מערכת ה־Linux שלך, הרץ את הפקודה הבאה.

$ nginx -v

nginx version: nginx/1.12.2

הפקודה לעיל פשוט מציגה את מספר הגרסה. אם ברצונך לראות את הגרסה ואפשרויות התצורה, השתמש בדגל -V כפי שמוצג.

$ nginx -V
Show Nginx, Compiler and Configuration Parameters
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
Show Nginx Configuration Settings
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
Show Nginx Status Information
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 tecmint.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 05 05:27:15 tecmint.com nginx[31509]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 05 05:27:15 tecmint.com nginx[31509]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 05 05:27:15 tecmint.com systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Mar 05 05:27:15 tecmint.com 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
Nginx Help Commands and Options
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 – לפקוד קבצי יומן של Nginx בזמן אמת ב-Linux
  4. איך להתקין את Nginx עם מארחים ותעודת SSL
  5. איך להסתיר את גרסת השרת של Nginx ב-Linux

זהו הכל לעכשיו! במדריך זה, הסברנו על חלק מהפקודות לניהול שירות של Nginx הנפוצות ביותר שכדאי שתדע, כולל פתיחה, הפעלה, איתור ועצירת Nginx. אם יש לך הוספות או שאלות לשאול, השתמש בטופס משוב למטה.

Source:
https://www.tecmint.com/useful-nginx-command-examples/