SoFunction
Updated on 2025-04-08

The entire tutorial on configuration and maintenance of Nginx in Linux

1. Introduction to Nginx

Nginx is a high-performance open source HTTP and reverse proxy server known for its high concurrency processing capabilities and low resource consumption. It supports a variety of functions, including load balancing, reverse proxy, static file service, etc. Nginx's configuration files are text-based and easy to understand and modify, making them one of the preferred tools for web development and operation staff.

2. Installation of Nginx

1. Preparation before installation

Before installing Nginx, make sure your Linux system has the necessary compilation tools and libraries installed. If not installed, you can use the following command to install:

yum -y install gcc gcc-c++ autoconf automake make

2. Install Nginx

The following are the steps to install Nginx based on source code:

  • Download Nginx source code package:
wget /download/nginx-1.24.
  • Unzip the source code package:
tar -zxvf nginx-1.24.
  • Enter the decompressed directory:
cd nginx-1.24.0
  • Configure Nginx and specify the installation path:
./configure --prefix=/usr/local/nginx
  • Compile and install:
make && make install
  • Start Nginx:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/
  • Check whether Nginx starts successfully:
ps -ef | grep nginx
  • If you seenginx: master processandnginx: worker process, it means that Nginx has been started successfully.

3. Set Nginx to power on

Add Nginx to the system's startup script:

vim /etc//

Add the following at the end of the file:

/usr/local/nginx/sbin/nginx

Save and exit.

3. Detailed explanation of Nginx configuration file

The configuration file of Nginx is located in/usr/local/nginx/conf/, Here are the main parts of the configuration file and their description:

1. Global configuration

user nobody;  # Specify the user running Nginxworker_processes 1;  # Number of worker processes,It is recommended to set it to CPU Number of cores

2. events block

events {
    worker_connections 1024;  # Maximum number of connections per worker process}

3. http block

http {
    include ;  # Include file type definition    default_type application/octet-stream;  # Default file type    sendfile on;  # Enable efficient file transfer mode    keepalive_timeout 65;  # Long connection timeout 
    server {
        listen 80;  # Listen to the port        server_name localhost;  # Server Name 
        location / {
            root html;  # Website root directory            index  ;  # Default homepage file        }
 
        error_page 500 502 503 504 /;  # Error page        location = / {
            root html;
        }
    }
}

The above is a simple example of Nginx configuration file.

4. Common commands for Nginx

Start Nginx

/usr/local/nginx/sbin/nginx

Stop Nginx

/usr/local/nginx/sbin/nginx -s stop

Smooth restart Nginx

/usr/local/nginx/sbin/nginx -s reload

Check configuration file syntax

/usr/local/nginx/sbin/nginx -t

5. Nginx maintenance and optimization

1. Log Management

Nginx's logs are stored by default/usr/local/nginx/logsIn the directory. It can be done through the configuration fileaccess_loganderror_logThe instruction specifies the location and format of the log file. For example:

access_log /usr/local/nginx/logs/ main;
error_log /usr/local/nginx/logs/ warn;

2. Performance optimization

  1. Adjust the number of work processes: Willworker_processesSet to the number of CPU cores.

  2. Optimize the number of connections: Enlargeworker_connectionsvalue to support more concurrent connections.

  3. Enable Gzip compression: existhttpAdd the following configuration to the block:

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_types text/plain application/javascript application/json;

3. Security configuration

Restrict access: useallowanddenyDirectives restrict access to specific resources. For example:

location /admin {
    allow 192.168.1.0/24;
    deny all;
}

Hide version information: existhttpAdd the following configuration to the block:

server_tokens off;

6. Advanced applications of Nginx

1. Reverse proxy

Nginx can act as a reverse proxy server to forward requests to the backend server. For example:

server {
    listen 80;
    server_name ;
 
    location / {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

2. Load balancing

Nginx can implement simple load balancing. For example:

upstream backend {
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
}
 
server {
    listen 80;
    server_name ;
 
    location / {
        proxy_pass http://backend;
    }
}

7. Frequently Asked Questions and Solutions

  1. 502 Bad Gateway Error

    • Cause: The backend service is not started or Nginx cannot connect to the backend service.

    • Solution: Check whether the backend service is running normally and ensure that Nginx'sproxy_passCorrect configuration.

  2. 403 Forbidden Error

    • Reason: Insufficient file permissions orrootIncorrect command configuration.

    • Solution: Check file permissions and ensurerootThe directive points to the correct directory.

  3. Nginx cannot start

    • Cause: Configuration file syntax error.

    • Solution: Use/usr/local/nginx/sbin/nginx -tCheck the configuration file syntax.

8. Summary

Nginx is a powerful web server and reverse proxy tool. Through reasonable configuration and maintenance, high-performance and highly available Web services can be achieved. This tutorial introduces Nginx installation, configuration, optimization and maintenance methods, hoping to help you better use Nginx.

The above is the detailed content of the entire tutorial on Nginx configuration and maintenance in Linux. For more information about Nginx Linux configuration and maintenance, please pay attention to my other related articles!