Nignx configuration file content
#running user
user www-data;
#Start the process, usually set to twice the number of cpu
worker_processes 4;
#Global error log and PID file
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
#Working mode and connection limit
events {
use epoll; #epoll is a way of multiplexing IO (I/O Multiplexing), but it is only used for kernels above linux2.6, which can greatly improve the performance of nginx
worker_connections 1024;#The maximum number of concurrent connections for a single background worker process
}
#Set up the http server, use its reverse proxy function to provide load balancing support
http {
#Set the mime type, which is defined by the mime.type file
include mime.types;
default_type application/octet-stream;
#Set log format
access_log /usr/local/nginx/logs/access.log;
The #sendfile directive specifies whether nginx calls the sendfile function (zero copy mode) to output files. For common applications,
#Must be set to on, if it is used for downloading and other applications with heavy disk IO load, it can be set to off to balance the disk and network I/O processing speed and reduce the system uptime.
sendfile on;
#tcp_nopush on;
#Connection timeout
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
#fastcgi
fasccgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#Open gzip compression
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text/plain application/x-Javascript text/css application/xml;
gzip_vary on;
gzip_disable “MSIE [1-6]\.(?!.*SV1)”;
#Set request buffer
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
include /usr/local/nginx/conf/*.conf;
include /etc/nginx/sites-enabled/*;
#Set load balancing server list
upstream mysvr {
The #weigth parameter represents the weight value, the higher the weight value, the greater the probability of being assigned
#Squid on this machine opens port 3128
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
#Server definition
server {
#Listen to port 80
listen 80;
#Definition Use www.server110.com to access
server_name www.server110.com;
#Set the access log of this virtual host
access_log logs/www.server110.com.access.log main;
#default request
location / {
root /root; #Define the server’s default website root directory location
index index.php index.html index.htm; #Define the home page index file name
Fastcgi_pass www.server110.com;
fStation Directory
root /var/web1
}
#Second virtual machine
server{
listen 80; #80 port
server_name bbb.domain.com; #host name
access_log logs/server2.access.log combined ; #Access log
location / {
#Default homepage file
index index.html index.htm index.php
#website directory
root /var/web2
}
}
}
Log file configuration and cutting
The log format of Nignx is mainly related to two commands, the first is log_format to define the format of the log, and the other is access_log to define the storage path of the log.
The syntax of log_format is as follows:
log_format name format [format …] where name represents the name of the log format, and Nginx has a format name of combined by default. The specific parameters are as follows:
Log_format combined ‘$remote_addr – $remote_user [$time_local]’
‘”$request” $status $body_bytes_sent’
‘”$http_referer” “$http_user_agent”‘ ;
But now we assume that a web server, reverse proxy server, load balancing, etc. are made on Nignx. If the web server configuration is placed behind the reverse proxy server configuration, we cannot get the user’s IP through $remote_addr, because we can only get The IP of the reverse proxy server. So we need to use the $http_x_forwarded_for variable to record this user IP.
Significance of the log format: $remote_addr and $http_x_forwarded_for represent the user IP; $remote_user represents the name of the remote client; $time_local represents the access time and time zone; $request represents the HTTP protocol of the request URL; $status represents the request status such as 200; $body_bytes_sent represents The size of the content sent to the client; $http_referer represents which page was visited; $http_user_agent records the client browser information.
Analysis of access_log command
The syntax is: access_log path [format [buffer = size | off]]
Among them, path indicates the log storage path, format indicates the format name defined by log_format, and buffer indicates the size of the memory buffer, for example, buffer = 32k. If you don’t want to log, you can use access_log off ;
Slicing of Nignx log files
In a server in a production environment, the efficiency of the server will be greatly affected because the log file is too large. So we need to split the log files by day. Mainly through the crontab script to automatically execute the following code to ensure that the following code file is executed at 00:00 every day.
Suppose the file name is: cut_nignx_log.sh
#!/bin/bash
#Define the log directory
logs_path = “/usr/local/nignx/logs/”
#Create log directory by time
mkdir -p ${logs_path}$(date -d “yesterday”
“%Y”)/$(date -d “yesterday” “%m”)/
#Rename the access.log of the previous day to the date name
mv ${logs_path}/access.log ${logs_path}$(date -d “yesterday”
“%Y”)/$(date -d “yesterday”
“%m”)/access_$(date -d “yesterday”
“%Y%m%d”).log
#Restart the server and generate a new day’s access.log file
kill -USR1 `cat /usr/local/nignx/logs/nignx.pid`
Define crontab file:
Shell>crontab -e
00 00 * * * /bin/bash /usr/local/nignx/logs/cut_nignx_log.sh
Nignx’s compressed output configuration
Gzip can compress the page, and the size of the compressed page can be reduced to 30% or even smaller, which speeds up the user’s access speed. The configuration of Gzip is located in http{…}
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-Javascript text/css application/xml;
gzip_vary on;
Nignx automatic column directory configuration
location / {
autoindex on; #Open directory listing
autoindex_exact_size [on | off] #Set the unit of file size when indexing
autoindex_localtime [on | off] #Enable local time
}
Nginx browser local cache settings
The browser cache is to speed up user browsing and store requested documents on the user’s disk. Browser caching can be implemented by specifying the output Header header through expires. The syntax of the expires directive is as follows:
Syntax: expire [time | epoch | max | off]
Default: expire off
Scope: http , server , location
Purpose: It can control the header information of Expires and Cache-Control in the HTTP response
For example: for pictures in common formats, flash files are cached locally in the browser for 30 days, and js and css files are cached locally in the browser for 1 hour.
location ~ .*\.(gif|jpeg|jpg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 1h;
}
bsp;1h;
}