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 see
nginx: master process
andnginx: 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/logs
In the directory. It can be done through the configuration fileaccess_log
anderror_log
The 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
Adjust the number of work processes: Will
worker_processes
Set to the number of CPU cores.Optimize the number of connections: Enlarge
worker_connections
value to support more concurrent connections.Enable Gzip compression: exist
http
Add 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: useallow
anddeny
Directives restrict access to specific resources. For example:
location /admin { allow 192.168.1.0/24; deny all; }
Hide version information: existhttp
Add 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
-
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's
proxy_pass
Correct configuration.
-
403 Forbidden Error:
Reason: Insufficient file permissions or
root
Incorrect command configuration.Solution: Check file permissions and ensure
root
The directive points to the correct directory.
-
Nginx cannot start:
Cause: Configuration file syntax error.
Solution: Use
/usr/local/nginx/sbin/nginx -t
Check 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!