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

来源:互联网 发布:国外数据分析公司 编辑:程序博客网 时间:2024/05/22 00:38

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

这时候 要访问第一个网页 就是 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/目录下找文件。
阅读全文
0 0