- Nginx是一个HTTP的web服务器,可以将服务器上的静态文件通过HTTP协议返回给浏览器客户端
示例一:通过 ip地址 + / 访问静态资源
- Linux 服务器的 opt 目录下创建 static 目录
mkdir /opt/static
# 进入 /opt/static 目录
cd /opt/static
# 创建 html 文件
vi index.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 配置文件
vi /usr/local/nginx/conf/nginx.conf
# 在server中,通过location匹配访问的路径,然后转发给静态资源
location / {
# 配置转发的路径
root /opt/static;
# 配置默认打开的文件
index index.html index.htm;
}
![image.png](https://i-blog.csdnimg.cn/blog_migrate/f5a294ec5c035792b3ebf946675a62f4.png)
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
![image.png](https://i-blog.csdnimg.cn/blog_migrate/f34f29f212b00ff52a72ac26552068ee.png)
# 开启 80 端口
firewall-cmd --add-port=80/tcp --permanent
# 重新加载防火墙配置
firewall-cmd --reload
示例二:通过 ip地址 + 目录 访问静态资源
# 更改 nginx 安装目录下 conf 目录下的 nginx.conf 配置文件
vi /usr/local/nginx/conf/nginx.conf
# 这里接收/static请求
location /static {
# 配置转发的路径,会去 /opt 目录下找资源
root /opt;
# 配置默认打开的文件
index index.html index.htm;
}
# 相同配置文件,直接重启 Nginx 服务器
/usr/local/nginx/sbin/nginx -s reload
- 在浏览器中输入 ip 地址 + /static 进行访问
![image.png](https://i-blog.csdnimg.cn/blog_migrate/62a8ceb4f52217385689bfc6cbe4ed1d.png)