简体   繁体   English

为什么我们需要在Node.js表达web框架下使用apache?

[英]Why do we need apache under Node.js express web framework?

I am going to deploy my node.js application. 我将部署我的node.js应用程序。

I can start my own web application with purely node.js only. 我只能使用纯粹的node.js启动自己的Web应用程序。

But my fd told me that it is better to serve that web app under apache or nginx. 但我的fd告诉我,最好在apache或nginx下提供这个web应用程序。

Anyone have such kind of experience, why do we need apache as I can start my web app on node.js+ express only? 任何人都有这样的经验,为什么我们需要apache,因为我可以在node.js + express上启动我的网络应用程序?

I would like to know more on deployment knowledges. 我想了解有关部署知识的更多信息。 Thanks for any help. 谢谢你的帮助。

Kit 套件

Putting Apache in front of Node is not typical in a greenfield app. 将Apache置于Node之前在greenfield应用程序中并不常见。 The only case I can grant to this is if your company has an existing investment in an Apache based infrastructure (monitoring/security/routing/caching etc..) on the frontend and the sysadmin insist on this setup. 我可以批准的唯一情况是,如果您的公司在前端对基于Apache的基础架构(监控/安全/路由/缓存等等)进行了现有投资,并且系统管理员坚持这种设置。

Some folks prefer to put nginx or haproxy in front to manage routing rules, so they can channel requests for static files (assets) away from Node.js (node.js wasn't always performant when handling static files), or do fancy load balancing or failover. 有些人喜欢在前面放置nginx或haproxy来管理路由规则,因此他们可以将静态文件(资产)的请求从Node.js传递出来(在处理静态文件时,node.js并不总是高效),或者花哨的负载平衡或故障转移。 In the early days of 0.2.x etc... even Ryan Dahl advocated running something in front of Node.js for security mainly; 在0.2.x等早期......甚至Ryan Dahl主张在Node.js面前运行一些主要用于安全的东西; although, I don't think any significant issues were discovered. 虽然,我认为没有发现任何重大问题。 I personally run nginx in front of Node.js as we have several sites and services that hit the frontend shared IP which we proxy back to various node instances listening on different internal ports. 我个人在Node.js面前运行nginx,因为我们有几个站点和服务点击了前端共享IP,我们代理回到不同内部端口上监听的各个节点实例。

Nginx is better suited than Apache as it is light and single threaded vs Apache thread per request (in most normal setups). Nginx比Apache更适合,因为它是轻量级和单线程的,而不是每个请求的Apache线程(在大多数常规设置中)。 But nowadays there's even a reliable (node-http-proxy excluded) frontend Node.JS based proxy http://www.github.com/substack/bouncy which one of the celebrity node.js developers uses/will-use to frontend his PaaS. 但是现在甚至还有一个可靠的(node-http-proxy排除)基于Node.JS的前端代理http://www.github.com/substack/bouncy ,其中一个名人node.js开发人员使用/将用于前端PaaS的。

First of all, yes use nginx not Apache - it's far easier to configure nginx, and it's lighter and more efficient. 首先,是的,使用nginx而不是Apache - 配置nginx要容易得多,而且它更轻,更高效。

With an nginx proxy up front you get several advantages: 预先使用nginx代理,您将获得以下几个优势:

  • Ability to run several backends for different parts of your site 能够为您网站的不同部分运行多个后端
  • A faster static file server 更快的静态文件服务器
  • Clean restarts if you need to take down your Node server (nginx can deliver a pretty "under maintenance" page) 如果你需要关闭你的节点服务器,清理重启(nginx可以提供漂亮的“维护不足”页面)
  • Logging in your node app can focus on debugging, and nginx can log requests - keeps it nice and clean 登录您的节点应用程序可以专注于调试,nginx可以记录请求 - 保持它的美观和干净

And probably other things I've missed. 可能还有其他我错过的东西。

Once really nice thing nginx can do is the "try_files" directive, which will look for local files first, and if it doesn't find them it passes off to the Node backend. nginx可以做的一件非常好的事情是“try_files”指令,它将首先查找本地文件,如果找不到它们,它将传递给Node后端。

You don't want to use Apache because Nginx is better suited, since Nginx is built for async I/O. 你不想使用Apache,因为Nginx更适合,因为Nginx是为异步I / O而构建的。 You want to use Nginx as a forward proxy service that will point your clients to the actual node.js web servers. 您希望将Nginx用作转发代理服务,将客户端指向实际的node.js Web服务器。 This allows for horizontal scaling as your application grows to deal with increased load. 这允许在应用程序增长时进行水平扩展以处理增加的负载。 So if you outgrow your first Nginx server, you can install another one and another... You'll also be able to do the same with your Node.js web servers. 因此,如果您的第一个Nginx服务器已经超出了它,那么您可以安装另一个Nginx服务器......您还可以对Node.js Web服务器执行相同的操作。

client web browser <--> Nginx <--> Express app.js 客户端Web浏览器< - > Nginx < - > Express app.js

You will also be able to serve static content faster if you serve from nginx and then use Express for your dynamic content. 如果您从nginx提供服务,然后使用Express作为动态内容,您还可以更快地提供静态内容。 For deployment, you might want to write an sh script to just copy and run your Express server like normal and run your Nginx server like normal, but with a forwarding proxy setup. 对于部署,您可能希望编写一个sh脚本来像正常一样复制和运行Express服务器并像正常一样运行您的Nginx服务器,但使用转发代理设置。 You could use a script in Nginx like this: 你可以在Nginx中使用这样的脚本:

upstream your_domain_app {
    server 127.0.0.1:8000;
}

server {
    listen 0.0.0.0:80;
    server_name your_domain.com your_domain;
    access_log /var/log/nginx/your_domain.log;

    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://your_domain_app/;
      proxy_redirect off;
    }
 }

Reasons for using an webserver like apache or in most cases nginx are: 使用像apache这样的网络服务器或在大多数情况下使用nginx的原因是:

  1. Load Balancing - It is very common practise to use web-server in front of a application/web server for load balancing 负载平衡 - 在应用程序/ Web服务器前使用Web服务器进行负载平衡是非常常见的做法
  2. Caching - Webserver like nginx provide extensive caching abilities compared to that of nodejs or other such application servers 缓存 - 与nodejs或其他此类应用程序服务器相比,像nginx这样的Webserver提供了广泛的缓存功能
  3. Request handling - Pure webserver like nginx is far superior in terms of handling multiple request at a time ie high concurrency. 请求处理 - 像nginx这样的纯网络服务器在处理多个请求方面(即高并发性)方面要优越得多。

Caching and Superior Request handling abilities of webserver like nginx make them a killer choice to be used as proxy servers in front of nodejs easing off load from them. 像nginx这样的网络服务器的缓存和高级请求处理功能使它们成为在nodejs之前用作代理服务器的一个杀手选择,从而减轻了它们的负载。

Also of serving static files using webservers are very common instead of nodejs due to above mentioned reasons. 由于上述原因,使用web服务器提供静态文件也很常见,而不是nodejs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM