Nginx的参数配置:
301 #永久重定向
302 #临时重定向
200 #访问正确
实验:
[root@server5 ~]# cd /usr/local/lnmp/nginx/conf
[root@server5 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# mkdir /www1
[root@server5 conf]# mkdir /www2
[root@server5 conf]# cd /www1
[root@server5 www1]# vim index.html
####################
写入: www.westos.org
[root@server5 www1]# cd ..
[root@server5 conf]# cd /www2
[root@server5 www2]# vim index.html
####################
写入: www.linux.org
[root@server5 www2]# nginx -s reload
**本地解析:**
[root@foundation52 Desktop]# vim /etc/hosts
测试:
https加密
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -t
nginx: [emerg] BIO_new_file("/usr/local/lnmp/nginx//conf/cert.pem") failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/usr/local/lnmp/nginx//conf/cert.pem','r') error:2006D080:BIO routines:BIO_new_file:no such file)
[root@server5 conf]# cd /etc/pki/tls/certs/
[root@server5 certs]# ls
ca-bundle.crt make-dummy-cert renew-dummy-cert
ca-bundle.trust.crt Makefile
[root@server5 certs]# make cert.pem
umask 77 ; \
PEM1=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
PEM2=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
/usr/bin/openssl req -utf8 -newkey rsa:2048 -keyout $PEM1 -nodes -x509 -days 365 -out $PEM2 -set_serial 0 ; \
cat $PEM1 > cert.pem ; \
echo "" >> cert.pem ; \
cat $PEM2 >> cert.pem ; \
rm -f $PEM1 $PEM2
Generating a 2048 bit RSA private key
....................................................................+++
......................................+++
writing new private key to '/tmp/openssl.L2Clt6'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:server5
Email Address []:root@localhost
[root@server5 certs]# ll cert.pem
-rw------- 1 root root 3076 Aug 7 16:42 cert.pem
[root@server5 certs]# cp cert.pem /usr/local/lnmp/nginx/conf/
[root@server5 conf]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx//conf/nginx.conf test is successful
[root@server5 conf]# nginx -s reload
测试:
输入:http://www.westos.org
重定向
[root@foundation52 Desktop]# curl -I www.taobao.com
HTTP/1.1 302 Found
Server: Tengine
Date: Tue, 07 Aug 2018 09:33:35 GMT
Content-Type: text/html
Content-Length: 258
Connection: keep-alive
Location: https://www.taobao.com/ #
Set-Cookie: thw=cn; Path=/; Domain=.taobao.com; Expires=Wed, 07-Aug-19 09:33:35 GMT;
Strict-Transport-Security: max-age=31536000
[root@foundation52 Desktop]# curl -I taobao.com
HTTP/1.1 302 Found
Server: Tengine
Date: Tue, 07 Aug 2018 09:33:43 GMT
Content-Type: text/html
Content-Length: 258
Connection: keep-alive
Location: http://www.taobao.com/ #
实验1:
[root@server5 conf]# vim nginx.conf
####################
126 server{
127 listen 80;
128 server_name www.westos.org westos.org;
129
130 location /{
131 root /www1;
132 index index.html;
133 }
134 }
[root@server5 conf]# nginx -s reload
测试:
[root@foundation52 Desktop]# curl -I westos.org
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 07 Aug 2018 01:41:55 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Mon, 06 Aug 2018 23:57:30 GMT
Connection: keep-alive
ETag: "5b68e06a-f"
Accept-Ranges: bytes
实验2.1:
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -s reload
测试:
[root@foundation52 Desktop]# curl -I www.westos.org
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:44:37 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org
[root@foundation52 Desktop]# curl -I westos.org
HTTP/1.1 301 Moved Permanently
Server: wts/1.2
Date: Tue, 07 Aug 2018 09:51:38 GMT
Content-Type: text/html; charset=iso-8859-1
Connection: keep-alive
Location: http://www.westos.org/
实验2.2:
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -s reload
测试:
[root@foundation52 Desktop]# curl -I www.westos.org/index.html
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:50:50 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org/index.html
[root@foundation52 Desktop]# curl -I www.westos.org/westos
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:52:03 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org/westos
实验3:
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -s reload
[root@server5 conf]# cd /www2
[root@server5 www2]# ls
index.html
[root@server5 www2]# cat index.html
bbs.westos.org
本地解析:
测试:
实验5
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -s reload
测试:
实验6:
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -s reload
测试:
实验7:
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -s reload
[root@server5 conf]# cd /www1
[root@server5 www1]# mkdir bbs
[root@server5 www1]# ls
bbs index.html
[root@server5 www1]# cd bbs/
[root@server5 bbs]# vim index.html
###################
www.westos.org - bbs
测试:
[root@foundation52 Desktop]# curl -I bbs.westos.org
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 07 Aug 2018 02:38:33 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://www.westos.org/bbs/
实验8:
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -s reload
测试;
[root@foundation52 Desktop]# curl -I bbs.westos.org/index.html
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Thu, 09 Aug 2018 04:26:21 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: http://www.westos.org/bbs/index.html
实验9:
[root@server5 ~]# cd /usr/local/lnmp/nginx/conf/
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx
[root@server5 conf]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx//conf/nginx.conf test is successful
[root@server5 conf]# nginx -s reload
[root@foundation52 Desktop]# scp vim.jpg 172.25.52.5:/usr/local/lnmp/nginx/html/download
root@172.25.52.5's password:
vim.jpg 100% 443KB 442.9KB/s 00:00
[root@server5 conf]# cd ..
[root@server5 nginx]# cd html/
[root@server5 html]# pwd
/usr/local/lnmp/nginx/html
[root@server5 html]# mkdir download
[root@server5 html]# cd download/
[root@server5 download]# ls
vim.jpg
[root@server5 download]# ll
total 444
-rw-r--r-- 1 root root 453575 Aug 8 10:13 vim.jpg
测试(压测)
先在浏览器中测试:输入172.25.52.5/download/vim.jpg
压测:
在真机中输入ab -c1 -n 10 http://172.25.52.5/download/vim.jpg
输入:curl http://172.25.52.5/download/vim.jpg -I
200表示正确
查看日志:
在真机中输入:
ab -c10 -n 10 http://172.25.52.5/download/vim.jpg
查看日志:
[root@server5 logs]# cat access.log
访问控制:
(1).黑名单
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -s reload
测试:
白名单:
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -s reload
测试:
访问控制也可以写成一个网段
##允许52整个网段都可以访问
例如:
测试:
缓存:减少网站流量 加速用户访问的速度
图片存储时间;
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -s reload
[root@server5 conf]# cd ..
[root@server5 nginx]# cd html/
[root@server5 html]# cd download/
[root@server5 download]# pwd
/usr/local/lnmp/nginx/html/download
[root@server5 download]# cp vim.jpg ..
[root@server5 download]# cd ..
[root@server5 html]# ls
50x.html bbs example.php index.php readme vim.jpg #
admin download index.html memcache.php utility
测试:
输入:curl -I 172.25.52.5/vim.jpg
域名访问
输入ip即可直接访问不合适(恶域名访问)
输入ip禁止访问并返回500
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -s reload
测试:
[root@server5 conf]# vim nginx.conf
[root@server5 conf]# nginx -s reload
测试:
输入172.25.52.5 重定向 到 www.westos.org
也可以写为 *.westos.org 缺点是访问速度慢 故一般不这样写
注意:解析不能这样写
解析:
[root@foundation52 Desktop]# vim /etc/hosts
测试:
输入:haha.westos.org 重定向 到 www.westos.org
字符:
##在发布文件中输入中文 会乱码
[root@server5 conf]# cd /www1/
[root@server5 www1]# ls
index.html
[root@server5 www1]# vim index.html
##############
www.westos.org 你好
测试:
解决方案:
测试:
日志:
不记录图片等无关紧要信息 以减少I/O 消耗
日志截断(备份)
[root@server5 conf]# cd ..
[root@server5 nginx]# cd logs/
[root@server5 logs]# ls
access.log error.log nginx.pid
[root@server5 logs]# ll
total 324
-rw-r--r-- 1 root root 292175 Aug 8 14:48 access.log
-rw-r--r-- 1 root root 22010 Aug 8 14:51 error.log
-rw-r--r-- 1 root root 5 Aug 8 10:10 nginx.pid
[root@server5 logs]# du -h access.log
292K access.log
##date命令 只截出时间
[root@server5 logs]# date
Wed Aug 8 14:57:02 CST 2018
[root@server5 logs]# date -d 1day
Thu Aug 9 14:57:16 CST 2018
[root@server5 logs]# date +%F -d -1day
2018-08-07
[root@server5 logs]# cd /opt/
[root@server5 opt]# ls
[root@server5 opt]# vim nginx_log.sh
[root@server5 opt]# /opt/nginx_log.sh
[root@server5 opt]# cd /usr/local/lnmp/nginx/logs/
##生成了access.log_2018-08-07日志
[root@server5 logs]# ll
防盗链
##模拟盗链
[root@server4 ~]# yum install httpd -y
[root@server4 ~]# cd /var/www/html
[root@server4 html]# vim index.html
####################
www.westos.org
[root@server4 html]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.52.44 for ServerName
[ OK ]
[root@foundation52 Desktop]# scp iso7.gif 172.25.52.5:/www1
root@172.25.52.5's password:
Permission denied, please try again.
root@172.25.52.5's password:
iso7.gif 100% 220KB 220.0KB/s 00:00
[root@server5 opt]# cd ..
[root@server5 /]# cd www1
[root@server5 www1]# ls
index.html iso7.gif
测试:
编写策略
[root@server4 ~]# vim /var/www/html
[root@server5 www1]# mkdir images
[root@server5 www1]# ls
images index.html iso7.gif
[root@server5 www1]# mv iso7.gif images/
[root@server5 www1]# pwd
/www1
测试:
输入172.25.52.4 Server4访了server5里的图片信息 即为盗链
解决方案:
[root@foundation52 Desktop]# scp daolian.jpg 172.25.52.4:/www2
root@172.25.52.4's password:
[4]+ Stopped scp daolian.jpg 172.25.52.4:/www2
[root@server5 images]# cd /www2
[root@server5 www2]# ls
daolian.jpg index.html
[root@server5 conf]# vim nginx.conf
解析:
[root@foundation52 Desktop]# vim /etc/hosts
测试:
[root@server5 conf]# vim nginx.conf
测试: