英文:
NGINX switch root by path param and run for angular project
问题
我想要通过第一个URL路径参数来更改根目录。
示例URL为:https://example-domain.com/uuid_1/product/list?mode=card
预期行为是将根目录切换到uuid_1
的路径参数,并且其余路径(/product/list?mode=card
)应该在Angular项目中运行。
我需要用于此情况的nginx配置。我尝试了一些配置但失败了。
成功场景
示例 1:
URL: https://example-domain.com/uuid_1
然后:
根目录 => /main_folder/uuid_1/angular/dist
为Angular运行 => /
示例 2:
URL: https://example-domain.com/uuid_2/product/list?mode=card
然后:
根目录 => /main_folder/uuid_2/angular/dist
为Angular运行 => /product/list?mode=card
示例 3:
URL: https://example-domain.com/uuid_3/product/edit?id=abc
然后:
根目录 => /main_folder/uuid_3/angular/dist
为Angular运行 => /product/edit?id=abc
服务器文件夹层次结构
- main_folder
- uuid_1
- angular_project_dist
- index.html
- ...(Angular项目的其余文件)
- uuid_2
- angular_project_dist
- index.html
- ...(Angular项目的其余文件)
- uuid_3
- angular_project_dist
- index.html
- ...(Angular项目的其余文件)
- uuid_4
- angular_project_dist
- index.html
- ...(Angular项目的其余文件)
失败的NGINX配置:
server {
server_name example-domain.com;
index index.html index.htm;
# 阻止访问 .htaccess 文件,如果 Apache 的文档根目录
location ~ /\.ht {
deny all;
}
location / {
if ($uri ~* /([^/]+)/(.*)){
set $project_id $1;
set $rest_of_uri /$2;
}
root /var/www/example-domain.com/$project_id/angular/dist/;
try_files $rest_of_uri $rest_of_uri/ /index.html;
}
}
请检查上述Nginx配置是否符合您的要求。
英文:
I want to change the root directory by first URL path param.
The example URL is: https://example-domain.com/uuid_1/product/list?mode=card
Expected behaviour is that the root should be switched to path param of uuid_1
. And rest path (/product/list?mode=card
) should be run for the angular project.
I need the nginx config for this situation. I tried some config but failed.
Success Scenerios
Example 1:
URL: https://example-domain.com/uuid_1
THEN:
root => /main_folder/uuid_1/angular/dist
run for angular => /
Example 2:
URL: https://example-domain.com/uuid_2/product/list?mode=card
THEN:
root => /main_folder/uuid_2/angular/dist
run for angular => /product/list?mode=card
Example 3:
URL: https://example-domain.com/uuid_3/product/edit?id=abc
THEN:
root => /main_folder/uuid_3/angular/dist
run for angular => /product/edit?id=abc
Server Folder Hierarchy
- main_folder
- uuid_1
- angular_project_dist
- index.html
- ...(rest_of_files_of_angular_project)
- uuid_2
- angular_project_dist
- index.html
- ...(rest_of_files_of_angular_project)
- uuid_3
- angular_project_dist
- index.html
- ...(rest_of_files_of_angular_project)
- uuid_4
- angular_project_dist
- index.html
- ...(rest_of_files_of_angular_project)
Failed NGINX config:
server {
server_name example-domain.com;
index index.html index.htm;
# deny access to .htaccess files, if Apache's document root
location ~ /\.ht {
deny all;
}
location / {
if ($uri ~* /([^/]+)/(.*)){
set $project_id $1;
set $rest_of_uri /$2;
}
root /var/www/example-domain.com/$project_id/angular/dist/;
try_files $rest_of_uri $rest_of_uri/ /index.html;
}
}
答案1
得分: 1
Here is the translation:
"在if...set
块之外,你可以使用具有命名捕获的正则表达式位置来创建变量。
例如:
location ~ ^/(?<project_id>[^/]+)(?<rest_of_uri>/.*)$ {
root /var/www/example-domain.com/$project_id/angular/dist/;
try_files $rest_of_uri $rest_of_uri/ /index.html =404;
}
如果你想在这个位置内查找index.html
,你需要在try_files
语句的末尾添加=404
,否则Nginx会将其视为重定向并搜索另一个位置来处理请求。请参阅try_files
文档。"
英文:
Instead of the if...set
block, you could use a regular expression location with named captures to create the variables.
For example:
location ~ ^/(?<project_id>[^/]+)(?<rest_of_uri>/.*)$ {
root /var/www/example-domain.com/$project_id/angular/dist/;
try_files $rest_of_uri $rest_of_uri/ /index.html =404;
}
If you want to find index.html
within this location you will need to add =404
to the end of the try_files
statement, otherwise Nginx will treat it as a redirect and search for another location to process the request. See try_files
documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论