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