首页
学习
活动
专区
圈层
工具
发布

09-基于Nginx发布静态资源

使用Nginx发布静态资源

找一些静态资源, 上传拿到服务器, 我就直接拿老师的了

上传完成

配置nginx.conf

进入nginx/conf文件夹, 创建独立配置文件

代码语言:javascript
复制
vi staticfile.conf

编写路由映射

代码语言:javascript
复制
server {
  listen       90;
  server_name  localhost;
  location / {
    root   /home;
  }
}

在配置里面导入自己写的配置

代码语言:javascript
复制
[root@localhost conf]# cd ../
[root@localhost nginx]# cd sbin/
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sbin]# ./nginx -s reload
[root@localhost sbin]#

检查配置文件并重启

直接在根路径后添加资源路径就可以访问了

但是这样做存在一个问题, 那就是会对外暴露真实的磁盘资源

修改配置文件

代码语言:javascript
复制
server {
  listen       90;
  server_name  localhost;
  location / {
    root   /home;
  }
    # 添加新的别名映射, 为了和之前的对比, 就没有删除上面的
  location /static {
    alias   /home;
  }
}

检查重启

也是可以访问的, 这样如果不是/home,而是有很长的路基都可以写在别名里面, 这样就不会对外暴露真实路径了

下一篇
举报
领券