nginx installation
Download address: http://nginx.org/download/nginx-1.4.2.tar.gz
Installation preparation: nginx depends on the pcre library, pcre must be installed first
yum install pcre pcre-devel
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure --prefix=/usr/local/nginx executes to this step and reports the following error
It means that you have not installed the gcc compiler, nginx is written in C language, of course you need a C compiler, then install one
yum install gcc-c++
Executing the command again will report the following error
You need to install "zlib-devel". SSH executes the following command: yum install -y zlib-devel
No problem now, compile and install nginx
make && make install
If there is no obvious error in the process, it means success.
start up:
cd /ulsr/local/nginx, see the following 4 directories
./
....conf configuration file
... html web page file
...logs log files
...sbin main binary program
[root@localhost nginx]# ./sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
....
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
Cannot bind port 80, port 80 is already occupied
(Sometimes you install apache, nginx, etc., and in more cases, the operating system comes with apache and starts it as a service)
Solution: Just close the software or service that occupies port 80.
Nginx configuration section
The Nginx.conf file under the conf directory under the nginx directory is the nginx configuration file
// Global area
worker_processes 1; // There is 1 working child process, which can be modified by itself, but it is too big to be beneficial, because it needs to compete for CPU, and it is generally set to the number of CPUs * the number of cores
Event {
// Generally, it is the feature of configuring nginx connection
// For example, how many connections can a word allow at the same time
worker_connections 1024; // This means that a child process allows a maximum of 1024 connections
}
http { //This is the main section for configuring the http server
Server1 { // This is the virtual host segment
Location { //Locate, relocate the special path or file, such as the image directory is processed separately
} /// If .php is processed separately
}
Server2 {
}
}
Example 1: Domain-based virtual hosting
server {
listen 80; #Listening port
server_name a.com; #monitor domain name
location / {
root /var/www/a.com; #Root directory location
index index.html;
}
}
Example 2: Port-based virtual host configuration
server {
listen 8080;
server_name 192.168.1.204;
location / {
root /var/www/html8080;
index index.html;
}
}
log management
We observe the server section of nginx, and we can see similar information as follows
#access_log logs/host.access.log main;
This shows that the server, its access log file is logs/host.access.log ,
The format used is the "main" format.
In addition to the main format, you can customize other formats.
What is the main format?
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
The main format is that we define a log format, and give it a name for easy reference.
Take the above example, the main type of log, the recorded remote_addr... http_x_forwarded_for and other options.
1: The log format refers to which options are recorded
Default log format: main
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
Such as the default main log format, record these few items
Remote IP- remote user/user time request method (such as GET/POST) request body length referer source information
http-user-agent user agent/spider, the original IP of the forwarded request
http_x_forwarded_for: When passing through the proxy, the proxy adds your original IP to this header information and transmits your original IP
2: Declare a unique log_format and name it
log_format mylog '$remote_addr- "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
In the following server/location, we can refer to mylog
In the server section, declare it like this
Nginx allows different logs for different servers, (some web servers do not support, such as lighttp)
access_log logs/access_8080.log mylog;
Declare log log position log format;
Practical application : shell+timed task+nginx signal management, complete log storage by date
Analysis ideas :
At 00:00:01 in the morning, rename yesterday's log and put it in the corresponding directory
Then the USR1 information number controls nginx to regenerate a new log file
Specific script :
#!/bin/bash
base_path='/usr/local/nginx/logs'
log_path=$(date -d yesterday +"%Y%m")
day=$(date -d yesterday +"%d")
mkdir -p $base_path/$log_path
mv $base_path/access.log $base_path/$log_path/access_$day.log
#echo $base_path/$log_path/access_$day.log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
timed task
Crontab edit timed tasks
01 00 * * * /xxx/path/b.sh 0:01 every day (recommended between 02-04, the system load is small)