Nginx下升级https的方法步骤
购买证书
可以去阿里云的云盾证书服务购买
下载证书
在证书控制台下载Nginx版本证书。下载到本地的压缩文件包解压后包含:
- .pem文件:证书文件
- .key文件:证书的私钥文件(申请证书时如果没有选择 自动创建CSR,则没有该文件)
配置Nginx
1、在Nginx的安装目录下创建cert目录,并且将下载的全部文件拷贝到cert目录中,如果申请证书时是自己创建的CSR文件,请将对应的私钥文件放到cert目录下。
2、打开Nginx安装目录下conf目录中的nginx.conf文件
#usernobody;
worker_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pidlogs/nginx.pid;
events{
worker_connections1024;
}
http{
includemime.types;
default_typeapplication/octet-stream;
#log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
#'$status$body_bytes_sent"$http_referer"'
#'"$http_user_agent""$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfileon;
#tcp_nopushon;
#keepalive_timeout0;
keepalive_timeout65;
gzipon;#开启gzip
gzip_min_length1k;#低于1kb的资源不压缩
gzip_comp_level3;#压缩级别【1-9】,越大压缩率越高,同时消耗cpu资源也越多,建议设置在4左右。
gzip_typestext/plainapplication/javascriptapplication/x-javascripttext/javascripttext/xmltext/css;#需要压缩哪些响应类型的资源,多个空格隔开。不建议压缩图片,下面会讲为什么。
gzip_disable"MSIE[1-6]\.";#配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
gzip_varyon;#是否添加“Vary:Accept-Encoding”响应头
server{
listen80defaultbacklog=2048;#配置http可用
listen443ssl;#配置https
server_namelocalhost;
ssl_certificate../cert/hzzly.pem;#配置证书文件
ssl_certificate_key../cert/hzzly.key;#配置私钥文件
ssl_session_cacheshared:SSL:1m;
ssl_session_timeout5m;
ssl_ciphersHIGH:!aNULL:!MD5;
ssl_prefer_server_cipherson;
location/{
root/home/hzzly;
indexindex.htmlindex.htm;
}
#location^~/apis/{
#proxy_set_headerHost$host;
#proxy_set_headerX-Real-IP$remote_addr;
#proxy_set_headerX-Forwarded-Server$host;
##匹配任何以/apis/开始的请求,并停止匹配其它location
#proxy_passhttp://xxxxxxxxxx/;
#}
#location^~/assets/{
#gzip_staticon;
#expiresmax;
#add_headerCache-Controlpublic;
#}
}
}
3、重启Nginx
$cd/usr/local/nginx/sbin $./nginx-sreload
错误详解
1、Nginx如果未开启SSL模块,配置Https时提示错误
nginx:[emerg]the"ssl"parameterrequiresngx_http_ssl_modulein...
Nginx开启SSL模块
切换到源码包:
$cd/usr/local/src/nginx-1.16.0
修改新的configure参数
$./configure--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module
配置完成后,运行命令
$make//这里不要进行makeinstall,否则就是覆盖安装
备份原有已安装好的nginx
$cp/usr/local/nginx/sbin/nginx/usr/local/nginx/sbin/nginx.bak
将刚刚编译好的nginx覆盖掉原有的nginx
$cp./objs/nginx/usr/local/nginx/sbin/
重启Nginx
$cd/usr/local/nginx/sbin $./nginx-sreload
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。