由于本站全面开启https协议访问,小编决定将实践过程记录下来,方便后来人。
什么是https?
https 全称:Hyper Text Transfer Protocol over Secure Socket Layer,是http的安全版。即http下加入SSL协议层,因此https的安全基础就是SSL,所以加密内容需要SSL。
配置过程
首先需要申请一个证书,可以申请一个免费得。然后会得到nginx版本证书,一个公钥,一个私钥。将其上传到服务器目录。本次将讲解两种配置方式。
先确认nginx安装时已编译http_ssl
模块,也就是执行nginx -V
命令查看是否存在--with-http_ssl_module
。如果没有,则需要重新编译nginx将该模块加入。
http与https共存方式
意思是指网站可以通过http请求访问,也可以通过https请求访问。注:http
端口为80
,https
端口为443。
- server {
- listen 80;
- listen 443 ssl;
- ssl_certificate ssl/itunic.crt; #证书公钥文件路径
- ssl_certificate_key ssl/itunic.key; #证书私钥文件路径
- ssl_session_timeout 5m; #5分钟session会话保持
- ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
- ssl_ciphers HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
- root /www/web/itunic_com/public_html;
- server_name itunic.com www.itunic.com;
- index index.html index.php index.htm;
- error_page 400 /errpage/400.html;
- error_page 403 /errpage/403.html;
- error_page 404 /errpage/404.html;
- error_page 503 /errpage/503.html;
- location ~ \.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- include fcgi.conf;
- }
- location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
- expires 1d;
- }
- location ~ .*\.(js|css|html|htm)?$ {
- expires 12h;
- }
- location ~ /\.ht {
- deny all;
- }
- access_log logs/itunic.com_access.log wwwlogs;
- error_log logs/itunic.com_error.log;
- }
将http强制转https
- server{
- listen 80;
- server_name itunic.com www.itunic.com;
- location / {
- rewrite (.*) https://itunic.com$1 permanent;
- }
- }
- server {
- listen 443 ssl;
- ssl_certificate ssl/itunic.crt; #证书公钥文件路径
- ssl_certificate_key ssl/itunic.key; #证书私钥文件路径
- ssl_session_timeout 5m; #5分钟session会话保持
- ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
- ssl_ciphers HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
- root /www/web/itunic_com/public_html;
- server_name itunic.com www.itunic.com;
- index index.html index.php index.htm;
- error_page 400 /errpage/400.html;
- error_page 403 /errpage/403.html;
- error_page 404 /errpage/404.html;
- error_page 503 /errpage/503.html;
- location ~ \.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- include fcgi.conf;
- }
- location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
- expires 1d;
- }
- location ~ .*\.(js|css|html|htm)?$ {
- expires 12h;
- }
- location ~ /\.ht {
- deny all;
- }
- access_log logs/itunic.com_access.log wwwlogs;
- error_log logs/itunic.com_error.log;
- }
本站采用第二种也推荐第二种,即强制转https。原因有二:第一,全站加密,更安全。第二,有利于SEO,在搜索引擎方面,https权重高于http。