Linux 部署Nginx反向代理服務 使用openssl自生成證書並配置https


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.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:error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory  

在redhat 64位機器上, nginx可能讀取的pcre文件為/lib64/libpcre.so.1文件.需要建立一個軟連接:

ln -s /usr/local/lib/libpcre.so.1 /lib64/  
  • 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中

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM