Nginx configuration and commands

Nginx is a high-performance web server and reverse proxy server. Due to its excellent performance and reliability, more and more websites are beginning to use Nginx as their web server. The configuration and commands of Nginx are very flexible and can meet various complex needs. This article will introduce the configuration and commands of Nginx in detail.

1. Installation and startup of Nginx

The installation of Nginx is very simple. You can install it through a package manager such as yum or apt-get, or you can download the source code from the official website for compilation and installation. After the installation is complete, you can start Nginx with the following command:

nginx

If you need to start Nginx with a specified configuration file, you can use the following command:

nginx -c /path/to/nginx.conf

2. Nginx configuration file

The Nginx configuration file is a text file, usually located at /etc/nginx/nginx.conf or /usr/local/nginx/conf/nginx.conf. A configuration file consists of multiple blocks, each block surrounded by a pair of curly braces {}. The following is a simple Nginx configuration file example:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log /var/log/nginx/access.log;

    sendfile on;
    keepalive_timeout 65;

    server {
        listen 80;
        server_name example.com;

        location / {
            root /var/www/example.com;
            index index.html;
        }
    }
}

The above configuration file contains three blocks: user, events and http. The user block is used to specify the user and group under which Nginx runs, the events block is used to specify Nginx's event model and connection limit, and the http block is used to specify HTTP protocol-related configurations. In the http block, multiple server blocks can be defined, each server block corresponding to a virtual host.

3. Commonly used commands of Nginx

1. Start Nginx

nginx

2. Stop Nginx

nginx -s stop

3. Restart Nginx

nginx -s reload

4. Check the Nginx process

ps aux | grep nginx

5. Check whether Nginx configuration is correct

nginx -t

4. Nginx module

Nginx supports many modules through which the functionality of Nginx can be extended. Here are some commonly used modules:

1. HttpRewriteModule: used for URL rewriting.

2. HttpProxyModule: used for reverse proxy.

3. HttpFastcgiModule: used for FastCGI protocol.

4. HttpSslModule: used for SSL protocol.

5. HttpGzipModule: used to compress response data.

6. HttpLimitReqModule: used to limit the request rate.

7. HttpLimitConnModule: used to limit the number of connections.

8. HttpCacheModule: used to cache response data.

5. Optimization of Nginx

1. Adjust worker_processes parameters

The worker_processes parameter specifies the number of worker processes opened by Nginx. The default value is 1. If the server has multiple CPU cores, worker_processes can be set to the number of CPU cores.

2. Adjust worker_connections parameters

The worker_connections parameter specifies the maximum number of connections that each worker process can handle. The default value is 512. If the server has more concurrent connections, worker_connections should be adjusted to a larger value.

3. Enable TCP_NODELAY option

The TCP_NODELAY option can disable the Nagle algorithm and improve TCP transmission efficiency. The following configuration can be added in the http block:

tcp_nodelay on;

4. Enable TCP_NOPUSH option

The TCP_NOPUSH option can disable the TCP send buffer and improve data sending efficiency. The following configuration can be added in the http block:

tcp_nopush on;

5. Enable sendfile option

The sendfile option can take advantage of the zero-copy technology of the operating system to improve file transfer efficiency. The following configuration can be added in the http block:

sendfile on;

6. Summary

This article introduces Nginx configuration and commands, as well as common modules and optimization methods. Through reasonable configuration and optimization, Nginx can achieve maximum performance and reliability. If you are using Nginx as a web server or reverse proxy server, I hope this article will be helpful to you.

おすすめ

転載: blog.csdn.net/zhengren964/article/details/131600027