用免费腾讯云服务器体验搭建 Nginx 静态网站。
更新日期:2021-08-12 08:02  雨后彩虹_旺远  点击:
用免费腾讯云服务器体验搭建 Nginx 静态网站

本实验是基础入门教程,域名专业人士表示带您从零开始基于 Nginx 搭建一个静态网站服务。过程会涉及到 Nginx 的安装、配置和运行。

首次可**云主机 45 分钟 ,到期后云主机将被重置并退库,若想保留成果请及时留用。

实验资源

免费腾讯云服务器

软件环境
CentOS 7.2 64 位

搭建Http静态服务器环境
任务时间:15min ~ 30min

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

安装 Nginx
在 CentOS 上,可直接使用 yum 来安装 Nginx

yum install nginx -y
安装完成后,使用 nginx 命令启动 Nginx:

nginx
此时,访问 http://<您的域名> 可以看到 Nginx 的测试页面 [?]

如果无法访问,请重试用 nginx -s reload 命令重启 Nginx

配置静态服务器访问路径
外网用户访问服务器的 Web 服务由 Nginx 提供,Nginx 需要配置静态资源的路径信息才能通过 url 正确访问到服务器上的静态资源。

打开 Nginx 的默认配置文件 /etc/nginx/nginx.conf ,修改 Nginx 配置,将默认的 root /usr/share/nginx/html; 修改为: root /data/www;,如下:

示例代码:/etc/nginx/nginx.conf
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://<您的域名>/static/index.js,将会去 /data/www/static/ 目录下去查找 index.js。现在我们需要重启 Nginx 让新的配置生效,如:

nginx -s reload
重启后,现在我们应该已经可以使用我们的静态服务器了,现在让我们新建一个静态文件,查看服务是否运行正常。

首先让我们在 /data 目录 下创建 www 目录,如:

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

示例代码:/data/www/index.html
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>第一个静态文件</title>
</head>
<body>
Hello world!
</body>
</html>
现在访问 http://<您的域名>/index.html 应该可以看到页面输出 [Hello world!]

到此,一个基于 Nginx 的静态服务器就搭建完成了,现在所有放在 /data/www 目录下的的静态资源都可以直接通过域名访问。

如果无显示,请刷新浏览器页面

完成实验

恭喜!您已经成功完成了 搭建Http静态服务器环境 的实验任务。
  • 云数据库-腾讯云数据库。

    云数据库-腾讯云数据库 域名 专业人士表示腾讯云数据库主要分为:云数据库MySQL,云数据库Redis,云数据库MongoDB以及SQL Server 腾讯云数据库地址 https://cloud.tencent.com/act/cps/redirect?redirect=1014cps_key=9e19e1536ac69d202d7e62b72e932a91from=console 页...

  •  用免费腾讯云服务器体验搭建 Nginx 静态网站。
  • 用免费腾讯云服务器体验搭建 Nginx 静态网站。

    用免费腾讯云服务器体验搭建 Nginx 静态网站 本实验是基础入门教程, 域名 专业人士表示带您从零开始基于 Nginx 搭建一个静态网站服务。过程会涉及到 Nginx 的安装、配置和运行。 首次可**云主机 45 分钟 ,到期后云主机将被重置并退库,若想保留成果请及时留用。 实验...

  • 二级域名配置步骤|

    二级域名配置步骤 这篇文章主要介绍在配置 域名 时候的步骤,主要涉及到nginx、dns的配置,有相同需求的同学可以参考一下。 DNS原理 先简单的说下dns的原理,当你在域名注册商那注册了一个新的域名后,你需要设置你的dns服务器(如dnspod这样)或者不设置也行,本身域...