我听说您最好从 NGINX 或其他服务器而不是 Node.js 应用程序服务器提供静态 Assets 。
我有一个单页应用程序,在生产模式下,仅从索引页提供一个优化的 .js 文件,从那里开始,服务器仅向客户端提供 JSON API。
因此,根据我的收集,从静态 Assets 服务器提供一个 .js 文件是一个好主意。
这是当客户端点击我的应用程序的 URL 时我提供的第一个也是唯一一个 HTML 文件:
<!DOCTYPE html>
<html>
<head>
<!-- stylesheets-->
<link href="/static/css/bootstrap/bootstrap-notify.css" rel="stylesheet">
</head>
<body>
<main>
<div name="main-div" id="main-div-id"></div>
</main>
<% if(env === 'development') {%>
<script data-main="/static/app/js/main" src="/static/vendor/require.js"></script>
<% } else { %>
<script src="/static/app/optimized/optimized.js"></script>
<% } %>
</body>
</html>
所以我的问题是,如何配置这个标准应用程序以需要来自 NGINX 的optimized.js 文件?就像输入 NGINX 服务器 Assets 的 URL 一样简单吗?有人有这方面的好例子吗?
最佳答案
如果您的 NGINX 服务器与您的应用程序运行在同一台服务器上,则无需更改 url,您可以在/etc/nginx/conf.d/yoursitename.conf 中使用此配置来告诉 NGINX 直接从文件夹查找静态文件,而不是通过 Nodejs 应用程序路由器。
upstream someservice.somedomain.com {
# port where you have the app runing on the machine
server 127.0.0.1:3000;
}
server {
listen 80;
server_name someservice.somedomain.com someservice.somedomain;
client_max_body_size 10M;
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm|ps|woff|svg)$ {
expires modified +7d;
# static files path
root /home/projectfolder/public/;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://someservice.somedomain.com/;
proxy_redirect off;
}
}
关于javascript - 将 RequireJS 主要优化文件作为 NGINX 的静态 Assets 提供,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31950499/