Introduction
Before we begin talking about Nginx Commands, let's briefly understand – What is Nginx?
Nginx, pronounced "engine x," is a free, open-source, high-performance HTTP and reverse proxy server that helps some of the world's most popular websites handle their traffic. It works as a standalone web server as well as a reverse proxy for Apache and other web servers.
You probably deal with Nginx on a frequent basis if you're a developer or a system administrator.
In this tutorial, we'll go over the most critical and commonly used Nginx commands, such as starting, halting, and restarting Nginx. We will also address a few FAQs on Nginx commands.
Before You Begin
We'll assume you're logged in as root or as a user with sudo access. Any recent Linux distribution, such as Ubuntu 18.04, CentOS 8, or Debian 10, should work with the commands in this guide.
Starting Nginx
It's easy to get Nginx up and running. Simply enter the following command:
sudo systemctl start nginx
The command does not provide any output if it succeeds.
To start Nginx on a Linux distribution that does not use systemd
, type:
sudo service nginx start
Instead of starting the Nginx service manually, you should set it to start automatically when the system boots up:
sudo systemctl enable nginx
Stopping Nginx
Even if there are open connections, stopping Nginx promptly shuts off all Nginx worker processes.
Run one of the following commands to stop Nginx:
sudo systemctl stop nginx
sudo service nginx stop
Restarting Nginx
The restart option allows you to quickly stop and start the Nginx server.
To restart Nginx, type one of the following commands:
sudo systemctl restart nginx
sudo service nginx restart
This is the command that you will almost certainly use the most.
Reloading Nginx
When you make changes to Nginx's settings, you must reload or restart it.
The reload command loads a new configuration, start new worker processes with it, and gracefully kills down existing worker processes.
Use one of the following commands to reload Nginx:
sudo systemctl reload nginx
sudo service nginx reload
Testing Nginx Configuration
It's a good idea to test the configuration before restarting or reloading the Nginx server whenever you make changes to the configuration file.
To check for any syntactic or system issues in the Nginx configuration, run the following command:
sudo nginx -t
You will get an output like the below:
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
The command provides a descriptive message if there are any errors.
Viewing Nginx Status
Use the following command to verify the status of the Nginx service:
sudo systemctl status nginx
You will get an output like the below:
Output
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
Checking Nginx Version
You might need to know the version of Nginx you're running to troubleshoot an issue or see if a certain feature is available.
You can find out what version of Nginx you're running by typing:
sudo nginx -v
Output
nginx version: nginx/1.14.0 (Ubuntu)
Along with the configure option, the -V
option displays the Nginx version.
sudo nginx -V
FAQs on Nginx Commands
How do I start/stop/restart Nginx?
To start Nginx, use the command: sudo service nginx start
. To stop it, use: sudo service nginx stop
. And to restart, use: sudo service nginx restart
.
How can I check the Nginx configuration for syntax errors?
Use the command: sudo nginx -t
or sudo nginx -T
to test the configuration file for any syntax errors. It will highlight any issues if present.
How can I view the Nginx server logs?
The Nginx access logs and error logs are typically located in the /var/log/nginx/
directory. Use commands like sudo tail -f /var/log/nginx/access.log
or sudo tail -f /var/log/nginx/error.log
to view the logs in real-time.
How can I reload the Nginx configuration without restarting the server?
To reload the Nginx configuration, use the command: sudo service nginx reload
. This allows you to apply any changes in the configuration without interrupting the server.
How do I test a specific Nginx server block configuration?
You can test a specific server block configuration by temporarily placing it in a separate file under the /etc/nginx/conf.d/
directory and using the sudo nginx -t -c /path/to/your/config
command to test it.
Can I force Nginx to close all connections immediately?
Yes, you can do a fast shutdown of Nginx by sending the QUIT signal to the master process using the command: sudo nginx -s quit
.
How can I enable SSL/TLS for Nginx?
To enable SSL/TLS, you need a valid SSL certificate. Configure the certificate paths, add an HTTPS server block, specify the certificate details, and define the SSL protocols and ciphers to secure Nginx with SSL/TLS.
Conclusion
We've covered some of the most important Nginx commands in this tutorial. Visit the Nginx manual to learn more about the command-line options for Nginx.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.