【发布时间】:2019-10-29 00:45:19
【问题描述】:
我正在尝试在同一台服务器上设置多个 React 应用程序。问题是我构建项目后,找到了build/中的index.html,但没有找到build/static中的辅助文件。最初,只有一个应用程序,我有 location static/ 和别名。但是,对于多个项目和多个 static/ 目录,我不能这样做。基本上我希望每个应用程序都有自己的静态文件夹。我该如何解决我的问题?
在浏览器中,错误如下所示:
GET http://my.servername/static/css/2.266e55a5.chunk.css net::ERR_ABORTED 404 (Not Found)
我目前的设置是这样的:
server {
listen 80;
server_name my.servername;
root /data/adpop/;
location /possible-malware-domains-viewer/ {
alias /data/adpop/possible-malware-domains-viewer/build/;
try_files $uri /possible-malware-domains-viewer/index.html;
add_header Access-Control-Allow-Origin *;
autoindex on;
# Simple requests
if ($request_method ~* "(GET|POST)") {
add_header "Access-Control-Allow-Origin" *;
}
# Preflighted requests
if ($request_method = OPTIONS ) {
add_header "Access-Control-Allow-Origin" *;
add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
return 200;
}
}
location /punycode-domains-viewer/ {
alias /data/adpop/punycode-domains-viewer/build/;
try_files $uri /punycode-domains-viewer/index.html;
[...same settings as above...]
}
}
}
我尝试结合来自here、here 或here 的答案,如果看起来很乱或者我有重大错误,请见谅。如果我想要实现的目标不太好,请提出其他建议。谢谢!
【问题讨论】:
-
所以应该在多个目录下搜索以
/static/开头的URI,例如:/data/foo/build/static/和/data/bar/build/static/? -
@RichardSmith 没错。每个应用都应该知道自己的静态文件夹。
-
@RichardSmith 例如,
/punycode-domains-viewer/应用程序应在data/adpop/punycode-domains-viewer/build/static/中搜索其静态文件等。我不想真正混合多个应用的静态文件。
标签: reactjs nginx deployment nginx-location