默认状况下ssl模块并未被安装,若是要使用该模块则须要在编译nginx时指定–with-http_ssl_module参数.javascript
需求:
作一个网站域名为 www.localhost.cn 要求经过https://www.localhost.cn进行访问.php
10.10.100.8 www.localhost.cn
实验步骤:css
1.首先确保机器上安装了openssl和openssl-develhtml
1
2
|
#yum install openssl
#yum install openssl-devel
|
2.建立服务器私钥,命令会让你输入一个口令:java
1
2
|
openssl genrsa -des3 -
out
server.key 1024<br>
#由于之后要给nginx使用。每次reload nginx配置时候都要你验证这个PAM密码的.因为生成时候必须输入密码,你能够输入后 再删掉。
|
3.建立签名请求的证书(CSR):jquery
1
|
openssl req -
new
-key server.key -
out
server.csr
|
4.在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:nginx
1
2
|
cp server.key server.key.org
openssl rsa -
in
server.key.org -
out
server.key
|
5.配置nginx
最后标记证书使用上述私钥和CSR:web
1
|
openssl x509 -req -days 365 -
in
server.csr -signkey server.key -
out
server.crt
|
6.修改Nginx配置文件,让其包含新标记的证书和私钥:vim
1
2
3
4
5
|
#vim /usr/local/nginx/conf/nginx.conf
http {
include server/*.cn;
}
|
7.修改Nginx配置文件,让其包含新标记的证书和私钥:浏览器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#vim /usr/local/nginx/server/www.localhost.cn
server {
listen 443;
server_name www.localhost.cn;
ssl
on
;
ssl_certificate /etc/pki/tls/certs/server.crt;
ssl_certificate_key /etc/pki/tls/certs/server.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers
on
;
location / {
root html;
index index.html index.htm;
}
}
|
8.启动nginx服务器.
1
|
#/usr/local/nginx/sbin/nginx -s reload //若是环境容许的话直接杀掉进程在启动nginx
|
若是出现“[emerg] 10464#0: unknown directive "ssl" in /usr/local/nginx-0.6.32/conf/nginx.conf:74”则说明没有将ssl模块编译进nginx,在configure的时候加上“--with-http_ssl_module”便可
1
|
如:[root@localhost nginx-1.4.4]# ./configure --prefix=/usr/local/nginx --user=www --
group
=www --with-http_stub_status_module --with-http_ssl_module
|
9.测试网站是否可以经过https访问
另外还能够加入以下代码实现80端口重定向到443
1
2
3
4
5
6
|
server {
listen 80;
server_name www.localhost.cn;
#rewrite ^(.*) https://$server_name$1 permanent;
rewrite ^(.*)$ https:
}
|
过如下配置,能够设置一个虚拟主机同时支持HTTP和HTTPS
1
2
|
listen 80;
listen 443
default
ssl;
|
.
参考文档:http://dreamfire.blog.51cto.com/418026/1141302/
SSL英文名为Secure Socket Layer,安全套接字层。SSL是一种数字证书,它使用ssl协议在浏览器和web server之间创建一条安全通道,数据信息在client与server之间的安全传输
在这以前,记得nginx编译安装时加参数–with-http_ssl_module,使得nginx支持ssl模块。
1、颁发证书
下面自行颁发不受浏览器信任的证书
cd /usr/local/nginx/conf/key
一、建立服务器私钥,并输入口令
openssl genrsa -des3 -out server.key 1024
二、建立签名请求的csr证书
openssl req -new -key server.key -out server.csr
三、加载ssl支持的Nginx并使用私钥是除去必须的口令
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
2、配置Nginx
标记证书使用上述私钥和csr
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
修改nginx配置档,使用ssl
<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">server {
listen <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">443</span>;
server_name <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">192.168</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.0</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.216</span>;
autoindex <span class="hljs-function_start" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">on</span></span>;
root /usr/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">local</span>/nginx;
ssl <span class="hljs-function_start" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">on</span></span>;
ssl_certificate /usr/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">local</span>/nginx/conf/key/server.crt;
ssl_certificate_key
/usr/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">local</span>/nginx/conf/key/server.key;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>
配置完成后启动nginx,https访问
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx
访问https://192.168.0.216
能够看到访问成功,只是ssl证书不是受信的而已
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Nginx开启Gzip压缩大幅提升页面加载速度及相关测试
文章做者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合做。
刚刚给博客加了一个500px相册插件,lightbox引入了不少js文件和css文件,页面一会儿看起来很是臃肿,因此仍是把Gzip打开了。
环境:Debian 6
一、Vim打开Nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
二、找到以下一段,进行修改
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
三、解释一下
第1行:开启Gzip
第2行:不压缩临界值,大于1K的才压缩,通常不用改
第3行:buffer,就是,嗯,算了不解释了,不用改
第4行:用了反向代理的话,末端通讯是HTTP/1.0,有需求的应该也不用看我这科普文了;有这句的话注释了就好了,默认是HTTP/1.1
第5行:压缩级别,1-10,数字越大压缩的越好,时间也越长,看心情随便改吧
第6行:进行压缩的文件类型,缺啥补啥就好了,JavaScript有两种写法,最好都写上吧,总有人抱怨js文件没有压缩,其实多写一种格式就好了
第7行:跟Squid等缓存服务有关,on的话会在Header里增长"Vary: Accept-Encoding",我不须要这玩意,本身对照状况看着办吧
第8行:IE6对Gzip不怎么友好,不给它Gzip了
四、:wq保存退出,从新加载Nginx
/usr/local/nginx/sbin/nginx -s reload
五、用curl测试Gzip是否成功开启
curl -I -H "Accept-Encoding: gzip, deflate" "//www.slyar.com/blog/"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:13:09 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.2.17p1
X-Pingback: //www.slyar.com/blog/xmlrpc.php
Content-Encoding: gzip
页面成功压缩
curl -I -H "Accept-Encoding: gzip, deflate" "//www.slyar.com/blog/wp-content/plugins/photonic/include/css/photonic.css"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:21:25 GMT
Content-Type: text/css
Last-Modified: Sun, 26 Aug 2012 15:17:07 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:21:25 GMT
Cache-Control: max-age=43200
Content-Encoding: gzip
css文件成功压缩
curl -I -H "Accept-Encoding: gzip, deflate" "//www.slyar.com/blog/wp-includes/js/jquery/jquery.js"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:21:38 GMT
Content-Type: application/x-javascript
Last-Modified: Thu, 12 Jul 2012 17:42:45 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:21:38 GMT
Cache-Control: max-age=43200
Content-Encoding: gzip
js文件成功压缩
curl -I -H "Accept-Encoding: gzip, deflate" "//www.slyar.com/blog/wp-content/uploads/2012/08/2012-08-23_203542.png"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:22:45 GMT
Content-Type: image/png
Last-Modified: Thu, 23 Aug 2012 13:50:53 GMT
Connection: keep-alive
Expires: Tue, 25 Sep 2012 18:22:45 GMT
Cache-Control: max-age=2592000
Content-Encoding: gzip
图片成功压缩
curl -I -H "Accept-Encoding: gzip, deflate" "//www.slyar.com/blog/wp-content/plugins/wp-multicollinks/wp-multicollinks.css"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:23:27 GMT
Content-Type: text/css
Content-Length: 180
Last-Modified: Sat, 02 May 2009 08:46:15 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:23:27 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
最后来个不到1K的文件,因为个人阈值是1K,因此没压缩