Nginx развертывает несколько каталогов статических ресурсов на одном сервере.

Nginx

В общем, иногда бизнес-потребности требуютserverДва разных проекта развернуты в разных каталогах ниже.

Напримерhttp://domain:port/admin соответствуетadminпроект

Напримерhttp://domain:port/реагировать соответствуетreactпроект

мы вnginx.confнаписать новыйserver;

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

[root]# nginx -s stop

перезагружатьnginx

[root]# nginx -c /etc/nginx/nginx.conf

Суммировать:

Здесь следует отметить, чтоlocationПроблема сопоставления путей в ,rootиaliasразница;

# 错误写法,会报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:

In case of the root directive, full path is appended to the root including the location part, where as in case of the alias directive, only the portion of the path NOT including the location part is appended to the alias.

Let's say we have the config

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