Linux 搭建Nginx並添加配置 SSL 證書


1. 安裝准備
 
1.1 gcc安裝
安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:
[root@nginx ~]# yum -y install gcc-c++

 

1.2 pcre安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。
[root@nginx ~]# yum -y install pcre pcre-devel

 

1.3 zlib安裝
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。
[root@nginx ~]# yum -y install zlib zlib-devel

 

1.4 OpenSSL安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
[root@nginx ~]# yum -y install openssl openssl-devel
 
2. Nginx安裝
2.1 Nginx版本
下載網址:https://nginx.org/en/download.html
選擇最新的穩定版nginx-1.10.3
版本說明:
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說是開發版
Stable version:最新穩定版,生產環境上建議使用的版本
Legacy versions:遺留的老版本的穩定版
2.2 Nginx下載
使用wget命令下載
1 [root@nginx ~]# wget -c https://nginx.org/download/nginx-1.10.3.tar.gz

 

如沒有wget命令則安裝:
[root@nginx ~]# yum -y install wget

 

2.3 解壓
[root@nginx ~]# tar -zxvf nginx-1.10.3.tar.gz
 
2.4.1 新建nginx用戶和組 (根據自己的需求 可忽略此步)
[root@nginx include]# groupadd nginx 
[root@nginx include]# useradd -g nginx -d /home/nginx nginx 
[root@nginx include]# passwd nginx

 

2.4.2第三方模塊安裝
本文以安裝第三方模塊sticky為例,版本為1.,2.5,
下載地址: https://pan.baidu.com/s/1Zpv6axGNUJkkGcam7EoLaQ 密碼:6jaq
Sticky是nginx的一個模塊,它是基於cookie的一種nginx的負載均衡解決方案,通過分發和識別cookie,來使同一個客戶端的請求落在同一台服務器上,默認標識名為route
1.客戶端首次發起訪問請求,nginx接收后,發現請求頭沒有cookie,則以輪詢方式將請求分發給后端服務器。
2.后端服務器處理完請求,將響應數據返回給nginx。
3.此時nginx生成帶route的cookie,返回給客戶端。route的值與后端服務器對應,可能是明文,也可能是md5、sha1等Hash值
4.客戶端接收請求,並保存帶route的cookie。
5.當客戶端下一次發送請求時,會帶上route,nginx根據接收到的cookie中的route值,轉發給對應的后端服務器。

http {
  #OK include vhost/xxx.conf;
  upstream shop_server{
  sticky;
  #Sticky是nginx的一個模塊,它是基於cookie的一種nginx的負載均衡解決方案,通過分發和識別cookie,來使同一個客戶端的請求落在同一台服務器上
  # server 192.168.1.23; 
  server 192.168.1.24;
  # server 192.168.1.25;
  keepalive 32; 
}

 

上傳解壓:
[root@nginx ~]# tar -zxvf nginx-goodies-nginx-sticky-module-ng-08a395c66e42..gz 
[root@nginx ~]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-1.2.5

 

2.4.3 安裝
  先卸載老版本nginx(若為首次安裝可忽略此步)

    查找並刪除mysql有關的文件

    find / -name nginx

    rm -rf 上邊查找到的路徑,多個路徑用空格隔開,或者下邊一條命令即可 

    find / -name nginx|xargs rm -rf

  安裝:
[root@nginx ~]# cd nginx-1.10.3 
[root@nginx nginx-1.10.3]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --add-module=/usr/local/nginx-sticky-1.2.5

 

指定用戶、路徑和模塊配置(可選):
./configure \ 
--user=nginx --group=nginx \ #安裝的用戶組 
--prefix=/usr/local/nginx \ #指定安裝路徑 
--with-http_stub_status_module \ #監控nginx狀態,需在nginx.conf配置 
--with-http_ssl_module \ #支持HTTPS 
--with-http_sub_module \ #支持URL重定向 
--with-http_gzip_static_module #靜態壓縮 
--add-module=/usr/local/nginx-sticky-1.2.5 #安裝sticky模塊

 

2.5 編譯
[root@nginx nginx-1.10.3]# make && make install
報錯:
];
 
解決方法:
修改ngx_http_sticky_misc.c文件,新增#include <openssl/sha.h>和#include <openssl/md5.h>模塊
[root@nginx nginx-1.10.3]# sed -i '12a #include <openssl/sha.h>' /usr/local/nginx-sticky-1.2.5/ngx_http_sticky_misc.c 
[root@nginx nginx-1.10.3]# sed -i '12a #include <openssl/md5.h>' /usr/local/nginx-sticky-1.2.5/ngx_http_sticky_misc.c

 

重新編譯:
[root@nginx nginx-1.10.3]# make && make install

 

3.6 nginx命令全局執行設置
[root@nginx bin]# cd /usr/local/nginx/sbin/ 
[root@nginx sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
 
4. Nginx相關命令
4.1 版本查看
[root@nginx ~]# nginx -v 

   nginx version: nginx/1.10.3

4.2 查看加載的模塊
[root@nginx ~]# nginx -V 

   nginx version: nginx/1.10.3

  built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)

  configure arguments: --add-module=/root/nginx-sticky-1.2.5/

4.3 啟停命令
4.3.1 啟動
[root@nginx nginx-1.10.3]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

 

4.3.2 停止
[root@nginx nginx-1.12.2]# nginx -s stop 
[root@nginx nginx-1.12.2]# nginx -s quit

 

4.3.3 動態加載
[root@nginx nginx-1.12.2]# nginx -s reload

 

4.3.4 測試配置文件nginx.conf正確性
[root@nginx ~]# nginx -t 
  nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
 
nginx -s quit:此方式停止步驟是待nginx進程處理任務完畢進行停止。
nginx -s stop:此方式相當於先查出nginx進程id再使用kill命令強制殺掉進程。
nginx -s reload:動態加載,當配置文件nginx.conf有變化時執行該命令動態加載。
4.4 開機自啟動
編輯/etc/rc.d/rc.local文件,新增一行/usr/local/nginx/sbin/nginx
1 [root@nginx rc.d]# cd /etc/rc.d 
2 [root@nginx rc.d]# sed -i '13a /usr/local/nginx/sbin/nginx' /etc/rc.d/rc.local 
3 [root@nginx rc.d]# chmod u+x rc.local

 

 
5. 更改默認端口
編輯配置文件/usr/local/nginx/conf/nginx.conf,將默認端口80修改為81:
1 [root@nginx ~]# view /usr/local/nginx/conf/nginx.conf

 

加載配置:
1 [root@nginx ~]# nginx -s reload
 
6. 訪問Nginx
6.1 關閉防火牆
[root@nginx ~]# firewall-cmd --state 
running
[root@nginx ~]# systemctl stop firewalld.service
[root@nginx ~]# firewall-cmd --state
not running

 

6.2 訪問Nginx

 7.開始配置SSL證書(證書需另行獲取,獲取的途徑很多,如阿里雲的服務或第三方服務購買)

進入 conf/nginx.conf 開始配置文件的修改。在修改配置文件之前,最好做一個備份,防止修改錯誤,也能及時回退錯誤
 
7.1、找到第一個監聽80端口的server:一下是我修改好的server
 
 1 server {
 2     listen 80;
 3     server_name 需要訪問的域名;
 4  
 5     rewrite ^(.*) https://$server_name$1 permanent; #這句是代表 把    http的域名請求轉成https
 6  
 7     #charset koi8-r;
 8     #access_log logs/host.access.log main;
 9     location / {
10         root html;
11         index index.html index.htm;
12        proxy_pass http://需要訪問的域名; #因為這里還是80端口,所以保持http就可以了
13  
14     }
15 } 
 
在實際的配置文件中,最好把我上面的備注刪除
 
7.2、第一個server修改好了之后。那么就需要開始配置第二個server。拉到文件的底部。看到有一個https類型的server。而且已經全部被注釋封上了,這是我們需要改的第二個server,如圖:
這里除了HTTPS server這行之外,其他的 # 刪除,啟動https模塊
 
 1 # HTTPS server
 2 #
 3 server {
 4     listen 443 ssl;
 5     server_name 需要訪問的域名,這里也不用加https;
 6  
 7     ssl on;
 8  
 9     ssl_certificate /usr/local/nginx/ssl/ssl.pem; #這里是ssl key文件存放的絕對路徑,根據自己的文件名稱和路徑來寫
10     ssl_certificate_key /usr/local/nginx/ssl/ssl.key; #這里是ssl key文件存放的絕對路徑,根據自己的文件名稱和路徑來寫
11  
12  
13     ssl_session_cache shared:SSL:1m;
14     ssl_session_timeout 5m;
15  
16     ssl_ciphers HIGH:!aNULL:!MD5;
17     ssl_prefer_server_ciphers on;
18  
19     location / {
20         proxy_pass http://需要訪問的域名:8080/;
21     }
22 }

 

7.3、檢驗配置文件是否有錯誤
 
 [root@localhost sbin]# nginx -t
 
如果nginx曾經安裝過SSL模塊,那么應該會顯示以下界面:(如果已經顯示配置成功,那么可以跳過這一步,直接重啟nginx就可以了)
 
可是大多數第一次安裝https證書,都會報錯,說缺少SSL模塊,如下:
 
 
7.4、這時候我們就可以先安裝SSL模塊:
先確認2個位置:
1)我的nginx是安裝在了/usr/local/nginx/下
2)我的nginx的源碼包放在了/usr/local/nginx/nginx/nginx-1.8.0下。如果沒有的話,重新下載你對應的nginx版本的源碼包,找個目錄解壓
 
3)目錄切換到我們的源碼包安裝位置:
 
cd /usr/local/nginx/nginx/nginx-1.8.0
 
4)執行語句,重新安裝ssl模塊:
 
./configure  --with-http_ssl_module
 
5)這時候應該就執行配置成功了。配置成功后,那么就需要編譯我們的配置。(注意這里只能用make 而不要用make install,因為執行make install是覆蓋安裝的意思)
運行:
 
make
 
等待執行完成后,我們需要把新編譯的nginx模塊替換原來的nginx。
 
6)還是老規矩,先備份舊的nginx,執行語句(這里面復制的文件的路徑需要根據你們安裝的目錄自行修改,如果和我安裝的路徑是一樣的那么可以直接運行該語句):
 
  cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
 
  先停止nginx進程,然后
 
    cp objs/nginx /usr/local/nginx/sbin/nginx
 
7)然后就是啟動nginx。在啟動之前,也可以在測試一次配置文件是否已經生效:
 
#先切換到sbin目錄
cd /usr/local/nginx/sbin/
#檢測nginx的配置文件是否有錯誤
./niginx -t
 
看到這樣的,就是已經成功了
 
 
最后啟動nginx:
 
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
 
因為剛才替換nginx模塊的時候是把nginx進程都殺死了,所以要用上面的命令進行啟動,而不能使用reload重啟。
 
nginx正常啟動后,我們在訪問我們的網站,發現https就已經配置好了:
 
附上nginx配置文件
  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 
 34     server {
 35         listen       80;
 36         server_name  test.feihe.com;
 37         #rewrite ^(.*) https://$server_name$1 permanent; #這句是代表 把http的域名請求轉成https
 38 
 39         #charset koi8-r;
 40 
 41         #access_log  logs/host.access.log  main;
 42 
 43         location / {
 44             root   html/;
 45             index  index.html index.htm;
 46         }
 47 
 48 
 49         location /coa/ {
 50             proxy_pass http://localhost:8080;
 51         }
 52 
 53         location /front/ {
 54             proxy_pass http://localhost:8080;
 55         }
 56 
 57         #error_page  404              /404.html;
 58 
 59         # redirect server error pages to the static page /50x.html
 60         #
 61         error_page   500 502 503 504  /50x.html;
 62         location = /50x.html {
 63             root   html;
 64         }
 65 
 66         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 67         #
 68         #location ~ \.php$ {
 69         #    proxy_pass   http://127.0.0.1;
 70         #}
 71 
 72         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 73         #
 74         #location ~ \.php$ {
 75         #    root           html;
 76         #    fastcgi_pass   127.0.0.1:9000;
 77         #    fastcgi_index  index.php;
 78         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 79         #    include        fastcgi_params;
 80         #}
 81 
 82         # deny access to .htaccess files, if Apache's document root
 83         # concurs with nginx's one
 84         #
 85         #location ~ /\.ht {
 86         #    deny  all;
 87         #}
 88     }
 89 
 90 
 91     # another virtual host using mix of IP-, name-, and port-based configuration
 92     #
 93     #server {
 94     #    listen       8000;
 95     #    listen       somename:8080;
 96     #    server_name  somename  alias  another.alias;
 97 
 98     #    location / {
 99     #        root   html;
100     #        index  index.html index.htm;
101     #    }
102     #}
103 
104 
105     # HTTPS server         ssl_protocols  SSLv2 SSLv3 TLSv1;
106     
107     server {
108         listen       443 ssl;
109         server_name  test.feihe.com;
110 
111         ssl                  on;
112         ssl_certificate      /usr/local/nginx/ssl/_.xx.com_bundle.crt;  #這里是ssl pem文件存放的絕對路徑
113         ssl_certificate_key  /usr/local/nginx/ssl/_.xx.com.key;  #這里是ssl key文件存放的絕對路徑
114 
115         ssl_session_cache    shared:SSL:1m;
116         ssl_session_timeout  5m;
117 
118 
119         ssl_ciphers  HIGH:!aNULL:!MD5;
120         ssl_prefer_server_ciphers   on;
121 
122         location / {
123             root   html/;
124             index  index.html index.htm;
125         }
126 
127 
128         location /coa/ {
129             proxy_pass http://localhost:8080;
130         }
131 
132         location /front/ {
133             proxy_pass http://localhost:8080;
134         }
135     }
136 
137 }

 


免責聲明!

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



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