Nginx 作为 Apache 的反向代理是否仅对动态内容提供帮助

发布于 2024-08-28 19:30:14 字数 421 浏览 11 评论 0原文

我计划将所有静态内容移至 CDN,以便在我的服务器上只剩下动态内容。我现在将 Nginx 设置为 Apache 的反向代理。传入的静态请求由 Nginx 直接传递,无需转到 Apache。

在这种情况下,Nginx 处理了很大一部分请求,我可以清楚地看到 Nginx 的必要性。

既然我把所有静态内容都移到了另一个域,那么Apache前面还需要有nginx吗?因为现在所有的请求默认都是动态请求并且全部转到Apache。

仅针对动态内容运行 Nginx 和 Apache 是否还有其他好处?

我的动态内容是 PHP/MySQL

编辑:

需要明确的是:我现在将 Nginx 作为反向代理。它提供静态和动态内容。但我正在将静态文件移动到 CDN。那么我的域名上还需要 Nginx 吗?

I am planning to move all my static content to a CDN so on my server I only have dynamic content left. I now have Nginx set up as reverse proxy to Apache. The static request that came in where directly delivered by Nginx without having to go to Apache.

In this case Nginx handled a large portion of the request and I can clearly see the necessity of Nginx.

Now that I moved all the static content to another domain, is there still a need to have nginx in front of Apache. Because now all the request are by default dynamic requests and all go to Apache.

Are there any other benefits of having Nginx and Apache running for only dynamic content.

My dynamic content is PHP/MySQL

Edit:

To be clear: I now have Nginx as a reverse proxy. It delivers static and dynamic content. But I am moving my static files to a CDN. Do I then still need Nginx on my domain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疑心病 2024-09-04 19:30:14

是的,你绝对需要在 Apache 之前使用 nginx。 Apache 每个连接使用 1 个线程或进程。这些线程中的每一个都占用内存。如果有几百人访问您的网站并且启用了 keepalive,则每个浏览器都会让 apache 进程或线程忙于占用服务器上的内存。

您可以通过在 apache 服务器上禁用 keepalive 来解决此问题,但这会降低网站的性能,因为浏览器无法重用连接。

因此,您可以使用 nginx 作为启用 keepalive 的反向代理。它可以用很小的内存占用(大约 8 兆)维持数千个连接。因为 nginx 位于 apache 服务器本地,所以每个请求仅占用 apache 子进程或线程几微秒。这意味着您只需很少的 apache 进程就可以为数千人提供服务。

此外,nginx 的配置比 apache 更加灵活,并且通过将其放在前端,它为您提供了很大的灵活性。

Yes you absolutely do need nginx in front of Apache. Apache uses 1 thread or process per connection. Each of these threads occupy memory. If you have a few hundred people visiting your website and you have keepalive enabled, each of these browsers will keep an apache process or thread busy occupying memory on your server.

You can work around this by disabling keepalive on your apache server but this slows down the performance of your website because browsers can't reuse connections.

So instead you use nginx as a reverse proxy with keepalive enabled. It can maintain thousands of connections with a tiny memory footprint (about 8 megs). Because nginx is local to your apache server each request only occupies an apache child or thread for a few microseconds. That means you can serve thousands of people with only a tiny handful of apache processes.

Also nginx's configuration is much more flexible than apache and by having it on the front end it gives you a lot of flexibility.

撩起发的微风 2024-09-04 19:30:14

我为一个网站所做的事情是:

  • 将 nginx 设置为 Apache 前面的反向代理,
  • 配置如下:
    • 对 PHP 页面的请求(即动态内容)被发送到 Apache
    • 对静态文件的请求(CSS、JS、...)由 nginx 直接提供服务。

这无需设置两个域:全部都在同一个域上。

基本上,我所做的是:

  • 从nginx提供图像,没有gzip压缩,使用缓存
  • 从nginx提供js/css(即文本文件),使用gzip压缩,使用缓存
  • 提供一些其他扩展(pdf,可执行文件,... ) 形成 nginx,不压缩,不缓存
  • 将其他请求传递给 Apache

我的 nginx 配置文件如下所示:

server {
    listen   80;
    server_name  MY_DOMAIN_NAME;

    access_log  /var/log/nginx/MY_DOMAIN_NAME.access.log;

    gzip on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types text/plain text/html text/css text/xml application/xml application/xml+rss application/xml+atom text/javascript application/x-javascript application/javascript;

    location ~* ^.+\.(jpg|jpeg|gif|png|ico)$  {
        root    /home/www/MY_DOMAIN_NAME;
        #access_log off;
        gzip off;
        expires 1d;
    }
    location ~* ^.+\.(css|js)$ {
        root    /home/www/MY_DOMAIN_NAME;
        #access_log off;
        expires 1d;
    }
    location ~* ^.+\.(pdf|gz|bz2|exe|rar|zip|7z)$ {
        root    /home/www/MY_DOMAIN_NAME;
        gzip off;
    }


    location / {
        proxy_pass   http://MY_DOMAIN_NAME:8080;
        proxy_redirect     off;

        proxy_set_header   Host             \$host;
        proxy_set_header   X-Real-IP        \$remote_addr;
        proxy_set_header   X-Forwarded-For  \$proxy_add_x_forwarded_for;
        proxy_max_temp_file_size 0;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
}

现在,为什么要做这样的事情呢?

好吧,nginx 应该:

  • 需要更少的内存
  • 更快
  • 能够处理更多的连接

因此,我认为它可以帮助网站有一点流量,以降低 Apache 的负载。

What I've done for one website is :

  • set up nginx as a reverse proxy in front of Apache
  • configure it so :
    • Requests to PHP pages (i.e. dynamic content) are sent to Apache
    • Requests to static files (CSS, JS, ...) are directly served by nginx.

This without having to set up two domains : all is on the same domain.

Basically, what I've done is :

  • serve images from nginx, without gzip compression, with caching
  • serve js/css (i.e. text files) from nginx, with gzip compression, with caching
  • serve some other extensions (pdf, exeutables, ...) form nginx, without compression, without caching
  • pass the other requests to Apache

Here's how my nginx's configuration file looks like :

server {
    listen   80;
    server_name  MY_DOMAIN_NAME;

    access_log  /var/log/nginx/MY_DOMAIN_NAME.access.log;

    gzip on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types text/plain text/html text/css text/xml application/xml application/xml+rss application/xml+atom text/javascript application/x-javascript application/javascript;

    location ~* ^.+\.(jpg|jpeg|gif|png|ico)$  {
        root    /home/www/MY_DOMAIN_NAME;
        #access_log off;
        gzip off;
        expires 1d;
    }
    location ~* ^.+\.(css|js)$ {
        root    /home/www/MY_DOMAIN_NAME;
        #access_log off;
        expires 1d;
    }
    location ~* ^.+\.(pdf|gz|bz2|exe|rar|zip|7z)$ {
        root    /home/www/MY_DOMAIN_NAME;
        gzip off;
    }


    location / {
        proxy_pass   http://MY_DOMAIN_NAME:8080;
        proxy_redirect     off;

        proxy_set_header   Host             \$host;
        proxy_set_header   X-Real-IP        \$remote_addr;
        proxy_set_header   X-Forwarded-For  \$proxy_add_x_forwarded_for;
        proxy_max_temp_file_size 0;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
}

Now, why do such a thing ?

Well, nginx is supposed to :

  • Need less memory
  • Be faster
  • Be able to handle more connections

So, I suppose it could help on a website with a bit of traffic, to lower the load that's put on Apache.

揽清风入怀 2024-09-04 19:30:14

不,你不再需要 nginx 了。

No, you don't need nginx anymore.

许仙没带伞 2024-09-04 19:30:14

您还可以使用 nginx 从 apache 实例卸载 SSL 处理。

例如,我们有一个堆栈配置有 nginx->haproxy->apache 服务器池。 nginx 和 haproxy 共同生活在一个 heartbeat 集群上,并将请求发送到后端的 apache 盒子池中。我们在 nginx 前端安装所有 SSL 证书。

You can also use nginx to offload SSL processing from the apache instances.

For example, we have one stack configured with nginx->haproxy->pool of apache servers. nginx and haproxy live together on a heartbeat cluster and feed requests into a pool of apache boxes on the backend. We install all the SSL certs on the nginx frontend.

橘虞初梦 2024-09-04 19:30:14

如果您使用 Apache 1.3,前面的 nginx 是最好的解决方案:

nginx 可以轻松地为数千个连接提供服务,但 Apache 不能

nginx in front is the best solution in case you use Apache 1.3:

nginx can easily serve thousands of conections, but Apache can't

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文