Nginx搭建 Https 運行Docker項目

1、環境介紹

阿里雲centos 7.4php

Nginx/1.12.1html

Let’s Encrypt 永久免費 SSL 證書nginx

Docker已打包好項目的鏡像 (詳情參考http://www.cnblogs.com/killall007/p/8477484.html)c++

2、Nginx安裝

首先安裝ssl,pcre,zlib,gzip等,使用以下命令:git

(yum install -y httpd-devel pcre perl pcre-devel zlib zlib-devel GeoIP GeoIP-devel)
要安裝下一些gcc庫用於編譯 和一些nginx的擴展lib包:
 yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel

下載所需的包: 
分別從這些網站下載最新穩定版:github

http://www.pcre.org/
http://zlib.net/
http://openssl.org/
cd 
wget https://ftp.pcre.org/pub/pcre/pcre-8.00.tar.gz
wget http://zlib.net/zlib-1.2.11.tar.gz
wget ftp://ftp.openssl.org/source/openssl-1.0.2n.tar.gz

解壓這些文件,並不須要安裝:web

tar -xvf pcre-8.00.tar.gz
tar -xvf openssl-1.0.2n.tar.gz
從http://www.nginx.org上下載nginx源碼包:
cd 
wget http://nginx.org/download/nginx-1.12.1.tar.gz
tar -xvf nginx-1.12.1.tar.gz 
cd nginx-1.12.1

進入nginx目錄
[root@admin local]# cd nginx-1.12.1
首先,設置安裝目錄爲 /usr/local/nginx
[root@admin nginx-1.12.1]# ./configure --with-http_ssl_module  
若是沒有報錯,開始編譯安裝
[root@admin nginx-1.12.1]# make  
[root@admin nginx-1.12.1]# make install

防火牆開啓80、443端口docker

systemctl start firewalld.service
firewall-cmd --add-port=80/tcp --permanent  
firewall-cmd --add-port=443/tcp --permanent
firewall-cmd --reload 
netstat -tunpl #查看已開啓端口及服務

啓動nginx服務centos

進入安裝目錄
 /usr/local/nginx
[root@admin ~]# cd /usr/local/nginx  
[root@admin sbin]# ./nginx

查看進程,能夠看到nginx的master和worker進程
[root@admin sbin]# ps -ef | grep nginx  
root     32150     1  0 13:28 ?        00:00:00 nginx: master process ./nginx  
nobody   32151 32150  0 13:28 ?        00:00:00 nginx: worker process  
root     32154 28494  0 13:28 pts/1    00:00:00 grep nginx ``
能夠經過訪問ip:80測試是否成功

3、安裝Let’s Encrypt 永久免費 SSL 證書(有效期90天)

yum -y install git
git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
chmod +x letsencrypt-auto

安裝證書:瀏覽器

./letsencrypt-auto certonly  --email 120459905@qq.com -d www.xiaowenxiao.com   #-d後對應的是域名

根據提示安裝完後會顯示證書和私鑰的保存位置

在nginx.conf中添加代碼:

    #gzip  on;

    server {
            listen 80;
            server_name www.xiaowenxiao.xin;
            rewrite ^/(.*) https://$server_name$1 permanent;    #跳轉到Https
    }
    server {
        listen       443;
        server_name  www.xiaowenxiao.xin;
        ssl on;
        ssl_certificate      /etc/letsencrypt/live/www.xiaowenxiao.xin/fullchain.pem; #證書位置
        ssl_certificate_key  /etc/letsencrypt/live/www.xiaowenxiao.xin/privkey.pem; #私鑰位置


        location / {
              proxy_pass http://172.17.0.2:5000; #反向代理到把請求轉發到已經部署好項目的docker容器
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

4、啓動docker容器運行項目

(詳情參考http://www.cnblogs.com/killall007/p/8477484.html)

docker run -p 5000 xiaowenxiao/hellodocker.web:v1

5、重啓nginx

[root@admin sbin]# ./nginx -s reload
(中止服務:ps -ef | grep nginx查進程號以後 kill -9 進程號 便可)

瀏覽器輸入:http://www.xiaowenxiao.xin 自動跳轉https://www.xiaowenxiao.xin 請求會轉發到docker已部署好的項目上。

 

至此,https部署升級結束。

相關文章
相關標籤/搜索