我想在nginx
同一台服务器下部署我的 React 应用程序,为不同的位置提供不同的 HTML,我尝试了以下配置,但它对我来说不起作用
server {
listen 8080;
# root /usr/share/nginx/build-bms;
# Add index.php to the list if you are using PHP
server_name _;
location ^~ /portal1/ {
root PATH_TO_BUILD_FOLDER_ONE;
try_files $uri /index.html;
index invalid;
add_header Cache-Control public;
expires 1d;
autoindex on;
}
location ^~ /portal2/ {
root PATH_TO_BUILD_FOLDER_TWO;
try_files $uri /index.html;
add_header Cache-Control public;
index invalid;
expires 1d;
break;
autoindex on;
}
location / {
root PATH_TO_BUILD_FOLDER_THREE;
try_files $uri /index.html;
add_header Cache-Control public;
expires 1d;
}
}
每次只供应最后一个
答案1
我不是 nginx 专家,所以我检查了我的配置,并建议删除正则表达式匹配(如果不需要的话)并将您的配置转换为:
server {
listen 8080;
root /usr/share/nginx/build-bms;
# Add index.php to the list if you are using PHP
server_name _;
location ^~ /portal1/ {
alias /usr/share/nginx/portal1/;
try_files $uri /index.html index.php;
index invalid;
add_header Cache-Control public;
expires 1d;
autoindex on;
}
location ^~ /portal2/ {
alias /usr/share/nginx/portal2/;
try_files $uri /index.html index.php;
add_header Cache-Control public;
index invalid;
expires 1d;
break;
autoindex on;
}
location / {
alias /usr/share/nginx/portal3/;
try_files $uri /index.html index.php;
add_header Cache-Control public;
expires 1d;
}
}
我不会使用多个根,而是使用别名。但我总是会为服务器指定一个根元素,然后使用别名。我会把这个问题留给 nginx 专家来评论。