![Nginx 处理同一服务器中不同位置的多个 html](https://linux22.com/image/751774/Nginx%20%E5%A4%84%E7%90%86%E5%90%8C%E4%B8%80%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%AD%E4%B8%8D%E5%90%8C%E4%BD%8D%E7%BD%AE%E7%9A%84%E5%A4%9A%E4%B8%AA%20html.png)
我想在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 专家来评论。