- Nginx是一个HTTP的web服务器,可以将服务器上的静态文件通过HTTP协议返回给浏览器客户端
示例一:通过 ip地址 + / 访问静态资源
- Linux 服务器的 opt 目录下创建 static 目录
mkdir /opt/static
- 在 static 目录下创建 html 文件
# 进入 /opt/static 目录
cd /opt/static
# 创建 html 文件
vi index.html
- 编写 html 文件内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Nginx</title>
</head>
<body>
<h1>HELLO NGINX</h1>
</body>
</html>
- 修改 nginx.conf 配置文件
# 更改 nginx 安装目录下 conf 目录下的 nginx.conf 配置文件
vi /usr/local/nginx/conf/nginx.conf
# 在server中,通过location匹配访问的路径,然后转发给静态资源
location / {
# 配置转发的路径
root /opt/static;
# 配置默认打开的文件
index index.html index.htm;
}
- 启动 Nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
- 在浏览器中输入 ip 地址 进行访问
- 如果无法访问,查看是否开启 80 端口
# 开启 80 端口
firewall-cmd --add-port=80/tcp --permanent
# 重新加载防火墙配置
firewall-cmd --reload
示例二:通过 ip地址 + 目录 访问静态资源
- 修改 nginx.conf 配置文件
# 更改 nginx 安装目录下 conf 目录下的 nginx.conf 配置文件
vi /usr/local/nginx/conf/nginx.conf
# 这里接收/static请求
location /static {
# 配置转发的路径,会去 /opt 目录下找资源
root /opt;
# 配置默认打开的文件
index index.html index.htm;
}
重启 nginx 服务器
# 相同配置文件,直接重启 Nginx 服务器
/usr/local/nginx/sbin/nginx -s reload
- 在浏览器中输入 ip 地址 + /static 进行访问
版权声明:本文为LFQJava原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。