Detailed explanation of nginx installation, deployment and usage on Linux

Detailed explanation of nginx installation, deployment and usage on Linux

1. Download

Official website download address

2. Deployment

2.1 Installation Prerequisites

To install under Linux, you need to install the following components

1. gcc && g++
yum install gcc-c++
2. pcre
yum install -y pcre pcre-devel
3. zlib
yum install -y zlib zlib-devel
4. openssl
yum install -y openssl openssl-devel

2.2 Installation

1. Unzip the nginx file tar -zxvf nginx-1.17.5.tar.gz
2. Installation## Create an nginx installation directory mkdir nginx
cd nginx-1.12.2
## Specify the file installation path./configure --prefix=/home/lege/nginx
make
make install
#After the installation is complete, the content will be installed to the specified path /home/lege/nginx, otherwise it will be installed in the default directory /usr/local/nginx

2.3 Start nginx

## Modify the configuration file cd /home/lege/nginx/conf
vim nginx.conf
## Set the port to 8080, or set it to other listen 8080;
## Enter the startup directory cd /home/lege/nginx/sbin
## Check if there is any problem with the configuration file./nginx -t
##The result without any problem is as follows:
[soa@testsoa04 sbin]$ ./nginx -t
nginx: the configuration file /home/lege/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/lege/nginx/conf/nginx.conf test is successful
[soa@testsoa04 sbin]$ 
 
## Query configuration parameters./nginx -V
## For the installed nginx, you need to modify the configuration parameters./configure --prefix=/home/lege/nginx ...Configuration parameters make
make install 
Then restart nginx## Start ./nginx
## Stop ./nginx -s stop
## Restart ./nginx -s reload
## Enter the URL to verify whether it is started successfully http://ip:port/

3. Nginx log related configuration

The format of log definition:
Syntax format: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
           access_log off;
Default value: access_log logs/access.log combined;
Scope: http, server, location, if in location, limit_except
 
1. Define the log format syntax: log_format name [escape=default|json] string ...;
Default value: log_format combined "...";
Scope: http
 
Common log variables $remote_addr, $http_x_forwarded_for record the client IP address $remote_user record the client user name $request record the requested URL and HTTP protocol (GET, POST, DEL, etc.)
$status records the request status $body_bytes_sent the number of bytes sent to the client, excluding the size of the response header; This variable is compatible with the "%B" parameter in the Apache module mod_log_config.
$bytes_sent The total number of bytes sent to the client.
$connection The serial number of the connection.
$connection_requests The current number of requests received through a connection.
$msec Log writing time. The unit is seconds, and the precision is milliseconds.
$pipe If the request is sent via HTTP pipelining, the pipe value is "p", otherwise it is ".".
$http_referer records the page from which the link was accessed $http_user_agent records client browser related information $request_length The length of the request (including request line, request header and request body).
$request_time Request processing time, in seconds, with millisecond precision; starting from the first byte read from the client until the last character is sent to the client and written to the log.
$time_iso8601 Local time in ISO8601 standard format.
$time_local Local time in common log format.
 
Example:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" "$request_time" '
           '"$http_user_agent" "$http_x_forwarded_for"

For log-related cleanup, you can use Linux scheduled tasks to handle it. The example is as follows:

The clear.sh script is as follows:

#!/bin/bash
#LOGS_PATH is the log storage path LOGS_PATH=/home/lege/data/nginx/logs
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
KEEPTIME=$(date -d "-3 days" +%Y-%m-%d)
#Split log files mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access_${YESTERDAY}.log
mv ${LOGS_PATH}/error.log ${LOGS_PATH}/error_${YESTERDAY}.log
#Reread the log through Nginx semaphore control, /web/nginx/ is the nginx installation directory kill -USR1 $(cat /home/lege/data/nginx/logs/nginx.pid)
#Delete the log file from 3 days ago rm -f ${LOGS_PATH}/access_{KEEPTIME}.log
rm -f ${LOGS_PATH}/error_{KEEPTIME}.log
echo 0
 
crontab -e add the following:
0 0 * * * /bin/sh /home/lege/data/nginx/logs/clear.sh
 
crontab -l Check whether the addition is successful

4. Using nginx as a file downloader

http {
  server_tokens off;
  client_header_buffer_size 8k;
  client_max_body_size 130m;
  proxy_buffer_size 64k;
  proxy_buffers 8 64k;
  log_format access '$remote_addr $host $remote_user [$time_local] $status $request_length $body_bytes_sent $request_time 0 0 0 - "-" "$request" "$http_referer" "$http_user_agent" $http_cookie $bytes_sent';
  access_log logs/access.log access;
  keepalive_requests 16;
  keepalive_timeout 5;
  server {
    listen 8080;
    server_name localhost;
    charset utf-8;
    location / {
      default_type 'application/octet-stream';
      add_header Content-disposition "attachment";
      ## Configure the path of the file that can be downloaded. The following is the path used for testing under Windows. Linux can also be replaced with the corresponding path root D://tools//nginx-1.17.4//conf;
     }
   }
}

After the configuration is complete, restart nginx, then enter the browser and enter the file name under http://ip:8080/conf to download the corresponding file. ps: You can't download directories, you can only download files.

Summarize

The above is the installation, deployment and use process of nginx on Linux introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Detailed explanation of nginx compilation and installation tutorial and compilation parameters under Linux
  • How to install Nginx under Linux (pcre and openssl)
  • Detailed explanation of installing and configuring nginx under Linux
  • How to install and start nginx in Linux
  • Detailed steps to install and build Nginx server on Linux
  • How to install nginx under Linux
  • Install waf module for nginx under Linux
  • Linux server nginx uninstall and installation tutorial
  • A simple way to install two nginx under Linux system
  • How to quickly install Nginx in Linux

<<:  Kill a bunch of MySQL databases with just a shell script like this (recommended)

>>:  Vue.js framework implements shopping cart function

Recommend

MySQL 8.0.12 Simple Installation Tutorial

This article shares the installation tutorial of ...

Detailed explanation of jQuery chain calls

Table of contents Chain calls A small case Chain ...

A brief analysis of the game kimono memo problem

Today, after the game was restarted, I found that...

MySQL 5.6.33 installation and configuration tutorial under Linux

This tutorial shares the installation and configu...

Detailed tutorial on deploying Apollo custom environment with docker-compose

Table of contents What is the Apollo Configuratio...

What are HTML inline elements and block-level elements and their differences

I remember a question the interviewer asked durin...

Pure CSS to achieve automatic rotation effect of carousel banner

Without further ado, let’s get straight to the co...

Nginx improves access speed based on gzip compression

1. Why does nginx use gzip? 1. The role of compre...

Vue custom v-has instruction to implement button permission judgment

Application Scenario Taking the background manage...

Learn how to use the supervisor watchdog in 3 minutes

Software and hardware environment centos7.6.1810 ...

A comprehensive summary of frequently used statements in MySQL (must read)

The knowledge points summarized below are all fre...

JS array deduplication details

Table of contents 1 Test Cases 2 JS array dedupli...