./configure --prefix=/usr/local/nginx1.21.1 --user=nginx --group=nginx \
--with-http_ssl_module --with-http_dav_module --with-http_stub_status_module \
--with-http_addition_module --with-http_sub_module --with-http_flv_module \
--with-http_mp4_module --with-http_realip_module --with-http_gzip_static_module \
--with-pcre --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log --with-debug --with-http_image_filter_module
vim /etc/nginx/nginx.conf
worker_processes auto;
查看 nginx 管理进程和 4 个 work 进程的父子关系
pstree -p |grep nginx
select,poll,epoll 都是 nginx 下的 IO 多路复用的机制。I/O 多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作。
Epoll 在 Linux2.6 内核中正式引入,和 select 和 poll 相似,其实都是 I/O 多路复用技术。
Epoll 没有最大并发连接的限制,上限是最大可以打开文件的数目,这个数字一般远大于 2048, 一般来说这个数目和系统内存关系很大,具体数目可以 cat /proc/sys/fs/file-max 察看。
cat /proc/sys/fs/file-max
808855
效率提升,Epoll 最大的优点就在于它只管你“活跃”的连接,而跟连接总数无关,因此在实际的网络环境中,Epoll 的效率就会远远高于 select 和 poll。
Epoll 在这点上使用了“共享内存”,更省内存,效率更高。
http://nginx.org/en/docs/http/ngx_http_fastcgi_module.htmlfastcgi_cache
配置详解:
HTTP 字段:
fastcgi_connect_timeout 300; 指定链接到后端 FastCGI 的超时时间。
fastcgi_send_timeout 300; Nginx 允许 FastCGI 服务器返回数据的超时时间,即在规定时间内后端服务器必须传完所有的数据,否则 Nginx 将断开这个连接。
fastcgi_read_timeout 300; Nginx 从 FastCGI 服务器读取响应信息的超时时间,表示连接建立成功后,Nginx 等待后端服务器的响应时间。
fastcgi_buffers 4 64k; 指定缓冲区的数量和大小,如果一个 PHP 脚本返回的页面大小为 256K,则分配为 4 个 64K 的缓冲区来缓存,大于缓冲区大小的应答结果将被写入 temp_file 临时文件中。推荐该值为你的站点 php 脚本返回的页面大小的平均进行设置,尽量不需要使缓存文件写入磁
盘,也不要设置太大导致内存资源浪费。
fastcgi_temp_path /var/run/nginx-tmp-cache; 指定临时目录
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=ngx_fcgi_cache:512m
inactive=1d max_size=40g; 定义缓存的路径、大小、缓存时间。
上条命令详解:(参数过长拆分详解)
fastcgi_cache_path 创建缓存路径,如果指定其他路径需注意上层目录是否创建。
levels:指定了该缓存空间有两层 hash 目录,设置缓存目录层数,levels=1:2,表示创建两层目录缓存,最多创建三层。第一层目录名取 fastcgi_cache_key md5 的最后一个字符,第二层目录名取倒数2-3 字符,如:fastcgi_cache_key md5 为 b7f54b2df7773722d382f4809d65029c,则:
注:levels 虽然可以设置 3 层但是数值请勿大于 2.如果设置 1:2:3 则报错。1:2:2 则正常。
levels=2:2 为/var/run/nginx-cache/9c/02/b7f54b2df7773722d382f4809d65029c
levels=1:2 为/var/run/nginx-cache/c/29/b7f54b2df7773722d382f4809d65029c
keys_zone 为这个缓存区起名为 zone_name
512m 指缓存空间最一开始为 512MB;
inactive=1d 代表如果缓存文件一天内没有被访问,则删除;
max_size=40g 代表缓存最大为 40G,建议根据自己的磁盘剩余空间来写大小,不能超过剩余缓存的 80%;
Server 字段中配置详解:
include fastcgi.conf; 引用 fastcgi 默认配置文件
fastcgi_cache ngx_fcgi_cache; 指定缓存名称,由 http 字段中 fastcgi_cache_path
创建。
fastcgi_cache_valid 200 302 1h; 设置 200、302 状态的 URL 缓存 1 小时
fastcgi_cache_valid 301 1d; 设置 301 状态的 URL 缓存 1 天。
fastcgi_cache_valid any 1m; 将其他应答缓存为 1 分钟。
fastcgi_cache_min_uses 1; 设置请求 1 次就会被缓存。
fastcgi_cache_use_stale error timeout invalid_header http_500; 设置 500 状态码
不进行缓存。500 状态码意思是:内部服务器错误
fastcgi_cache_key http://$host$request_uri; 该指令用来设置 Web 缓存的 Key 值,Nginx
根据 Key 值 MD5 缓存。一般根据 host(域名),request_uri(请求的路径)等变量组合成
fastcgi_cache_key。MD5 值将作为缓存文件名。
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 365d;
}
location ~ .*\.(js|css)?$ {
expires 30d;
}
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
expires 360d;
}
location ~(robots.txt) {
expires 7d;
break;
}
}
日志优化的目的,是为了一天日志一压缩,按天存放,超过 10 天的删除。
vim /usr/local/nginx1.21.1/logs/cut_nginx_log.sh 每天日志分割脚本。
!/bin/bash
date=$(date +%F -d -1day)
cd /var/log/nginx/
if [ ! -d cut ] ; then mkdir cut
fi
mv access.log cut/access_$(date +%F -d -1day).log
mv error.log cut/error_$(date +%F -d -1day).log
/usr/local/nginx1.21.1/sbin/nginx -s reload
tar -JcPf cut/$date.tar.xz cut/*
mv cut/$date.tar.xz /var/log/nginx/
rm -f cut/access* && rm -f cut/error*
if [[ -z `egrep -v "00 00 * * * /bin/bash /usr/local/nginx1.21.1/logs/cut_nginx_log.sh &> /dev/null" /var/spool/cron/root` ]]; then
echo "00 00 * * * /bin/bash /usr/local/nginx1.21.1/logs/cut_nginx_log.sh &> /dev/null" >> /var/spool/cron/root
fi
find /var/log/nginx/cut/ -type f -mtime +10 | xargs rm -rf
chmod +x cut_nginx_log.sh
./cut_nginx_log.sh
if ( -f $request_filename) {
root /usr/local/nginx1.21.1/html;
rewrite ^/(.*)$ http://www.gf-beyond.com last;
break;
}
deny all;
}
......
}
location ~ ^/(upload)/ {
deny all;
}
location ~ ^/(test)/ {
deny all;
}
listen 80;
server_name www.gf-beyond.com gf-beyond.com;
if ( $host = '192.168.2.40' ) {
rewrite ^/(.*)$ http://www.gf-beyond.com/$1 permanent;
}
server {
listen 80;
server_name gf-beyond.com;
rewrite ^ http://www.baidu.com$request_uri?;
........
}
网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。
加入交流群
请使用微信扫一扫!