環(huán)境說明
操作系統(tǒng): CentOS 7.2 64位
1. 準備NGINX靜態(tài)服務器環(huán)境
搭建靜態(tài)網站肤频,首先需要部署環(huán)境戈泼。下面的步驟同诫,將告訴大家如何在服務器上通過 Nginx 部署 HTTP 靜態(tài)服務咙俩。
1. 安裝 Nginx
在 CentOS 上向胡,可直接使用 yum 來安裝 Nginx
yum install nginx -y
2. 檢查是否安裝成功
nginx
然后訪問 http://IP 可以看到 Nginx 的測試頁面
3. 配置靜態(tài)服務器訪問路徑
外網用戶訪問服務器的 Web 服務由 Nginx 提供,Nginx 需要配置靜態(tài)資源的路徑信息才能通過url正確訪問到服務器上的靜態(tài)資源亡脑。
打開 Nginx 的默認配置文件 /etc/nginx/nginx.conf 堕澄,修改 Nginx 配置,將默認的<code>root /usr/share/nginx/html</code>; 修改為: <code>root /data/www</code>;霉咨,如下配置:
user nginx;
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 {
}
}
}
配置文件將<code> /data/www</code> 作為所有靜態(tài)資源請求的根路徑蛙紫,如訪問: http://IP/index.js,就會去 <code> /data/www/</code> 目錄下去查找 index.js⊥窘洌現在我們需要重啟 Nginx 讓新的配置生效坑傅,如:
nginx -s reload
重啟后,現在我們應該已經可以使用我們的靜態(tài)服務器了喷斋,現在讓我們新建一個靜態(tài)文件唁毒,查看服務是否運行正常。
首先讓我們在<code> /data </code>目錄 下創(chuàng)建 www 目錄星爪,如:
mkdir -p /data/www
4. 創(chuàng)建一個靜態(tài)頁面
在<code>/data/www</code>目錄下枉证,創(chuàng)建一個靜態(tài)文件<code>index.html</code>,如下代碼:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Test Static Page</title>
</head>
<body>
Hello world移必!
</body>
</html>
5. 測試
現在訪問 http://IP/index.html 就可以看到頁面輸出 Hello world!
到此,一個基于 Nginx 的靜態(tài)服務器就搭建完成了毡鉴,現在所有放在 /data/www 目錄下的的靜態(tài)資源都可以直接通過域名訪問崔泵。
附1 . 關于微信公眾號
微信公眾號ID:jsj201501
微信公眾號名稱:瞎說開發(fā)那些事
感謝您的關注和閱讀,希望這篇文章能為您帶來幫助猪瞬。
歡迎轉載與分享憎瘸,也請注明出處。
如果您有需要了解的關于Java開發(fā)陈瘦、RPA的等內容幌甘,也可以給我留言或發(fā)郵件 (shexd1001@gmail.com)。
附2 . 關于本文作者
本文作者:折(she) 向東
微信號:wxxdong2102
識別以下二維碼,可以與作者進行更為深入的交流锅风。