在日常项目部署的时候,常常会有多个项目公用一个域名的情况,这时就需要利用nginx在域名下配置多个子路径。
配置代码如下:
server {
listen 80;
server_name baidu.com; // 自己域名
index index.php index.html index.htm;
root /xxx/xxx/xxx; // 根路径
location /test1 { // 配置子路径1
alias /demo-project-test1/dist; // 项目路径
#try_files $uri $uri/ /index.html;
index index.html;
}
location /test2 { // 配置子路径2
alias /demo-project-test2/public; // 项目路径
index index.php index.html index.htm;
}
location / {
# try_files '' ../demo-project-test/public/index.html;
}
}
检测配置
根据上面的代码对应的修改自己的nginx配置后, 找到存放nginx的那层路径,先检测一下刚刚的配置是否正确:./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
重新启动
重新启动:./nginx -s reload,输入此命令后如果没有任何提示,则代表重启成功,到浏览器上面刷新页面就可以了。