從零開始配置 Nginx + HTTPS

Nginx

Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。其特色是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等。html

HTTPS

HTTPS(全稱:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全爲目標的HTTP通道,簡單講是HTTP的安全版。即HTTP下加入SSL層,HTTPS的安全基礎是SSL,所以加密的詳細內容就須要SSL。 它是一個URI scheme(抽象標識符體系),句法類同http:體系。用於安全的HTTP數據傳輸。https:URL代表它使用了HTTP,但HTTPS存在不一樣於HTTP的默認端口及一個加密/身份驗證層(在HTTP與TCP之間)。這個系統的最初研發由網景公司(Netscape)進行,並內置於其瀏覽器Netscape Navigator中,提供了身份驗證與加密通信方法。如今它被普遍用於萬維網上安全敏感的通信,例如交易支付方面。node

安裝 Nginx

## 準備基本的東西
yum install gcc-c++
yum -y install pcre*
yum -y install openssl*

## 把下載文件、站點數據都放到 home 文件夾裏面

## 先下載 Nginx,目前最新穩定版本是1.12.2

wget http://nginx.org/download/nginx-1.12.2.tar.gz

## 解壓壓縮包
tar -zxvf nginx-1.12.2.tar.gz

## 進入文件夾,準備安裝
cd nginx-1.12.2

## 設置安裝目錄爲 /usr/local/nginx
## 由於要使用ssl 因此須要如下額外的模塊

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

## 若是沒有報錯,開始編譯安裝
make

## make 這步可能會報錯
## ./configure: error: SSL modules require the OpenSSL library.
## 再安裝一些東西
## yum -y install openssl openssl-devel

## 安裝
make install

## 啓動nginx服務
cd /usr/local/nginx/sbin
./nginx

## 先搞一個簡單的服務吧
## 在 home 文件夾下面搞

cd /home
mkdir wwwroot
cd wwwroot
mkdir www.onlyling.com

## 把站點程序放到 `www.onlyling.com` 文件夾
## 程序啓動佔用 3000 端口好
## 靜態資源目錄 /home/wwwroot/www.onlyling.com/app/public

## 修改 nginx.conf ,通常狀況使用軟連的方式,但仍是不太懂,直接修改默認的配置
cd /usr/local/nginx/conf
vi nginx.conf

## 簡單的配置一個 http 服務
## 參考了 Thinkjs 線上部署使用 Nginx 的代碼
## https://thinkjs.org/zh-cn/doc/3.0/deploy.html 
server {
    listen       80;
    server_name  www.onlyling.com;
    root /home/wwwroot/www.onlyling.com/app/public;
    
    set $node_port 3000;
    location / {
        proxy_http_version 1.1;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://127.0.0.1:$node_port$request_uri;
        proxy_redirect off;
        root   html;
        index  index.html index.htm;
    }
}

## 保存好後重啓
## 沒有配置別名,嗯,有點長
/usr/local/nginx/sbin/nginx -s reload

## www.onlyling.com 解析到服務器,應該就能夠訪問這個站點了

HTTPS 證書

此次證書是本身搞的,用的是 acme.sh 查看倉庫,能夠實現每60天自動更新一次證書,很棒哦。nginx

## 按照文檔提示安裝
## 安裝的時候會提示安裝另外一個服務器程序,咱們用 Nginx 了,因此不須要
curl  https://get.acme.sh | sh

## 驗證身份、生成證書
## 驗證身份其實就是在站點生成一個文件,經過域名訪問文件,有就經過
## 因此後面的目錄指定到了站點的靜態資源文件(反正個人程序裏就把 public 設置成根目錄)
acme.sh  --issue  -d www.onlyling --webroot  /home/wwwroot/www.onlyling.com/app/public

## 等待一分鐘左右吧
## 生成了蠻多的文件

## 複製證書
## 可能會提示沒有文件夾,手動按目錄建立
## 可是最後一步,我沒有執行成功,就手動配置了
## 證書已經複製到了 /etc/nginx/ssl 文件夾
acme.sh  --installcert  -d  <domain>.com   \
        --key-file   /etc/nginx/ssl/<domain>.key \
        --fullchain-file /etc/nginx/ssl/fullchain.cer \
        --reloadcmd  "service nginx force-reload"

手動配置 Nginx + HTTPS

cd /usr/local/nginx/conf
vi nginx.conf

## 在剛剛配置 http 那裏添加一個 https 服務
server {
    listen 443 ssl;
    server_name www.onlyling.com;
    root /home/wwwroot/www.onlyling.com/app/public;
    set $node_port 3000;

    ssl_certificate /etc/nginx/ssl/fullchain.cer;
    ssl_certificate_key /etc/nginx/ssl/www.onlyling.com.key;

    keepalive_timeout   70;
    fastcgi_param   HTTPS               on;
    fastcgi_param   HTTP_SCHEME         https;
    server_tokens off;

    location / {
        proxy_http_version 1.1;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://127.0.0.1:$node_port$request_uri;
        proxy_redirect off;
        root   html;
        index  index.html index.htm;
    }
}

## 若是隻想開啓 https
## 在 http 配置處添加
## rewrite ^ https://$http_host$request_uri? permanent;

## 重啓 Nginx 服務
## 大概、應該、可能就搞定了
## 惟一不肯定的是 acme.sh 可否自動更新,畢竟沒有按照它的步驟走下去

原文閱讀:從零開始配置 Nginx + HTTPSc++

相關文章
相關標籤/搜索