Linux 部署Nginx反向代理服务 使用openssl自生成证书并配置https

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

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

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

  依赖包说明:linux

  一、编译依赖 gcc 环境,因此须要:gcc gcc-c++;nginx

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

  三、zlib 库提供了不少种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,因此须要在 Centos 上安装 zlib 库,因此须要:zlib zlib-devel ;正则表达式

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

  若是经过yum没法安装某个依赖包,能够下载下来,经过解压、make && make install的方式安装centos

我在使用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.org/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中

相关文章
相关标签/搜索