关于Nginx配置ssl证书实现https安全访问

来自:网络
时间:2023-05-29
阅读:
目录

前题条件,拥有服务器与可以解析到该服务器的自己的域名。

一、Nginx的安装与配置

若已安装好了Nginx,则需查看自己的Nginx是否开启了SSL的模块功能:

./nginx -V

关于Nginx配置ssl证书实现https安全访问

 显示如上,则代表ssl功能已开启,否则可能出现以下错误提示:

nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx.conf:%

安装步骤

1.官网下载nginx压缩包 nginx: download
我们先去官网下载一个最新稳定版的nginx

关于Nginx配置ssl证书实现https安全访问

然后使用xftp或者rz上传到我们的服务器

# 解压压缩包

tar -zxvf nginx-1.22.1.tar.gz

 然后进入到目录里面,查看是否有可执行权限(是不是绿色的),没有赋予执行权限

# 赋予执行权限

chmod +x configure

2.安装nginx所需要的环境

在安装之前先安装nginx所需要的一些环境

# c编译器
yum -y install gcc gcc-c++ autoconf automake make
# 解析正则的pcre库
yum install -y pcre pcre-devel
# 添加对gzip的支持
yum install -y zlib zlib-devel
# SSL
yum -y install pcre  pcre-devel zlib  zlib-devel openssl openssl-devel

3、开始安装

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make

二、SSL证书获取

可以使用openssl.cn获取免费的证书:

百度安全验证

三、Nginx配置

将获取的ssl证书放到服务器上,配置相应的路径。

    server {
        listen       80;
        #填写绑定证书的域名
        server_name  dragonwu.xyz;
    
        #charset koi8-r;
    
        #access_log  logs/host.access.log  main;
    
        #强制将http的URL重写成https
        return 301 https://$host$request_uri;
    }
 
    server {
        listen       443 ssl;
        server_name  dragonwu.xyz; #你的域名
 
        ssl_certificate      /usr/local/ssl/dragonwu.xyz_cert_chain.pem; #证书
        ssl_certificate_key  /usr/local/ssl/dragonwu.xyz_key.key;  #证书
 
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
 
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
    }

Nginx服务器重新加载:

./nginx -s reload

关于Nginx配置ssl证书实现https安全访问

注意事项:443端口一定要打开,之前我就是因为443端口被防火墙保护,一直访问不到,开放443端口即可! 

返回顶部
顶部