Top 10 Nginx Commands to Help You Manage Your Server Like a Pro

Updated July 21, 2020・5 minute read Post By serverenthusiast
Top 10 Nginx Commands to Help You Manage Your Server Like a Pro

Introduction

If you have ever done any web development, it is very likely that you've heard of Nginx.

Nginx is an open-source web server. It can also be used as a reverse proxy, a caching server, a load balancing, even a media streaming server, and more.

In this tutorial, I will show you 10 commands that will help you manage your Nginx server like a pro SysAdmin!

Prerequisites

Before you get started you would need to have a Linux server with Nginx installed.

I would recommend using DigitalOcean if you do not have an account you can use the following link to get a $100 free credit and spin up your own servers in seconds:

DigitalOcean $100 Free Credit

Nginx Installation

In order to install Nginx all you need to do is run one of the following commands depending on your Linux distribution:

  • Install Nginx on CentOS
sudo yum install epel-release && yum install nginx
  • Install Nginx on Ubuntu or Debian:
sudo apt update && sudo apt install nginx

Once you have Nginx installed, we are ready to proceed with the actual commands:

Check Nginx Version

In order to check the exact Nginx version that has been installed on your server, you have to use the following command:

nginx -v

You will get the following output:

nginx version: nginx/1.14.0 (Ubuntu)

This is just the Nginx version number. In order to get some additional information, you can use a capital -V argument instead:

nginx -V

Output:

nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.1  11 Sep 2018
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-GkiujU/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -
Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --
modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-tem
p-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_mod
ule --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --w
ith-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module

Nginx Config Test

This is probably my favorite Nginx command, I use it very often, basically after every single configuration change and before every Nginx restart.

This is very handy as it will show you if there are any configuration problems that could prevent your Nginx service from starting.

Syntax:

sudo nginx -t

Output:

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

In the example above there are no error and the config test was successful. This means that you can safely go ahead and restart Nginx if needed.

If you use a capital -T, this would dump all of your Nginx configurations, this is quite handy as well as you will be able to see what the exact config that your Nginx service is currently running with:

sudo nginx -T

Output:

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:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;
...

Starting Nginx

Once you are sure that your Nginx config is correct and that there are no errors, you can start the Nginx service with the following systemctl command:

sudo systemctl start nginx

Enabling Nginx

In order for Nginx to start after a possible server reboot, you need to enable it. The exact systemctl command that you would need to use is:

sudo systemctl enable nginx

That way if you reboot your server, you don't have to manually start the Nginx server every time.

Checking Nginx Status

In order to see if the Nginx service is running and how long it has been running for you can use this systemctl command:

sudo systemctl status nginx

Output:

? nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; disabled; vendor preset: enabled)
  Drop-In: /etc/systemd/system/nginx.service.d
           ??override.conf
   Active: active (running) since Mon 2020-06-15 15:15:28 UTC; 1 months 5 days ago
     Docs: man:nginx(8)
 Main PID: 12233 (nginx)
    Tasks: 3 (limit: 4702)
   CGroup: /system.slice/nginx.service
           ??12233 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ??12834 nginx: worker process
           ??12835 nginx: worker process

Jun 15 15:15:28 do-feedback-bobby systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 15 15:15:28 do-feedback-bobby systemd[1]: Started A high performance web server and a reverse proxy server.

Restarting and reloading Nginx

If you've made any changes to your Nginx service, in order for those new changes to take effect you need to reload the service:

sudo systemctl reload nginx

This would not kill the Nginx process and if you check your Nginx status again you will be able to see that the uptime of the service is not changed.

Restarting the service on the other side, would actually stop the current process and spin up a new one:

sudo systemctl restart nginx

If you run systemctl status nginx right after that, you will see that the uptime is just a couple of seconds.

Getting Nginx Help

In order to get a quick manual for the nginx command, you can just add the -h argument as follows:

systemctl -h nginx

Output:

# nginx -h
nginx version: nginx/1.14.0 (Ubuntu)
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-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: /usr/share/nginx/)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

Troubleshooting Nginx problems

If you are getting any kind of error, the best place to look at is the Nginx error log.

The location of the Nginx error log is /var/log/nginx/error.log. You can the tail command to get the latest entries from that log:

tail -f /var/log/nginx/error.log

Checking Nginx access logs

More often than not you might see some spikes in your CPU, those might be a result of a brute force attack or some malicious activity against your website. To verify what kind of requests are being handled by your Nginx server, you should keep an eye on your access logs.

To do so you could use again the tail command:

tail -f /var/log/nginx/access.log

I also recommend checking out this cool Bash script which would summarize your entire Nginx log and provide you with some nice insights:

BASH Script to Summarize Your NGINX and Apache Access Logs

Conclusion

Now you know how to install and manage an Nginx server! The next step would be to learn some more advanced Nginx configuration. To do so I strongly recommend checking out this Opensource Nginx Configuration Tool!

Hope that this helps!

Still have a question?

Get help or discuss anything development/design related.

Start a discussion