美文网首页
搭建Nginx静态网站

搭建Nginx静态网站

作者: 林清猫耳 | 来源:发表于2018-11-28 02:11 被阅读17次

软件环境
CentOS 7.4

参考文档
腾讯云开发者实验室:https://cloud.tencent.com/developer/labs

搭建Http静态服务器环境

搭建静态网站,首先需要部署环境。下面的步骤,将告诉大家如何在服务器上通过 Nginx 部署 HTTP 静态服务。

安装Nginx

在 CentOS 上,可直接使用 yum 来安装 Nginx:
yum install -y nginx
安装完成后,使用 nginx 命令启动 Nginx:
nginx
设置 Nginx 开机启动:
systemctl enable nginx.service

配置静态服务器访问路径

外网用户访问服务器的 Web 服务由 Nginx 提供,Nginx 需要配置静态资源的路径信息才能通过 url 正确访问到服务器上的静态资源。
打开 Nginx 的默认配置文件 /etc/nginx/nginx.conf ,修改 Nginx 配置,将默认的 root/usr/share/nginx/html;; 修改为: root/data/www;,如下:

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 {
        }
    }

}

配置文件将 /data/www/static 作为所有静态资源请求的根路径,如访问: http://你的ip地址/static/index.js,将会去 /data/www/static/ 目录下去查找 index.js。现在我们需要重启 Nginx 让新的配置生效,如:
nginx -s reload
重启后,现在我们应该已经可以使用我们的静态服务器了,现在让我们新建一个静态文件,查看服务是否运行正常。
首先让我们在 /data 目录 下创建 www 目录,如:
mkdir -p /data/www

创建第一个静态文件

/data/www 目录下创建我们的第一个静态文件 index.html

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>第一个静态文件</title>
</head>
<body>
Hello world!
</body>
</html>

现在访问 http://你的ip地址/index.html 应该可以看到页面输出 Hello world!
到此,一个基于 Nginx 的静态服务器就搭建完成了,现在所有放在 /data/www 目录下的的静态资源都可以直接通过域名访问。

敲黑板!!!
用阿里云服务器的同学们要注意了,我用阿里云的服务器忙活了一下午重装了系统盘好几次还是不能正常访问域名,最后知道真相的我眼泪掉下来了。
解决方案:https://blog.csdn.net/LJFPHP/article/details/78670459

image

相关文章

  • 搭建 Nginx 静态网站

    环境说明 操作系统: CentOS 7.2 64位 1. 准备NGINX静态服务器环境 搭建静态网站,首先需要...

  • 搭建Nginx静态网站

    软件环境:CentOS 7.4 参考文档:腾讯云开发者实验室:https://cloud.tencent.com/...

  • 搭建 Nginx 静态网站

    搭建静态网站,首先需要部署环境。下面的步骤,将告诉大家如何在服务器上通过 Nginx 部署 HTTP 静态服务。 ...

  • CentOS 搭建Http静态服务器环境

    搭建Http静态服务器环境 搭建静态网站,首先需要部署环境。下面的步骤,将告诉大家如何在服务器上通过 Nginx ...

  • 在CentOS上搭建Nginx

    搭建Http静态服务器环境 搭建静态网站,首先需要部署环 境。下面的步骤,将告诉大家如何在服务器上通过 Nginx...

  • 使用Nginx搭建静态网站

    开始建站了,暂时还没想要做些什么东西。 Anyway,先搞个云服务器吧,那要怎么搭建呢?先来个最简单的。 以阿里云...

  • Linux yum 安装 Nginx

    搭建 Nginx 静态服务器 安装 Nginx 使用 yum 安装 Nginx: 修改 /etc/nginx/co...

  • Nginx静态服务

    搭建一个Nginx静态资源Web服务器

  • 树莓派安装LNMP的好文

    树莓派搭建nginx服务器+mysql+php7.0,设置静态网页 教程:树莓派LNMP开Web服务器搭网站,可外网访问

  • nginx服务器常见配置

    nginx是一个优秀的静态服务器,本博客就搭建在nginx服务器中。本博客由Hexo生成静态文件,上传至nginx...

网友评论

      本文标题:搭建Nginx静态网站

      本文链接:https://www.haomeiwen.com/subject/mrpoqqtx.html