25 Nginx commands that developers and administrators must master (middle)

Nginx is one of the most popular web servers among Linux and BSD users because of its rich functional instruction set and superior flexibility. If you are a web developer, then you may be using Nginx or Apache server. Therefore, it is important to understand in depth how Nginx works from the command line. Fortunately, you can improve your DevOps skills by mastering some Nginx commands. Our editors have selected these frequently used commands for you and provided a comprehensive discussion for starting Linux administrators. Then please read this article to learn more about these commands.

Nginx commands for Linux administrators

Unlike many web servers, Nginx only uses configuration files to handle server configuration. Therefore, there are few command line parameters that you can use. This is useful because administrators can quickly find the command they are looking for. Here are some widely used Nginx commands, you can use them now.

9. Display Nginx version information

Some newer features of Nginx are only available in the latest version. Therefore, administrators may encounter some compatibility issues on older servers. You can easily determine your Nginx version to see if your issue is related to the server version.

The following commands can be used to display additional information about Nginx installation, such as configuration variables and compiler version.

sudo systemctl -V nginx

25 Nginx commands that developers and administrators must master (middle)

sudo apt-cache policy nginx

10. Display the help page

The Nginx help page is a good reference point for beginners and experienced Linux administrators. You can view this reference from the Linux terminal emulator using one of the following Nginx commands.

sudo service nginx -h  #System V Init使用

25 Nginx commands that developers and administrators must master (middle)

Reference help pages are always a good choice because they allow you to quickly understand the options available to users.

11. Use alternative configuration

The Nginx web server provides an endless list of customized functions. Administrators can easily adjust Nginx configuration files to add other features to their servers. The default Nginx configuration file on most systems is /etc/nginx/nginx.conf. However, it is usually best to implement customization on other configuration files first.

Please note that we assume that your new configuration file is named test.conf. Replace this section with the actual name of your configuration file. Now you can test the new configuration using the following command.

You can also instruct Nginx to look for the default configuration in other directories when compiling from source code. You only need to pass this directory when configuring the installation.

./configure --conf-path=/etc/some/other/nginx.conf

12. Prohibit the display of non-error messages

When testing a new configuration, you usually receive an error message. However, when checking for customizations, you will also get a lot of irrelevant information. Fortunately, the Nginx daemon provides a simple option to suppress these non-error messages, as shown below.

13. Change the global order

The global directive contains all configuration parameters available to the Nginx server. This is one of the most sensitive parts of your web server and requires careful attention. The -g option allows administrators to define personalized instructions for their web server Nginx.

sudo nginx -g "pid /var/run/test.pid; worker_processes 2;"

This command specifies the global Nginx instruction for the PID and defines the number of worker processes, in this case it is 2. Now, we can test this using the alternate configuration file we used previously.

sudo nginx -t -c ~/test.conf -g "pid /var/run/test.pid; worker_processes 2;"

14. Change the Nginx prefix path

The prefix path contains all files used by the Nginx web server. It is also a directory for configuration setting relative paths (except library source). By default, Nginx uses the / usr / local / nginx directory as a prefix. The following command demonstrates how to override it from the command line.

sudo nginx -p /path/to/new/prefix

The -p flag allows us to pass the new prefix position. When testing a new server, it usually comes in handy. However, this option does not apply to Nginx servers with versions lower than 0.7.53.

15. Check the stub_status module

The stub_status module exposes some very important indicators about Nginx. Administrators often use it to monitor their Linux mail servers or proxy servers. Although all pre-built Nginx binaries are provided with this module, if you have compiled Nginx from source, your Nginx server may not have this binary. You can use the following command to test whether it is installed.

sudo nginx -V 2>&1 | grep --color -- --with-http_stub_status_module

If you find that this module is missing, you can always rebuild Nginx from the source code. As shown below, you only need to include the –with-http_stub_status_module parameter when configuring Nginx.

./configure --with-http_stub_status_module

16. Check the Nginx path

There are several Nginx commands that can be used to check the Nginx installation path. Just like daily Linux terminal commands, you can always use the which / whereis command to check the Nginx path.

The above command will display all system locations that contain files related to Nginx settings. You can do it another way by using the ps command and grepping the required information.

25 Nginx commands that developers and administrators must master (middle)

You should now be able to clearly see the location of the Nginx daemon. This is very useful for developers who do not have the underlying privileges of the server machine.

Guess you like

Origin www.linuxidc.com/Linux/2020-04/162935.htm