专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Nginx同一个server部署多个静态资源目录

一般情况,有时候业务需求,需要在一个server下面不同目录部署两个不同的项目。

比如 http://domain:port/admin 匹配的是 admin 项目

比如 http://domain:port/react 匹配的是 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中的路径匹配问题,rootalias 的区别;

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

文章永久链接:https://tech.souyunku.com/34690

未经允许不得转载:搜云库技术团队 » Nginx同一个server部署多个静态资源目录

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们