Contents

Linux - 設定 Web 伺服器(Nginx)

Nginx 是另一種比 Apache HTTP 更輕量且更簡單上的 Web 伺服器,也是反向代理與負載平衡中的佼佼者。

練習環境
可以使用 HowHow 的創建 Lab 練習環境,來實作 Web 伺服器與存取操作,使用方式請參閱 HowHow 網站 - Linux 練習 Lab 第二版

1. Nginx 簡介

早期的 Web 霸主從 Apache HTTP 逐漸變成 Nginx ,除了更佳的效能外,能處理更多的並行請求,在設定檔上又更加簡單,目前強烈建議使用的 Web 伺服器。

TLS 自簽憑證

在 HowHow 的 lab 環境可以直接在 bastion 中的 /data/home/student/lab_playbook 目錄下執行 git stash save && git pull 拉取自動產生 TLS Ansible 的 Playbook ,並自動簽發到指定的主機 /var/ssl 目錄內。

1
2
3
4
# bastion 主機
cd /data/home/student/lab_playbook
git stash save && git pull
ansible-playbook -l serverc.lab.example.com playbook.yml

2. 安裝及設定 nginx 伺服器

1
2
3
4
5
使用 Server A 為  Nginx Server 範例:

    1. Server A 上執行 nginx ,並為 www-a.lab.example.com 網域提供服務來至 /srv/www-a/www/ 其目錄內容 。
    2. 其他網域來至 /srv/default/www/ 期目錄內。
    3. 並且所有 http 的流量都強制轉至 https 。

2.1. 安裝 nginx 套件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
[root@servera ~]# yum module list *nginx*
Last metadata expiration check: 3:32:56 ago on Sun 26 Feb 2023 04:18:39 PM CST.
Rocky Linux 8 - AppStream
Name                  Stream                  Profiles                  Summary
nginx                 1.14 [d]                common [d]                nginx webserver
nginx                 1.16                    common [d]                nginx webserver
nginx                 1.18                    common [d]                nginx webserver
nginx                 1.20                    common [d]                nginx webserver

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

[root@servera ~]#  yum module install nginx:1.20 -y | tail -n 4
  perl-threads-shared-1.58-2.el8.x86_64
  rocky-logos-httpd-86.3-1.el8.noarch

Complete!
[root@servera ~]#
Nginx Life Cycle
預設會使用 nginx 1.14 版本,不過 1.14, 1.16 , 1.18 都已經 EOL 了,建議直接安裝 1.20 版本,其維護至 2023 年 11 月。

2.2. 創建 web 目錄及檔案

  1. 創建 /srv/www-a/www//srv/default/www/ 目錄。
1
[root@servera ~]# mkdir -p /srv/{www-a,default}/www
  1. 創建 html 顯示內容 。
1
2
[root@serverc ~]# echo 'www-a' > /srv/www-a/www/index.html
[root@serverc ~]# echo 'coming soon'  > /srv/default/www/index.html
  1. 設定 SELinux Context,並載入使生效
1
2
3
4
5
[root@serverc ~]# restorecon -Rv /srv/
Relabeled /srv/www-a/www from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /srv/www-a/www/index.html from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /srv/default/www from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /srv/default/www/index.html from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0

2.3. 設定 default 預設虛擬主機

Ngxin 常見類型為 :

  1. events : 一般連接處理
  2. http : 處理 HTTP 流量
  3. mail : 處理電子郵件流量
  4. stream : 用於 TCP & UDP 流量

其中最重要要關注的則是 http 處理,預設設定檔為 /etc/nginx/nginx.conf ,一般不會直接修改,會將自定義的設定寫在 /etc/nginx/conf.d/ 目錄下 ,請注意每一行結尾均須使用 ;為結束。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[root@servera ~]# cat >  /etc/nginx/conf.d/default.conf << EOF
server {
    listen 80 ;
    server_name _;
    return 301 https://$host$request_uri;

}
server {
    listen 443 ssl;
    server_name _;

    ssl_certificate /var/ssl/lab.crt;
    ssl_certificate_key /var/ssl/lab.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    location / {
        root /srv/default/www;
        index index.html index.htm;
    }
}
EOF
TLS 自簽憑證
使用 Lab 環境執行 Bastion 的 Playbook 會自動簽發 30 天的憑證,如果非 Lab 環境需自行簽章。

2.4. 設定 www-a.lab.example.com 虛擬主機

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[root@serverc ~]# cat > /etc/httpd/conf.d/www-a.conf [root@servera ~]# cat >  /etc/nginx/conf.d/www-a.conf << EOF
server {
    listen 80 ;
    server_name www-a www-a.lab.example.com;
    return 301 https://$host$request_uri;

}
server {
    listen 443 ssl;
    server_name www-a www-a.lab.example.com;

    ssl_certificate /var/ssl/lab.crt;
    ssl_certificate_key /var/ssl/lab.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    location / {
        root /srv/www-a/www;
        index index.html index.htm;
    }
}
EOF

2.5. 啟動伺服器及開放防火牆規則

1
2
3
4
5
6
[root@servera ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@servera ~]# firewall-cmd --permanent --add-service=http --add-service=https
success
[root@servera ~]# firewall-cmd --reload
success

3. Client 存取 Web 伺服器

3.1. 設定靜態解析

1
2
3
[root@serverb ~]# cat >> /etc/hosts << EOF
> 172.25.250.11 www-a.lab.example.com www-a
> EOF

3.2. 測試使用 www-awww-a.lab.example.com 存取

1
2
3
4
[root@serverb ~]# curl -k https://www-a.lab.example.com
www-a
[root@serverb ~]# curl -k https://www-a
www-a

3.3. 測試其他任意方式存取

1
2
3
4
[root@serverb ~]# curl -k https://servera.lab.example.com
coming soon
[root@serverb ~]# curl -k https://servera
coming soon
Note
囿於僅開放 www-xwww-x.lab.example.com 可以存取到正式的頁面,其他的流量均只能至 default 頁面。

4. 資安弱掃 Issue

只開放 TLS v1.2 與 TLS v1.3 版本

1
2
3
4
5
6
7
8
9
server {
    listen 443 ssl;
    server_name _;

    ssl_certificate /var/ssl/lab.crt;
    ssl_certificate_key /var/ssl/lab.key;
    ssl_protocols TLSv1.2 TLSv1.3;
...output omitted...
}
SSLProtocol
建議只開放 TLS v2 以上的協議,其餘的以下的均不安全不建議開放。

透過 curl 檢測可以驗證 tls 1.1 協議已經被關閉。

1
2
3
4
[root@serverb ~]# curl -k  --tlsv1.1  --tls-max 1.1 https://www-a.lab.example.com
curl: (35) error:141E70BF:SSL routines:tls_construct_client_hello:no protocols available
[root@serverb ~]# curl -k  --tlsv1.1  --tls-max 1.2 https://www-a.lab.example.com
www-a

5. 小結

Nginx 與先前的 Apache HTTP 文章設定比較起簡單又精簡的多,尤其在效能上面更是遠遠操超越,除了基本的 Web 之外還可以另外配置反向代理來使用,設定方式也是十分容易直覺。



如果你還沒有註冊 Like Coin,你可以在文章最下方看到 Like 的按鈕,點下去後即可申請帳號,透過申請帳號後可以幫我的文章按下 Like,而 Like 最多可以點五次,而你不用付出任何一塊錢,就能給我寫這篇文章的最大的回饋!