将 RequireJS 主要优化文件作为来自 NGINX 的静态资产提供
serving RequireJS main optimized file as a static asset from NGINX
我听说您最好从 NGINX 或其他服务器而不是您的 Node.js 应用程序服务器提供静态资产。
我有一个单页应用程序,在生产模式下,它只从索引页面提供一个优化的 .js 文件,从那里开始,服务器只提供 JSON API给客户。
据我所知,从静态资产服务器提供一个 .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服务器资产的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;
}
}
我听说您最好从 NGINX 或其他服务器而不是您的 Node.js 应用程序服务器提供静态资产。
我有一个单页应用程序,在生产模式下,它只从索引页面提供一个优化的 .js 文件,从那里开始,服务器只提供 JSON API给客户。
据我所知,从静态资产服务器提供一个 .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服务器资产的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;
}
}