1. 程式人生 > >基於nginx的靜態網頁部署

基於nginx的靜態網頁部署

ref 返回 follow add ID pst 文章 open mon

背景:

一序列的html網頁需要部署

基於nginx的部署:

本文采用的基於openresty的nginx 配置。
簡單地配置 Nginx 的配置文件,以便在啟動 Nginx 時去啟用這些配置即可實現對於編寫好的html網頁的點擊跳轉訪問。而本文的重點也是於此。

配置方式1:

Nginx 的配置系統由一個主配置文件和其他一些輔助的配置文件構成。這些配置文件均是純文本文件,一般地,我們只需要配置主配置文件就行了。/usr/local/openresty/nginx/conf 下的配置文件修改如下:
配置信息:

#user  nobody;worker_processes  1;

#error_log logs/error.log;

#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
resolver 10.1.16.10;
include mime.types;
default_type application/octet-stream;

log_format main $remote_addr\t$remote_user\t[$time_local]\t$request

‘\t$status\t$body_bytes_sent\t$http_referer
‘\t$http_user_agent\t$http_x_forwarded_for
‘\t$host\t$request_time\t$upstream_addr\t$upstream_status\t$upstream_response_time;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers
4 32k;
client_max_body_size 30m;

sendfile on;
tcp_nopush on;
log_subrequest on;

keepalive_timeout 60;
tcp_nodelay on;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

lua_package_cpath ‘lib/?.so;tcp/lib/?.so;/data1/htdocs/lua_v2/lib/*/?.so;;‘;
lua_shared_dict cache 100m;
lua_code_cache on;
lua_shared_dict lyrics_monitor_cnt 1024K;

server {
listen 8081; # 監聽本機所有 ip 上的 8081 端口
server_name _; # 域名:www.example.com 這裏 "_" 代表獲取匹配所有
root /home/liujiepeng/workspace/html/etc/resource/html/; # 站點根目錄
index Home.html;
}
}

創建一個目錄,例如: /home/liujiepeng/workspace/html/etc/resource/html/ 然後在這個 html文件夾下可以放置你需要部署的靜態頁面文件,例如 html下我有 google、baidu、liujiepeng這三個文件夾,其中 server 字段配置如下:

server {
listen 80;
server_name _;
root /home/liujiepeng/workspace/html/etc/resource/html/;
index Home.html;
}

這裏每個文件夾下面的靜態頁面文件名都是 Home.html 。這樣配置的話,例如當你訪問 www.example.com/google/ 時,nginx 就會去 root指定的目錄下的 google 文件夾下尋找到 Home.html 並把 google 頁面返回,同理,訪問 www.example.com/baidu/ 時,會尋找到 baidu文件夾下的 Home.html 並把 baidu頁面返回。

而在 google、baidu、liujiepeng 文件夾的同級目錄上,再添加你的域名首頁 Home.html 時,訪問 www.example.com 時就會返回了。

這裏唯一美中不足的是,訪問域名中 www.showzeng.cn/zhihu 末尾會自動加上 / ,在瀏覽器中按 F12 調試會發現 www.showzeng.cn/zhihu 為 301 狀態碼,因為 index.html 是在 zhihu/ 文件夾下,所以在搜索過程中會重定向到 www.showzeng.cn/zhihu/

配置方式2:

這裏需要註意的是 http 上下文裏的 server 上下文。

server {
listen 8081; # 監聽本機所有 ip 上的 8081 端口
server_name _; # 域名:www.example.com 這裏 "_" 代表獲取匹配所有
root /home/filename/; # 站點根目錄

location / { # 可有多個 location 用於配置路由地址
try_files index.html =404;
}
}

這裏的 root 字段最好寫在 location 字段的外邊,防止出現無法加載 css、js 的情況。因為 css、js 的加載並不是自動的,nginx 無法執行,需要額外的配置來返回資源,所以,對於靜態頁面的部署,這樣做是最為方便的。
這裏對 root 作進一步解釋,例如在服務器上有 /home/liujiepeng/workspace/html/etc/resource/html/,其下有 index.html 文件和 css/ 以及 img/ , root /home/liujiepeng/workspace/html/etc/resource/html/ 這配置語句就將指定服務器加載資源時是在 /home/liujiepeng/workspace/html/etc/resource/html/ 下查找。
其次, location 後的匹配分多種,其各類匹配方式優先級也各不相同。這裏列舉一精確匹配例子:

server {
listen 80;
server_name _;
root /home/zhihu/;

location = /zhihu {
rewrite ^/.* / break;
try_files index.html =404;
}
}

此時,訪問 www.example.com/liujiepeng 就會加載 zhihu.html 出來了。由於 location 的精確匹配,只有訪問 www.example.com/liujiepeng 這個路由時才會正確響應,而且此時要通過 rewrite 正則匹配,把 /zhihu 解析替換成原來的 / 。關於更多 location 字段用法,可以在文章最後給出的參考資料中查看。

參考:
http://showzeng.itscoder.com/nginx/2016/10/03/use-nginx-to-deploy-static-pages-easily.html

基於nginx的靜態網頁部署