通常狀況,有時候業務需求,須要在一個
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
複製代碼