配置nginx服务
编译环境
[root@localhost opt]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz
#创建用户nginx
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@promote conf]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@promote conf]# chmod +x /etc/init.d/nginx
[root@promote conf]# service nginx stop
[root@promote conf]# service nginx start
查看版本号
[root@localhost sbin]# curl -I http://192.168.136.88
HTTP/1.1 200 OK
Server: nginx/1.12.2 版本号
Date: Mon, 10 Aug 2020 17:06:32 GMT
Content-Type: text/html
Content-Length: 636
Last-Modified: Mon, 10 Aug 2020 16:05:29 GMT
Connection: keep-alive
ETag: "5f317049-27c"
Accept-Ranges: bytes
修改方法1
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; //添加
....
}
查看版本号
[root@localhost sbin]# curl -I http://192.168.136.88
HTTP/1.1 200 OK
Server: nginx 版本号
Date: Mon, 10 Aug 2020 17:06:32 GMT
Content-Type: text/html
Content-Length: 636
Last-Modified: Mon, 10 Aug 2020 16:05:29 GMT
Connection: keep-alive
ETag: "5f317049-27c"
Accept-Ranges: bytes
方法2
在编译前修改
[root@localhost opt]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz
#创建用户nginxv
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
开启编译
[root@promote nginx-1.12.2]# vim /opt/nginx-1.12.2/src/core/nginx.h
#define nginx_version 1012002
#define NGINX_VERSION "2.2.2" //修改
#define NGINX_VER "nginx/" NGINX_VERSION
编译安装
```
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
[root@localhost sbin]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# nginx
查看一下
[root@localhost sbin]# curl -I http://192.168.136.88
HTTP/1.1 200 OK
Server: nginx/2.2.2 版本号
Date: Mon, 10 Aug 2020 17:06:32 GMT
Content-Type: text/html
Content-Length: 636
Last-Modified: Mon, 10 Aug 2020 16:05:29 GMT
Connection: keep-alive
ETag: "5f317049-27c"
Accept-Ranges: bytes
编译环境
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
编译安装
[root@promote ~]# cd /opt/
[root@promote opt]#tar zxvf nginx-1.12.2.tar.gz
[root@promote opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
路径优化
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin
gzip on; #开启gzip压缩功能
gzip_ min_ length 1k; #压缩阈值
gzip_ buffers 4 16k; #buffer大小为4个1 6k缓冲区大小
gzip_ http_ version 1.1; #压缩版本
gzip_ comp_ level 6; #压缩比率,最小为1,处理速度快,传输速度慢,9最大压缩比,处理速度慢,传输适中选6
gzip_types text/plain application/x-javascript text/css image/ jpg image/jpeg image/png image/gif application/ xml text/javascript application/x-httpd-php application/javascript application/json;支持的压缩类型
gzip_ disable “MSIE [1-6].”; #配置禁用gzip条件,支持正则,表示ie6以下不启用gzip
gzip_ vary on; #选择支持very header可以让前端的缓存服务器缓存经过gzip压缩的页面
vim /usr/local/nginx/conf/nginx.conf
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css image/ jpg image/jpeg image/png image/gif application/ xml text/javascript application/x-httpd-php application/javascript application/json
;
gzip_disable "MSIE[1-6]\.";
gzip_vary on;
添加图片
[root@promote nginx-1.12.2]# cd /usr/local/nginx/html/
[root@promote html]# ll
总用量 44
-rw-r--r--. 1 root root 537 8月 10 18:35 50x.html
-rw-r--r--. 1 root root 33086 8月 10 18:58 game.jpg //添加图片
vim /usr/local/nginx/html/index.html
Welcome to nginx!
//添加图片信息
开启服务
[root@localhost nginx-1.12.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@promote html]# iptables -F
[root@promote html]# setenforce 0
[root@localhost nginx-1.12.2]# nginx
软件转包看到图片已经压缩成gzip格式
编译环境
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
编译安装
[root@promote ~]# cd /opt/
[root@promote opt]#tar zxvf nginx-1.12.2.tar.gz
[root@promote opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
路径优化
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin
添加图片
[root@promote nginx-1.12.2]# cd /usr/local/nginx/html/
[root@promote html]# ll
总用量 44
-rw-r--r--. 1 root root 537 8月 10 18:35 50x.html
-rw-r--r--. 1 root root 33086 8月 10 18:58 game.jpg //添加图片
vim /usr/local/nginx/html/index.html
Welcome to nginx!
//添加图片信息
[root@promote html]# vim /usr/local/nginx/conf/nginx.conf
location ~\.(gif|jpg|jepg|png|ico)$ {
root html;
expires 1d;
}
[root@promote html]# cd /usr/local/nginx/conf/
[root@promote conf]# nginx
[root@promote html]# setenforce 0
[root@localhost nginx-1.12.2]# nginx
用软件查看一下是否修改
通常需要用到命令(cat /proc/meminfo)查看内核数据结构
[root@promote conf]# cat /proc/cpuinfo | grep -c "physical" 查了开了几个进程
2
[root@promote sbin]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 4; 将默认的1修改为4
[root@promote sbin]# ps aux | grep nginx
root 18085 0.0 0.0 20544 612 ? Ss 20:04 0:00 nginx: master process nginx
nginx 18086 0.0 0.0 23072 1380 ? S 20:04 0:00 nginx: worker process
nginx 18087 0.0 0.0 23072 1380 ? S 20:04 0:00 nginx: worker process
nginx 18088 0.0 0.0 23072 1380 ? S 20:04 0:00 nginx: worker process
nginx 18089 0.0 0.0 23072 1380 ? S 20:04 0:00 nginx: worker process
root 18121 0.0 0.0 112824 980 pts/1 R+ 20:07 0:00 grep --c
Nginx使用keepalive_ timeout来指定KeepAlive的超时时间(timeout) 。
指定每个TCP连接最多可以保持多长时间。Nginx的默认值是65秒,有些浏览器最多只保持60秒,
若将它设置为0,就禁止了keepalive连接。
[root@promote sbin]# vim /usr/local/nginx/conf/nginx.conf
#keepalive_timeout 0; 在下面配置
keepalive_timeout 100;
client_header_timeout 80; //等待客户发送请求头的超时时间 超时会发送408错误
client_body_timeout 80; //设置客户端发送请求体超时时间
编译环境
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
编译安装
[root@promote ~]# cd /opt/
[root@promote opt]#tar zxvf nginx-1.12.2.tar.gz
[root@promote opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
路径优化
[root@localhost nginx-1.12.2]# ln /usr/local/nginx/sbin/nginx /usr/local/bin/
添加图片
[root@promote nginx-1.12.2]# cd /usr/local/nginx/html/
[root@promote html]# ll
总用量 44
-rw-r--r--. 1 root root 537 8月 10 18:35 50x.html
-rw-r--r--. 1 root root 33086 8月 10 18:58 game.jpg //添加图片
vim /usr/local/nginx/html/index.html
Welcome to nginx!
//添加图片信息
配置dns
[root@localhost html]# vim /etc/named.conf
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; };
[root@localhost html]# vim /etc/named.rfc1912.zones
zone "kgc.com" IN {
type master;
file "kgc.com.zone";
allow-update { none; };
[root@localhost named]# cp -p named.loopback kgc.com.zone
[root@localhost named]# vim kgc.com.zone
www IN A 192.168.136.88
开启服务
[root@localhost nginx-1.12.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@promote html]# iptables -F
[root@promote html]# setenforce 0
[root@localhost nginx-1.12.2]# nginx
[root@localhost named]# vim /etc/httpd/conf/httpd.conf
ServerName www.text.com:80
Listen 192.168.136.10:80
#Listen 80
设置盗链网站
[root@localhost named]# vim /var/www/html/index.html
this is dao tu web
[root@localhost named]# systemctl restart httpd
[root@localhost named]# systemctl stop firewalld
[root@localhost named]# iptables -F
[root@localhost named]# setenforce 0
在源地址设置放盗链
vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
}
location ~*\.(jpg|gif|swf)$ { ///添加
valid_referers none blocked *.kgc.com kgc.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.kgc.com/error.png;
}
}
[root@promote conf]# cd /usr/local/nginx/html/
[root@promote html]# ll
总用量 80
-rw-r--r--. 1 root root 537 8月 13 09:18 50x.html
-rw-r--r--. 1 root root 31566 8月 10 22:46 error.jpg
[root@localhost sbin]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sbin]# nginx
[root@localhost opt]# vim fenge.sh
#!/bin/bash
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) #删除30天前的日志
find $logs_path -mtime +30 | xargs rm -rf
开启权限
[root@localhost opt]# chmod +x fenge.sh
查看
[root@localhost nginx]# date -s 08/10/20 修改日期
2020年 08月 10日 星期一 00:00:00 CST
[root@localhost nginx]# cd /var/log/nginx/
[root@localhost nginx]# ls
test.com-access.log-20200810
vim php-fpm.conf
pid=run/php-fpm.pid
pm=dynamic
pm.max_children=20 //static模式下空闲进程数上限,大于下面的值
pm.start_servers=5 //动态方式下默认开启的进程数,在最小和最大之间
pm.min_spare_servers=2 //动态方式下最少的空闲进程数
pm.max_spare_servers=2 //动态方式下最大的空闲进程数