服务器已安装Apache。 如何安装nginx?

Apache被安装并且主要用于服务Django / Python的内容。 我想安装nginx来提供静态内容。 如何做到这一点,什么是一个好的configuration。

我在Ubuntu上,Apache通过apt-get安装。

  1. 第一个select:你有几个ip addreses? 将apache绑定到其中的一个,nginx绑定到另一个。

    在debian世界

    对于/etc/apache2/ports.conf中的apache包括:

    Listen 1.1.2.3:80 

    在/ etc / apache2 / sites-enabled中的vhost文件中更改:

     NameVirtualHost 1.1.2.3:80; <VirtualHost 1.1.2.3:80> .. <VirtualHost/> 

    对于/ etc / nginx / sites-enabled / add中的nginx:

     listen 1.1.2.4:80 
  2. 第二个选项:移动Apache来监听不同的端口[上面的文件。 告诉Apache听127.0.0.1:8080为例],并指示nginx反向代理dynamicstream量到你的Apache,而服务/静态/由Nginx – 例如:

     server { listen 1.1.2.3:80; server_name some.name another.dname; access_log /var/log/nginx/something-access.log; location / { proxy_pass http://localhost: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; } location ~* ^.+\.(jpg|js|jpeg|png)$ { root /some/where/on/your/disks; } ; put your static hosting config here. } 

顺便说一句 – 考虑将静态内容放在另一个域。 这应该会提高最终用户的速度。

如何做到这一点有很多变化。 我更喜欢有一个应用程序专门决定哪个服务器处理什么样的内容,而后端服务器只是提供他们所请求的文件。

为此,我在前端使用了Varnish反向代理,在端口80上侦听。在后面,我有Apache(端口8880)和nginx(端口8881),它们都configuration为相同的域并指向相同的目录结构。 在我的Varnishconfiguration文件中,我有这样的东西:

 backend apache { .host = "192.168.0.2"; .port = "8880"; } backend nginx { .host = "192.168.0.2"; .port = "8881"; } [...] if (req.url ~ "\.(png|gif|jpg|ico|html|htm|js|css|txt)$") { set req.backend = nginx; } else { set req.backend = apache; } 

当然,还有一点点,但你明白了。

既然您已经安装了Apache和nginx,您可能需要浏览这个链接, 这个链接描述了一个非常类似于您的情况,但是使用nginx作为静态内容的前端,然后将请求传递给Apache。

如果你想变得非常简单,你可以简单地在Apache前面使用一个反向caching代理(比如varnish或者nginx)。 它会做的是caching请求,以便快速地将它们提供给客户端,同时减轻Web服务器本身服务于相同的请求。 这就其本质而言,会产生与你所要求的相同的效果。 由于静态页面和图像很less发生变化,因此它们几乎总是被前端caching,而dynamic页面将被检测到,并始终传递到后端。