這篇文章將介紹如何利用Nginx部署靜態網站。
之前寫過2篇有關Nginx的文章,一篇是《利用nginx,騰訊雲免費證書制作https》,另外一篇是《linux安裝nginx》,如果有需要可以看一下,這一篇文章介紹如何使用Nginx部署靜態網站,需要在服務器部署靜態網站的同學可以看一下。
准備工作
首先我們需要在本地或者服務器上安裝Nginx,這時就用到了我之前的文章。
Linux---《linux安裝nginx》
Windows---官網下載個壓縮包解壓一下即可官網傳送門
配置Nginx主配置
在安裝目錄下的conf目錄下可以找到一個nginx.conf文件,打開這個文件。其中需要設置的內容主要就幾項,即如下帶有注釋的地方,nginx.conf完整內容如下:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
charset utf-8; # 設置編碼格式
server {
listen 8081; # 端口號
server_name _; # 配置域名信息
root web/; # 靜態頁面根目錄
index index.html;
}
}
由於只是做一個簡單的映射,所以我將靜態頁面放到了nginx目錄下,即如圖位置,默認頁面也設置為web目錄下index.html文件,如圖
html頁面為簡單測試頁面,內容很簡單。
index.html
<html>
<head>
<title>index</title>
</head>
<body>
<p>body</p>
<p>title</p>
<a href="edit.html">跳轉</a>
</body>
</html>
edit.html
<html>
<head>
<title>edit</title>
</head>
<body>
<p>body</p>
<p>title</p>
<a href="index.html">跳轉</a>
</body>
</html>
啟動Nginx
打開命令行,進入nginx目錄,開啟nginx
d:
cd D:\tool\nginx-1.15.2
start nginx
瀏覽器訪問測試
在瀏覽器訪問http://localhost:8081/,可以看到如圖
總結
到這里本篇文章就算結束了,其實主要Nginx部署靜態網站就是將Nginx主配置文件配置好即可,過程很簡單。