共計 1886 個字符,預計需要花費 5 分鐘才能閱讀完成。
這篇文章主要講解了“NGINX 怎么搭建靜態(tài)網(wǎng)站”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“NGINX 怎么搭建靜態(tài)網(wǎng)站”吧!
搭建 Http 靜態(tài)服務(wù)器環(huán)境
任務(wù)時間:15min ~ 30min
搭建靜態(tài)網(wǎng)站,首先需要部署環(huán)境。下面的步驟,將告訴大家如何在服務(wù)器上通過 Nginx 部署 HTTP 靜態(tài)服務(wù)。
00、安裝 Nginx
在 CentOS 上,可直接使用 yum 來安裝 Nginx
yum install nginx
-y
安裝完成后,使用 nginx
命令啟動 Nginx:
nginx
此時,訪問 http://ip 可以看到 Nginx 的測試頁面 [?]
如果無法訪問,請重試用 nginx
-s reload 命令重啟 Nginx
01、配置靜態(tài)服務(wù)器訪問路徑
外網(wǎng)用戶訪問服務(wù)器的 Web 服務(wù)由 Nginx 提供,Nginx 需要配置靜態(tài)資源的路徑信息才能通過 url 正確訪問到服務(wù)器上的靜態(tài)資源。
打開 Nginx 的默認配置文件 /etc/nginx
/nginx
.conf,修改 Nginx 配置,將默認的 root /usr/share/nginx
/html; 修改為: root /data/www;,如下:
示例代碼:/etc/nginx
/nginx
.conf
usernginx
;worker_processes auto;error_log /var/log/nginx
/error.log;pid /run/nginx
.pid; include /usr/share/nginx
/modules/*.conf; events { worker_connections 1024;} http { log_format main $remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for access_log /var/log/nginx
/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx
/mime.types; default_type application/octet-stream; include /etc/nginx
/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /data/www; include /etc/nginx
/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
配置文件將 /data/www/static 作為所有靜態(tài)資源請求的根路徑,如訪問: http:// 您的域名 /static/index.js,將會去 /data/www/static/ 目錄下去查找 index.js?,F(xiàn)在我們需要重啟 Nginx 讓新的配置生效,如:
nginx
-s reload
重啟后,現(xiàn)在我們應(yīng)該已經(jīng)可以使用我們的靜態(tài)服務(wù)器了,現(xiàn)在讓我們新建一個靜態(tài)文件,查看服務(wù)是否運行正常。
首先讓我們在 /data 目錄 下創(chuàng)建 www 目錄,如:
mkdir -p /data/www
02、創(chuàng)建第一個靜態(tài)文件
在 /data/www 目錄下創(chuàng)建我們的第一個靜態(tài)文件 index.html
示例代碼:/data/www/index.html
!DOCTYPE html html lang= zh head meta charset= UTF-8 title 第一個靜態(tài)文件 /title /head body Hello world!/body /html
如果無顯示,請刷新瀏覽器頁面
感謝各位的閱讀,以上就是“NGINX 怎么搭建靜態(tài)網(wǎng)站”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對 NGINX 怎么搭建靜態(tài)網(wǎng)站這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!