Nginx is the most widely used HTTP server. This article systematically introduces the most common operations of nginx in Linux, and provides a quick reference manual for related functions. The content mainly includes three parts: daily operations, common configuration, common errors, and related resources.
Overview of Nginx
NGINX is a free, open source, high-performance HTTP server and reverse proxy, as well as IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.
Prerequisites for this article
Prerequisites You have obtained the system administrator rights for all operations provided in this document. Different Linux distributions use different software dependency management tools and system service configuration tools. Therefore, you need to select corresponding commands based on the operating system.
The mapping of software dependency management tools for different distributions is as follows:
- Yum, for Centos7
The mapping of service management tools used by different distributions is as follows:
- Systemctl, for Centos7
Nginx daily operations
Install using the software source
- Add the EPEL software source. If the EPEL repository has been installed, skip this step.
yum
yum install epel-release
Copy the code
- Install the Nginx software
yum
yum install nginx
Copy the code
Start and Stop
- Setting startup
systemctl
systemctl enable nginx
Copy the code
- Start the nginx
systemctl
systemctl start nginx
Copy the code
- Stop nginx
systemctl
systemctl stop nginx
Copy the code
- Viewing the Current Status
systemctl
systemctl status nginx
Copy the code
- Restart the nginx
systemctl
systemctl restart nginx
Copy the code
- Reloading configuration
systemctl
systemctl reload nginx
Copy the code
View nginx run logs
The default log directory of nginx is /var/log/nginx/. The default operation directory of this section is the log directory. You can run the cat, grep, and tail commands to view logs based on the operation purpose. By default, you can view logs in real-time.
- Viewing access Logs
tail -f access.log
Copy the code
- Viewing error Logs
tail -f error.log
Copy the code
Common Nginx configurations
General operation
After configuration file changes are made, you need to perform a reload to make the configuration information take effect, and in a few cases you need to restart the Nginx service.
Configuration file structure
- Generally, nginx-related configuration files are in the /etc/nginx-/ directory.
- The main configuration file is nginx.conf in the above directory, where the default configuration information for Nginx is defined.
- By default, each individual nginx service configuration file should be placed in the conf.d/ subdirectory of the configuration directory.
- Subconfiguration files, such as dm2box.com.conf, are usually named by domain names for easy retrieval by o&M personnel.
Master configuration file
The main configuration file defines common software configuration information, including key information such as log path configuration and sub-configuration file configuration. /etc/nginx/nginx.conf
user nginx; worker_processes auto; # Error_log /var/log/nginx/error.log; pid /run/nginx.pid; # load dynamic module configuration include/usr/share/nginx/modules / *. Conf. events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; # access log path configuration access_log/var/log/nginx/access. Log the main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; Include /etc/nginx/conf.d/*.conf; }Copy the code
HTTPS service Reverse proxy Local JAVA application example
This example is a typical configuration example of the JAVA web site, including key configurations such as reverse proxy configuration, HTTP redirection to HTTPS services in the local domain, SSL certificate, and security configuration. /etc/nginx/conf.d/dm2box.com.conf
# Dm2Box service Java Application service port server 127.0.0.1:8080; } server {listen 80; server_name dm2box.com; Return 301 https://$server_name$request_uri; } server {listen 443 SSL; listen [::]:443; server_name dm2box.com; client_max_body_size 1024m; Ssl_certificate /dm2box/full_chain.pem; Ssl_certificate_key /dm2box/private.key; Ssl_session_timeout 5; # encryption algorithm ssl_ciphers ECDHE - RSA - either AES128 - GCM - SHA256: ECDHE: ECDH: AES: HIGH:! NULL:! aNULL:! MD5:! ADH:! RC4; Ssl_protocols TLSv1 TLSv1.1 TLSv1.2; Ssl_prefer_server_ciphers on; #Gzip related Gzip on; gzip_buffers 4 16k; gzip_comp_level 6; gzip_vary on; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; Location / {# proxy_pass http://dm2box; proxy_set_header HOST $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}Copy the code
Related Learning Resources
- The official website of the knowledge base, www.nginx.com/resources/w…
Copyright notice, this article first published in the digital magic box www.dm2box.com/ welcome to reprint.