SoFunction
Updated on 2025-04-06

Detailed explanation of common Nginx commands and deployments and practical case demonstrations

1. Common Nginx commands

1.1 Start Nginx

To start the Nginx service, you can use the following command:

sudo systemctl start nginx

1.2 Stop Nginx

If you need to stop the Nginx service, you can use the following command:

sudo systemctl stop nginx

1.3 Restart Nginx

After modifying the Nginx configuration file, you need to restart Nginx for the changes to take effect. The following commands can be used:

sudo systemctl restart nginx

1.4 Reload the configuration

When the configuration file is modified but you do not want to stop the service, you can reload the configuration using the following command:

sudo systemctl reload nginx

1.5 View Nginx Status

To view the current running status of Nginx, you can use the following command:

sudo systemctl status nginx

This will show if Nginx is running, and its related information.

1.6 Test configuration file

After modifying the Nginx configuration file, you can use the following command to test the correctness of the configuration:

sudo nginx -t

If the configuration file is correct, the information of "syntax is ok" and "test is successful" will be returned. If there is an error, the specific error message and the number of lines of the configuration file are given.

1.7 View Nginx logs

Nginx's error log and access log are usually located in/var/log/nginxin the directory. The following commands can view the error log in real time:

tail -f /var/log/nginx/

Access logs can be viewed using similar commands:

tail -f /var/log/nginx/

1.8 View Nginx Process

To view the currently running Nginx process, you can use the following command:

ps aux | grep nginx

This lists all Nginx-related process information, including the main process and worker process.

1.9 Configure Nginx to boot

To set Nginx to start automatically when the system starts, you can use the following command:

sudo systemctl enable nginx

1.10 Disable Nginx boot

If you need to disable Nginx to start automatically when the system starts, you can use the following command:

sudo systemctl disable nginx

2. Deploy Nginx on CentOS

2.1 Install Nginx

Installing Nginx on CentOS can be done through EPEL (Extra Packages for Enterprise Linux) repository or source code compilation. The following is a method of installing using yum:

# Install EPEL repositorysudo yum install epel-release

# Install Nginxsudo yum install nginx

# Start Nginxsudo systemctl start nginx

# Set up the startup self-startsudo systemctl enable nginx

2.2 Verify installation

After the installation is complete, you can check the status of Nginx through the following command:

sudo systemctl status nginx

Then enter in the browserhttp://your_server_ip, If you see the welcome page of Nginx, it means the installation is successful.

2.3 Nginx configuration file

The main configuration file of Nginx is usually located in/etc/nginx/. Here is a basic configuration example:

worker_processes auto;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/;
    default_type application/octet-stream;

    sendfile on;
    keepalive_timeout 65;

    server {
        listen 80;
        server_name localhost;

        location / {
            root /usr/share/nginx/html;  # Static file path            index  ;   # Default homepage        }

        error_page 404 /;       # Customize 404 pages        location = / {
            internal;
        }
    }
}

3. Deploy Nginx in Docker

3.1 Docker installation

First, you need to install Docker on CentOS:

# Update yumsudo yum update

# Install the necessary dependenciessudo yum install -y yum-utils device-mapper-persistent-data lvm2

# Add Docker's official yum sourcesudo yum-config-manager --add-repo /linux/centos/

# Install Dockersudo yum install docker-ce

# Start Dockersudo systemctl start docker

# Set up the startup self-startsudo systemctl enable docker

3.2 Pulling Nginx Image

sudo docker pull nginx

3.3 Running Nginx container

Here are the commands to start the Nginx container:

sudo docker run --name my-nginx -p 80:80 -d nginx

This command runs Nginx in the background and maps it to port 80 of the host.

4. Docker mounts HTML files to achieve real-time refresh of data

In actual development, we may need to update the static resources provided by Nginx in real time. This can be achieved through Docker's mount function.

4.1 Prepare HTML files

Create an HTML folder and add a simple HTML file to it.

mkdir ~/nginx-html
echo "<h1>Hello, Nginx!</h1>" > ~/nginx-html/

4.2 Start the Nginx container and mount the directory

Use the following command to start the Nginx container and mount the host's HTML folder into the container:

sudo docker run --name my-nginx -p 80:80 -v ~/nginx-html:/usr/share/nginx/html -d nginx

In this command,-v ~/nginx-html:/usr/share/nginx/htmlPut the host's~/nginx-htmlThe directory mounted to the Nginx container/usr/share/nginx/htmlin the directory.

4.3 Real-time refresh of data

Revise~/nginx-html/Nginx will automatically load new content for the content of the file. For example, you can add the following content:

<h1>Hello, Nginx! Updated!</h1>

After saving the file, refresh the browser to see the updated content.

5. Case of using Nginx's data analysis system

In data analysis systems, Nginx can be used as a static file server and reverse proxy to improve system performance and security. Here is a simple use case:

5.1 Scene Description

Suppose we have a data analysis system, the front-end is built using React, and the back-end is provided with APIs. We want to distribute static files and API requests to different services via Nginx.

5.2 Nginx configuration example

server {
    listen 80;
    server_name ;

    # Static file service    location / {
        root /usr/share/nginx/html;  # Static file path for front-end build        index ;
        try_files $uri $uri/ /;
    }

    # Reverse proxy to backend    location /api {
        proxy_pass http://localhost:3000;  # Service Address        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

VI. Performance optimization

In high concurrency scenarios, it is very important to optimize Nginx's performance. Here are some common optimization methods:

6.1 Adjust the work process and connection number

Adjust according to the number of CPU cores of the serverworker_processesandworker_connectionsParameters:

worker_processes auto;  # Automatically set to CPU core numberworker_connections 2048;  # Increase the maximum number of connections

6.2 Turn on cache

Relieve the pressure on the backend server by enabling caching:

proxy_cache_path /tmp/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;

6.3 Compressed Transmission

Reduce data transfer by turning on gzip compression:

gzip on;
gzip_types text/plain application/json application/javascript text/css;
gzip_min_length 1000;  # Only greater than1000Only bytes of files are compressed

Summarize

This is the article about the detailed explanation of common Nginx commands and deployments and practical cases. For more related common Nginx commands and deployment content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!