本篇文章將會以 Ubuntu 架設一個 NGINX 的靜態檔案伺服器

NGINX 設定檔

使用以下指令安裝完成 nginx

1
apt install nginx

/etc/nginx 可以看到 nginx.conf 檔案,確認檔案包含 include。nginx.conf 就像是 main 程式,透過引用來分開不同域名的 conf。註解 sites-enabled/*

1
2
include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;

之後要在 /etc/nginx/conf.d/ 加入 config

設定自己的設定檔

新增 /etc/nginx/conf.d/default.conf,並填入設定,以下設定檔作為範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 8080;
server_name _;
index index.html;
root /www/data;

location /week-1/ {
index latest/index.html;
}

location /week-1/navbar/ {
}

location /week-1/v1/ {
}

location /week-1/v2/ {
}
}
  1. 最外層 server block
  2. listen 可以填 port
  3. server_name _ 代表不驗證任何域名,所有域名都可以通過
  4. index 指定特定檔案作為 index,內層可以覆蓋外層
  5. location block 代表 host 底下的路由
  6. 沒有設定任何東西,代表打開那個路由,前面有設定抓 index
  7. root 代表要從哪個位置開始抓檔案