防盗链测试:
被允许的网站域名引用指定类型的文件正常:
[root@localhost ~]# curl -x127.0.0.1:80 -I -e "http://www.zhuohua.store/1.jpg" zhuohua.store/1.jpg
HTTP/1.1 200 OK
Server: nginx/1.10.0
Date: Wed, 04 Jul 2018 22:22:27 GMT
Content-Type: image/jpeg
Content-Length: 2
Last-Modified: Wed, 04 Jul 2018 22:18:48 GMT
Connection: keep-alive
ETag: "5b3d47c8-2"
Expires: Fri, 03 Aug 2018 22:22:27 GMT
Cache-Control: max-age=2592000
Accept-Ranges: bytes
[root@localhost ~]# curl -x127.0.0.1:80 -I -e "http://www.baidu.com/1.jpg" zhuohua.store/1.jpg
HTTP/1.1 200 OK
Server: nginx/1.10.0
Date: Wed, 04 Jul 2018 22:20:32 GMT
Content-Type: image/jpeg
Content-Length: 2
Last-Modified: Wed, 04 Jul 2018 22:18:48 GMT
Connection: keep-alive
ETag: "5b3d47c8-2"
Expires: Fri, 03 Aug 2018 22:20:32 GMT
Cache-Control: max-age=2592000
Accept-Ranges: bytes
未被允许的网站域名引用指定类型的文件不正常:
[root@localhost ~]# curl -x127.0.0.1:80 -I -e "http://www.aaa.store/1.jpg" zhuohua.store/1.jpg
HTTP/1.1 403 Forbidden
Server: nginx/1.10.0
Date: Wed, 04 Jul 2018 22:24:06 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
由于没有对doc文件类型进行限制,所以doc文件没有防盗链功能:
[root@localhost ~]# curl -x127.0.0.1:80 -I -e "http://www.baidu.com/1.doc" zhuohua.store/1.doc
HTTP/1.1 200 OK
Server: nginx/1.10.0
Date: Wed, 04 Jul 2018 22:26:34 GMT
Content-Type: application/msword
Content-Length: 2
Last-Modified: Wed, 04 Jul 2018 22:18:54 GMT
Connection: keep-alive
ETag: "5b3d47ce-2"
Accept-Ranges: bytes
[root@localhost ~]# curl -x127.0.0.1:80 -I -e "http://www.aaa.store/1.doc" zhuohua.store/1.doc
HTTP/1.1 200 OK
Server: nginx/1.10.0
Date: Wed, 04 Jul 2018 22:26:20 GMT
Content-Type: application/msword
Content-Length: 2
Last-Modified: Wed, 04 Jul 2018 22:18:54 GMT
Connection: keep-alive
ETag: "5b3d47ce-2"
Accept-Ranges: bytes
######################
######################
HTTPS是一种加密的HTTP协议,使用HTTPS通信,即使数据包被截获,其他人也无法破译里面的内容。如果公司网站对外提供Web服务,需要购买被各大浏览器厂商认可的SSL证书。
以下实验里,自己生成一对自定义的SSL证书。
先检测Nginx是否支持SSL:
[root@localhost ~]# nginx -V
nginx version: nginx/1.10.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
防火墙配置:(打开TCP 443 端口)
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
iptables-save > /etc/sysconfig/iptables
[root@localhost ~]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.7 on Thu Jul 5 06:44:42 2018
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [3:324]
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Thu Jul 5 06:44:42 2018
备注:TCP 80端口不需要打开了。
生成一对自定义的SSL证书:
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# openssl genrsa -des3 -out tmp.key
Generating RSA private key, 1024 bit long modulus
......++++++
................++++++
e is 65537 (0x10001)
Enter pass phrase for tmp.key: #输入自定义的密码
Verifying - Enter pass phrase for tmp.key: #重复输入自定义的密码
把tmp.key转换成zhuohua.key:
[root@localhost conf]# openssl rsa -in tmp.key -out zhuohua.key
Enter pass phrase for tmp.key: #输入自定义的密码
writing RSA key
[root@localhost conf]# rm -rf tmp.key
生成CSR文件:
[root@localhost conf]# openssl req -new -key zhuohua.key -out zhuohua.csr
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]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:zhuohua
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:
[root@localhost conf]#
生成CRT证书文件:
[root@localhost conf]# openssl x509 -req -days 365 -in zhuohua.csr -signkey zhuohua.key -out zhuohua.crt
Signature ok
subject=/C=XX/L=Default City/O=Default Company Ltd/CN=zhuohua
Getting Private key
生成的SSL证书文件: