Nginx學習之從零搭建靜態資源網站

前言

  在某學習網站學習了nginx的安裝和使用,以此文記錄。javascript

環境準備

  安裝在VMWare下的Centos虛擬機。因爲我這是新裝的虛擬機。因此不少插件都沒有,這裏乾脆一次性安裝上。php

  • wget command not foundcss

    yum -y install wget

  • c compiler cc is not foundhtml

    yum -y install gcc-c++

  • the HTTP rewrite module requires the PCRE libraryjava

    yum -y install pcre-devel openssl openssl-devel

編譯安裝nginx

  首先打開nginx官網複製下載連接。我下載的是nginx-1.14.1版本的。
nginx

  下載包到服務器。c++

wget http://nginx.org/download/nginx-1.14.1.tar.gz

  解壓包。git

tar -xzf nginx-1.14.1.tar.gz

  編譯代碼。prefix指定編譯生成的文件存放的地址github

cd nginx-1.14.1
./configure --prefix=/home/panzi/nginx

  編譯成功以後大概是這個樣子的

  繼續執行瀏覽器

make
make install

配置靜態資源網站

  這裏我以layui的fly模板爲例,下載完以後,將文件放到nginx的目錄下。

而後修改配置文件conf/nginx.conf:

server {
        listen     80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           #將html地址指定爲 fly 目錄
           alias fly/;
           # root   html;
           # index  index.html index.htm;
        }

  修改完成以後,保存,從新加載配置文件。

./sbin/nginx -s reload

  訪問瀏覽器:http://192.168.220.129/html/jie/index.html,一個靜態資源網站就搭建成功啦。

  最後記得配置一下gzip壓縮哦。

gzip  on;
    gzip_min_length 1;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

  若是主機訪問不了虛擬機的端口,打開便可:

firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload

nginx 的幾個啓動命令

./sbin/nginx -s stop //當即中止
./sbin/nginx -s quit //優雅中止,處理完全部請求自動關閉
./sbin/nginx -s reload //重載config
./sbin/nginx -s reopen //從新打開日誌(日誌文件切換)

總結

  一篇很簡單的nginx學習流水帳,中間遇到了一些小問題,好比防火牆限制了80端口,編譯過程當中的問題等等。不過都是些小問題。但願本文能幫助到初步學習nginx的同窗。

附錄

相關文章
相關標籤/搜索