Linux 部署Nginx-1.16.1 使用openssl自生成证书并配置https

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Linux 部署Nginx-1.16.1 使用openssl自生成证书并配置https

七星6609   2019-11-18 我要评论

1.安装Nginx编译所依赖的包

  正常centos中可以使用yum安装一下依赖包:

yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

  依赖包说明:

  1、编译依赖 gcc 环境,所以需要:gcc gcc-c++;

  2、PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库,所以需要:pcre pcre-devel ;

  3、zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库,所以需要:zlib zlib-devel ;

  4、OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库,所以需要:openssl openssl-devel ;

  如果通过yum无法安装某个依赖包,可以下载下来,通过解压、make && make install的方式安装

我在使用yum安装pcre时就就没有安装成功,导致在make时,报错:

make: *** No rule to make target `build', needed by `default'. Stop.

 如果使用yum安装不上pcre时,可以去官网(https://ftp.pcre.org/pub/pcre/)下载对应的压缩包,再解压安装:

tar zxvf pcre-8.43.tar.gz

cd pcre-8.43

./configure

make && make install

如果使用yum安装OpenSSL失败是,可以去(https://www.openssl.org/source/)下载OpenSSL压缩包,解压安装:

tar zxvf openssl-1.0.2t.tar.gz

cd openssl-1.0.2t

./config --prefix=/usr/local/ --openssldir=/usr/local/openssl -g3 shared zlib-dynamic enable-camellia

make && make install

测试是否可用:opensslversion

如果zlib使用yum安装失败,可以去http://www.zlib.net/下载压缩包,解压安装:

tar zxvf zlib-1.2.11.tar.gz

cd zlib-1.2.11

./configure

make && make install

 2.下载安装Nginx

  • 去官网下载Nginx:wget https://nginx.orghttps://img.qb5200.com/download-x/download/nginx-1.16.1.tar.gz
  • 解压安装:    
tar zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/opt/nginx/server 
make && make install

  这样Nginx安装的sbin,conf相关的目录就会在/opt/nginx/server中生成,

  • 测试安装是否成功:
[root@s1 sbin]# ./nginx -V
nginx version: nginx/1.16.1
built by gcc 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx/server --with-http_stub_status_module --with-http_ssl_module

  这里的 --with-http_stub_status_module --with-http_ssl_module 是配置https时需要添加的ssl模块,后面会有介绍,如果没有这两个模块使用https时会有报错:

nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /opt/nginx/server/conf/nginx.conf:37
  • 启动Nginx:
cd /opt/nginx/server/sbin
./nginx
  • nginx服务相关命令:
./nginx -t                #验证nginx.conf文件正确性
./nginx -s reload         #重新加载nginx.conf文件
./nginx -s stop           #停止Nginx服务
  • 验证服务是否启动成功:

可以查看端口:

[root@s1 sbin]# netstat -ntlp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      349/nginx: master 

也可以通过浏览器:http://ip

至此Nginx正常配置已完成,但是如果要是使用https,还需要配置证书和Nginx支持https相关的模块

首先,Nginx添加支持https的模块,在安装时./configuration 需要添加ssl相关模块:

./configure --prefix=/opt/nginx/server --with-http_stub_status_module --with-http_ssl_module

生成证书:

1.首先使用openssl执行如下命令生成一个key:

openssl genrsa -des3 -out nginx.key 1024

然后他会要求你输入这个key文件的密码。不推荐输入。因为以后要给nginx使用。每次reload nginx配置时候都要你验证这个PAM密码的。

由于生成时候必须输入密码。你可以输入后 再删掉。

mv nginx.key xxx.key

openssl rsa -in xxx.key -out nginx.key

rm xxx.key

2.然后使用openssl 根据这个key文件生成证书请求文件:

openssl req -new -key nginx.key -out nginx.csr

以上命令生成时候要填很多东西 一个个看着写吧(可以随便,毕竟这是自己生成的证书,但是如果使用java程序访问时,需要将在输入用户名或服务器名时,输入自己的域名,不然会报找不到匹配的域名证书错误)

3.最后根据这2个文件生成crt证书文件:

openssl x509 -req -days 3650 -in nginx.csr -signkey nginx.key -out nginx.crt

4.最后使用到的文件是key和crt文件。如果需要用pfx 可以用以下命令生成:

openssl pkcs12 -export -inkey nginx.key -in nginx.crt -out nginx.pfx

配置nginx https:

需要在nginx.conf配置文件中添加:

server {
            listen       443 ssl;
            server_name  httpfs.test.com;

            ssl_protocols SSLv2 SSLv3 TLSv1;
            #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

            ssl_certificate      /opt/nginx/ssl/nginx.crt;
            ssl_certificate_key  /opt/nginx/ssl/nginx.key;
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
            ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
            ssl_prefer_server_ciphers  on;
            location / {
                proxy_pass http://httpfs/;
            }
         }

我的nginx.conf配置文件:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream httpfs {
    server 127.0.0.1:14000;
    }

    server {
        listen       80;
        server_name  httpfs.test.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
          proxy_pass http://httpfs/;
     }

        #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;
        }

    }


    # HTTPS server
    server {
        listen       443 ssl;
        server_name  httpfs.test.com;

        ssl_protocols SSLv2 SSLv3 TLSv1;
        #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    
        ssl_certificate      /opt/nginx/ssl/nginx.crt;
        ssl_certificate_key  /opt/nginx/ssl/nginx.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        #ssl_ciphers  HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers  on;
        location / {
            proxy_pass http://httpfs/;
        }
     }
}

重启nginx后,使用https访问就可以了。

【注意】

1.如果不能访问,请检查防火墙配置

2.如果不能访问,可以将域名配置到hosts中

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们