通常状况,有时候业务需求,须要在一个
server
下面不一样目录部署两个不一样的项目。html好比 http://domain:port/admin 匹配的是
admin
项目react好比 http://domain:port/react 匹配的是
react
项目nginx
咱们在nginx.conf
里面写一个新的server
;shell
server { listen 6002; server_name **.**.**.**; gzip on; location /admin { alias /projects/admin/; #指定主页 index index.html; #自动跳转 autoindex on; } location /react { alias /projects/react/; #指定主页 index index.html; #自动跳转 autoindex on; } } 复制代码
而后关闭 nginx
bash
[root]# nginx -s stop 复制代码
重启 nginx
markdown
[root]# nginx -c /etc/nginx/nginx.conf 复制代码
这里须要注意的就是location
中的路径匹配问题,root
和 alias
的区别;app
# 错误写法,会报404 location /admin { root /projects/admin/; #指定主页 index index.html; #自动跳转 autoindex on; } location /react { root /projects/react/; #指定主页 index index.html; #自动跳转 autoindex on; } # 当你访问 http://***/admin # root ==> 实际指向的是 http://***/admin/projects/admin/, 这个时候确定找不到index.html # alias ==> 实际指向的是 http://***/admin, 能够在/projects/admin/找到index.html 复制代码
document:dom
In case of the
root
directive, full path is appended to the root including the location part, where as in case of thealias
directive, only the portion of the path NOT including the location part is appended to the alias.oop
Let's say we have the configthis
location /static/ {
root /var/www/app/static/;
autoindex off;
}
复制代码
In this case the final path that Nginx will derive will be
/var/www/app/static/static
复制代码
This is going to return 404
since there is no static/
within static/
This is because the location part is appended to the path specified in the root
. Hence, with root
, the correct way is
location /static/ {
root /var/www/app/;
autoindex off;
}
复制代码
On the other hand, with alias
, the location part gets dropped. So for the config
location /static/ { alias /var/www/app/static/; autoindex off; } 复制代码
the final path will correctly be formed as
/var/www/app/static
复制代码