提供RequireJS主优化文件作为NGINX的静态资产



我听说你最好从NGINX或其他服务器上提供静态资产,而不是你的Node.js应用服务器。

我有一个单页应用程序,在生产模式下,只从索引页提供一个优化的.js文件,从那里开始,服务器只向客户端提供JSON API。

所以从我收集的,这将是一个好主意,从一个静态资产服务器提供一个。js文件。

这是第一个也是唯一的HTML文件,当客户端点击我的应用程序的URL:

<!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优化。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;
    }
 }

最新更新