关于nginx:刘建Nginx配置SSL

2次阅读

共计 2224 个字符,预计需要花费 6 分钟才能阅读完成。

Nginx 配置 SSL

Author: Abbott Liu(刘建)

Create: 2021/09/18 5:50

Update: 2021/09/18 5:50

在咱们下载的证书文件中有一个 Nginx 的文件夹,这外面的两个文件都是须要的。咱们须要把这个两个文件上传到 linux 服务器中,举荐放到 /etc/ssl/ 目录下

而后咱们须要去找到 nginx 的配置文件。

ps -ef | grep nginx

能够看到 nginx 的目录是/usr/local/nginx

那么咱们须要找到 nginx.conf 文件并批改

cd /usr/local/nginx/conf
vim nginx.conf

配置文件内容

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  edj365.com;
        rewrite ^/(.*)$ https://edj365.com:443/$1 permanent;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {root   html;}
    }
    
    # http 节点中能够增加多个 server 节点
    server {
        #监听 443 端口
        listen 443 ssl;
        #对应的域名,把 edj365.com 改成你们本人的域名就能够了
        server_name edj365.com;
        #从腾讯云获取到的第一个文件的全门路
        ssl_certificate cert/edj365.com.pem; #将 domain name.pem 替换成您证书的文件名。ssl_certificate_key cert/edj365.com.key; #将 domain name.key 替换成您证书的密钥文件名。ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        #这是我的主页拜访地址,因为应用的是动态的 html 网页,所以间接应用 location 就能够实现了。location / {
                #文件夹
                root /usr/local/nginx/html;
                #主页文件
                index index.html;
        }
    }

}
/usr/local/nginx/sbin/nginx

报错

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

开始 Nginx 的 SSL 模块

Nginx 如果未开启 SSL 模块,配置 Https 时提醒谬误
起因也很简略,nginx 短少 http_ssl_module 模块,编译装置的时候带上 –with-http_ssl_module 配置就行了,然而当初的状况是我的 nginx 曾经装置过了,怎么增加模块,其实也很简略,往下看

做个阐明:我的 nginx 的装置目录是 /usr/local/nginx 这个目录,我的源码包在 /download/nginx-1.19.9 目录

切换到源码包:

cd /download/nginx-1.19.9

# 查看 nginx 原有的模块
/usr/local/nginx/sbin/nginx -V

在 configure arguments: 前面显示的原有的 configure 参数如下:

# 运行命令即可,等配置完
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

执行 make 命令,然而不要执行 make install,因为 make 是用来编译的,而 make install 是装置,不然你整个 nginx 会从新笼罩的。

make

# 先备份,备份则不必执行
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

cp ./objs/nginx /usr/local/nginx/sbin/

而后启动 nginx,仍能够通过命令查看是否曾经退出胜利

/usr/local/nginx/sbin/nginx -V

Nginx 配置 Http 和 Https 共存

server {
            listen 80 default backlog=2048;
            listen 443 ssl;
            server_name wosign.com;
            root /var/www/html;
  
            ssl_certificate /usr/local/Tengine/sslcrt/ wosign.com.crt;
            ssl_certificate_key /usr/local/Tengine/sslcrt/ wosign.com .Key;
        }
正文完
 0