Read in 3 minutes

last updated 

Nginx Commands You Should Know

Nginx pronounced “engine x” is a free, open-source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet. It can be used as a standalone web server, and as a reverse proxy for Apache and other web servers.

If you are developer or system administrator chances are that you’re dealing with Nginx on a regular basis.

In this guide, we will go over the most important and frequently used Nginx commands including starting, stopping, and restarting Nginx.

Before You Begin

All of the commands must be executed as sudo or root user and should work on any modern Linux distribution like Ubuntu 18.04 and CentOS 7 and Debian 9.

Start Nginx

Starting Nginx is pretty simple. Just run the following command:

sudo systemctl start nginx

On success, the command doesn’t produce any output.

If you are running a Linux distribution without systemd to start Nginx type:

sudo service start nginx

Instead of manually starting the Nginx service it is recommended to set it to start on system boot:

sudo systemctl enable nginx

Stop Nginx

Stopping Nginx will quickly shut down all Nginx worker processes even if there are open connections.

To stop Nginx, run one of the following commands:

sudo systemctl stop nginx
sudo service stop nginx

Restart Nginx

The restart option is a quick way of stopping and then starting the Nginx server.

Use one of the following commands to perform an Nginx restart:

sudo systemctl restart nginx
sudo service restart nginx

This is the command that you will probably use the most frequently.

Reload Nginx

You need to reload or restart Nginx whenever you make changes to its configuration.

The reload option will load the new configuration, start new worker processes with the new configuration and gracefully shut down old worker processes.

To reload Nginx, use one of the following commands:

sudo systemctl reload nginx
sudo service reload nginx

Test Nginx Configuration

Whenever you make changes to the Nginx server’s configuration file it is a good idea to test the configuration before restarting or reloading the service.

Use the following command to test the Nginx configuration for any syntax or system errors:

sudo nginx -t

The output will look something like this.

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

If there are any errors the command will print a detailed message.

View Nginx Status

To check the status of the Nginx service, use following command:

sudo systemctl status nginx

The output will look something like this:

 nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-04-21 13:57:01 PDT; 5min ago
     Docs: man:nginx(8)
  Process: 4491 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 4502 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 4492 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 4504 (nginx)
    Tasks: 3 (limit: 2319)
   CGroup: /system.slice/nginx.service
           |-4504 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           |-4516 nginx: worker process
           `-4517 nginx: worker process

Check Nginx Version

Sometimes you may need to know the version of your Nginx so you can debug an issue or determine whether a certain feature is available.

You can check your Nginx version by running:

sudo nginx -v
nginx version: nginx/1.14.0 (Ubuntu)

The -V option will output the Nginx version along with the configure option.

sudo nginx -V

Conclusion

In this guide, we have shown you some of the most essential Nginx commands. If you want to learn more about the Nginx command line visit the Nginx documentation