使用nginx为静态资源提供服务

1、上传静态资源到服务器。任意位置,我这里传到 /home 目录中

2、配置nginx.conf配置文件,这里推荐导入文件的形式,当然也可以直接写在主配置文件中

(1)加入如下配置

(2)在nginx.conf同目录下。创建上述自定义配置文件。直接vim xpf.conf即可

(3)配置步骤1中上传的静态资源的映射,写入如下内容

server {
        listen       89;
        server_name  localhost;


        location / {
            root   html;
            index  xpf.html index.htm;
        }
}

server {
        listen       90;
        server_name  localhost;


        location / {
            root   /home/foodie-shop;
            index  index.html;
        }

        location /xpfStatic{
            root /home;
        }

        #此和上述映射一样。这里使用了alias,使得可以使用别名,而隐藏实际路径。如这里的别名static
        location /static{
            alias /home/xpfStatic;
        }
}

只要看第二个server就行了,里面三个location,

第一个location配置了上传的 foodie-shop资源;

第二个location和第三个location作用一样,配置了上传的xpfStatic资源。只是第三个location使用了alias从而可以使用别名,使得可以隐藏资源的实际路径。

 3、重写了配置,检查nginx。使用 ./nginx -t

没问题,重启nginx。 使用  ./nginx -s reload

4、本地访问(这里使用windows访问虚拟机里的nginx,若是本地的nginx,改成127.0.0.1)

①第一个location访问地址:http://192.168.217.128:90/

②第二个loaction访问地址:http://192.168.217.128:90/xpfStatic/img/1001.gif

③第三个location访问地址:http://192.168.217.128:90/static/img/1001.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值