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的同窗。

附录

相关文章
相关标签/搜索