我希望将子域的根URL和子域的目录提供给我服务器上的两个不同文件夹.这是我拥有的简单设置,并且不起作用......
server {
index index.html index.htm;
server_name test.example.com;
location / {
root /web/test.example.com/www;
}
location /static {
root /web/test.example.com/static;
}
}
Run Code Online (Sandbox Code Playgroud)
在这个例子test.example.com/中将带入索引文件/web/test.example.com/www
并将去test.example.com/static带上索引文件/web/test.example.com/static
fur*_*urq 218
您需要使用该alias指令location /static:
server {
index index.html;
server_name test.example.com;
root /web/test.example.com/www;
location /static/ {
alias /web/test.example.com/static/;
}
}
Run Code Online (Sandbox Code Playgroud)
在nginx的维基解释比我的根,别名更好的区别:
请注意,它看起来可能与root指令类似,但文档根目录不会更改,只是用于请求的文件系统路径.在请求Nginx问题中删除请求的位置部分.
小智 97
Location指令系统是
就像你想转发所有开始的请求/static和你的数据/var/www/static
所以一个简单的方法是将最后一个文件夹与完整路径分开,这意味着
完整路径 : /var/www/static
最后路径:/static 和第一条路径:/var/www
location <lastPath> {
root <FirstPath>;
}
Run Code Online (Sandbox Code Playgroud)
那么让我们看看你做错了什么,你的解决方案是什么
你的错误:
location /static {
root /web/test.example.com/static;
}
Run Code Online (Sandbox Code Playgroud)
您的解决方案
location /static {
root /web/test.example.com;
}
Run Code Online (Sandbox Code Playgroud)
VBa*_*art 46
server {
index index.html index.htm;
server_name test.example.com;
location / {
root /web/test.example.com/www;
}
location /static {
root /web/test.example.com;
}
}
Run Code Online (Sandbox Code Playgroud)
erm*_*off 13
更详细一点的例子。
设置:您有一个网站,example.com并且您有一个网络应用程序example.com/webapp
...
server {
listen 443 ssl;
server_name example.com;
root /usr/share/nginx/html/website_dir;
index index.html index.htm;
try_files $uri $uri/ /index.html;
location /webapp/ {
alias /usr/share/nginx/html/webapp_dir/;
index index.html index.htm;
try_files $uri $uri/ /webapp/index.html;
}
}
...
Run Code Online (Sandbox Code Playgroud)
我是故意命名webapp_dir的website_dir。如果您有匹配的名称和文件夹,则可以使用该root指令。
此设置有效并通过 Docker 进行了测试。
注意!小心斜线。完全按照示例中的方式放置它们。
| 归档时间: |
|
| 查看次数: |
251585 次 |
| 最近记录: |