手工编译Nginx步骤详情查看之前博客
手工编译Nginx
(1)查看源码信息
[root@localhost opt]# vim /opt/nginx-1.12.2/src/core/nginx.h
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_
#define nginx_version 1012002
#define NGINX_VERSION "1.12.2"
#define NGINX_VER "nginx/" NGINX_VERSION
#ifdef NGX_BUILD
#define NGINX_VER_BUILD NGINX_VER " (" NGX_BUILD ")"
#else
#define NGINX_VER_BUILD NGINX_VER
#endif
#define NGINX_VAR "NGINX"
#define NGX_OLDPID_EXT ".oldbin"
#endif /* _NGINX_H_INCLUDED_ */
~
(1)修改源码信息
[root@localhost opt]# vim /opt/nginx-1.12.2/src/core/nginx.h
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_
#define nginx_version 1012002
#define NGINX_VERSION "1.1.1"
#define NGINX_VER "IIS/" NGINX_VERSION
#ifdef NGX_BUILD
#define NGINX_VER_BUILD NGINX_VER " (" NGX_BUILD ")"
#else
#define NGINX_VER_BUILD NGINX_VER
#endif
#define NGINX_VAR "NGINX"
#define NGX_OLDPID_EXT ".oldbin"
#endif /* _NGINX_H_INCLUDED_ */
**(3)验证修改后 版本信息
[root@localhost sbin]# iptables -F
[root@localhost sbin]# setenforce 0
[root@localhost sbin]# curl -I http://192.168.75.134
HTTP/1.1 200 OK
Server: IIS/1.1.1
Date: Mon, 10 Aug 2020 07:18:16 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:11:12 GMT
Connection: keep-alive
ETag: "5f30f310-264"
Accept-Ranges: bytes
[root@localhost sbin]#
[root@localhost sbin]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
[root@localhost 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)
cnginx: [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] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@localhost sbin]# sudo fuser -k 80/tcp //
80/tcp: 12552 12553
[root@localhost sbin]# nginx //重启Nginx
查看Nginx版本信息无验证成功
[root@localhost sbin]# nginx
[root@localhost sbin]# curl -I http://192.168.75.134
HTTP/1.1 200 OK
Server: nginx //无版本号
Date: Mon, 10 Aug 2020 07:22:33 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:11:12 GMT
Connection: keep-alive
ETag: "5f30f310-264"
Accept-Ranges: bytes
[root@localhost sbin]#
user nobody; 该为 user nginx nginx ;
[root@localhost sbin]# vim /usr/local/nginx.conf
[root@localhost sbin]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
location ~ \.(gif|jpg|jepg|png|bmp|ico)$ {
root html;
expires 1d;
}
[root@localhost html]# sudo fuser -k 80/tcp
80/tcp: 12663 12664
[root@localhost html]# nginx
[root@localhost html]# killall -s HUP nginx
[root@localhost html]# vim index.html
<img src="xunya.jpg"/>
[root@localhost html]# ll
总用量 52
-rw-r--r--. 1 root root 537 8月 10 15:11 50x.html
-rw-r--r--. 1 root root 636 8月 10 15:46 index.html
-rw-r--r--. 1 root root 42529 8月 10 15:42 xunya.jpg
[root@localhost html]# cd /opt
[root@localhost opt]# vim fenge.sh
#!/bin/bash
#Filename:fenge.sh
d=$#!/bin/bash
#Filename:fengge.sh
d=$(date -d "-1 day" "+%Y%m%d") #显示一天前的时间
logs_path="/var/log/nginx"
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d
kill -HUP $(cat $pid_path)
find $logs_path -mtime +30 | xargs rm -rf
[root@localhost opt]# chmod 777 fenge.sh
[root@localhost opt]# ./fenge.sh
[root@localhost opt]# cd /var/log/nginx/
[root@localhost nginx]# ll
总用量 4
-rw-r--r--. 1 root root 1905 8月 10 15:49 test.com-access.log-20200809
[root@localhost nginx]#
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
gzip on; #开启gzip压缩功能
gzip_min_length 1K; #压缩阀值
gzip_buffers 4 16K; # buffer大小为4个16K缓冲区大小
gzip_http_version 1.1; # 压缩版本
gzip_comp_level 6; #压缩比率,最小为1,处理速度快,传输速度慢,9最大压缩比,处理速度慢,传输速度快
gzip_types text/css text/javascript application/javascript image/jpeg image/png image/gif text/plain application/x-javascript image/jpg application/xml application/x-httpd-php application/json;
gzip_disable "MSIE [1-6]\."; #配置禁用gzip条件,支持正则,表示IE6以下不启用gzip
gzip_vary on; #选择支持vary header 可以让前端的缓存服务器缓存经过gzip压缩的页面
[root@localhost html]# systemctl restart nginx.service
[root@localhost html]# ll
总用量 84
-rw-r--r--. 1 root root 537 8月 10 15:11 50x.html
-rw-r--r--. 1 root root 29221 8月 6 15:26 error.jpg
-rw-r--r--. 1 root root 636 8月 10 15:46 index.html
-rw-r--r--. 1 root root 42529 8月 10 15:42 xunya.jpg
[root@localhost html]# vim index.html
<img src="xunya.jpg"/>
虚拟机win10 验证
清理浏览器的缓存
Nginx使用keepalive_timeout 来指定keepAlive 的超时时间(timeout)
指定每个TCP连接最多可以保持多长时间。Nginx 的默认值是65秒,有些浏览器最多只保持60秒,若将它设置为0。 就禁止了keepalive连接。
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf //在http中添加
keepalive_timeout 100;
client_header_timeout 80; # 等待客户端发送请求头的超时时间 超时会发送408错
误
client_body_timeout 80; #设置客户端发送请求体超时时间
[root@localhost html]# systemctl restart nginx.service
在高并发环境中,需要启动更多的Nginx进程以保证快速响应,用以处理用户的请求,避免造成阻塞,使用 ps aux 命令查看Nginx运行进程的个数
其中 master process 是Nginx的主进程,开启了1个,worker process 是子进程,
也是开启了1个。
修改Nginx的配置文件中的work_processes 参数,一般设为CPU的个数或者核数,在高并发的情况下可设置为CPU的个数或者核数的2倍,可以先查看CPU的核数以确定参数。
[root@localhost html]# cat /proc/cpuinfo | grep -c "physical"
8
[root@localhost html]# ps aux | grep nginx //cpu 核数
root 69719 0.0 0.0 20544 620 ? Ss 17:40 0:00 nginx: master process /usr/local/nginx/sbin/nginx //一个主进程中包含一个字进程
nobody 69720 0.0 0.0 23072 1392 ? S 17:40 0:00 nginx: worker process
root 70197 0.0 0.0 112724 988 pts/0 S+ 18:31 0:00 grep --color=auto nginx
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 2; #修改为核数相同或者2倍
work_cpu_affinity 01 10; #设置每个进程由不同CPU处理
[root@localhost html]# systemctl restart nginx.service