nginx 中配置多个location并解决js/css/jpg/等的加载问题

时间:2023-03-09 19:31:23
nginx 中配置多个location并解决js/css/jpg/等的加载问题
2017-11-09 22:07 277人阅读 评论(0) 收藏 举报
nginx 中配置多个location并解决js/css/jpg/等的加载问题 分类:
linux(1) nginx 中配置多个location并解决js/css/jpg/等的加载问题

版权声明:如有版权问题,请私信我。

ECS:阿里云 
系统:ubuntu 16.04 
我的配置文件位置: 
/etc/nginx/conf.d/**.conf 
我的静态网页的位置: 
/var/www/** 
html中加载的js之类的文件夹和index.html在一个文件夹中

配置文件内容:

server {
listen 80;
server_name #你的网站IP或****.com;
location /www1
{
alias /var/www/****;
index index.html index.php index.htm;
}
location /www2
{
alias /var/www/****;
index index.html index.php index.htm;
}
location ~ .*\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt)$
{
root /var/www/;
proxy_temp_path /var/www/;
}

配置好以后 重启nginx

sudo nginx -s reload
  • 1

这时候 要访问第一个网页 就是 IP/www1 
要访问第二的网页就是 IP/www2 
关于alias和root的区别: 
root和alias是系统文件路径的设置。 
root用来设置根目录,而alias用来重置当前文件的目录。

location /img/ {
alias /var/www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
location /img/ {
root /var/www/image;
}
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。