NGINX Web服务器可以充当功能非常强大的软件负载平衡器,此外还有更传统的角色,通过HTTP使用FastCGI处理程序为脚本提供静态内容和动态内容。因为NGINX使用非线程,事件驱动的架构,所以它能够胜过像Apache这样的Web服务器。在接收重负载的部署中尤其如此。
当服务单个网站的需求超过单个机器的功能时,使用代理是有帮助的。此外,还有一些Web框架,如Seaside和Ruby On Rails的Mongrel服务器,可以在特定于框架的Web服务器上部署应用程序。虽然这些单用途服务器提供强大的应用程序服务,但它们不适合托管整个应用程序。在这些情况下,使用NGINX作为前端代理仅将基本请求传递给应用程序服务器是将动态内容与静态内容统一并提供稳定生产环境的可行方法。
本文档概述了如何将NGINX用作其他HTTP服务器的前端代理服务器,以及作为软件负载平衡器在整个提供HTTP资源的计算机集群中分配流量。有关配置NGINX的介绍性指南,请参阅我们的基本NGINX配置指南。如果您想要使用PHP或Perl脚本的内容进行简单的NGINX部署,请考虑遵循我们的安装NGINX指南之一。
在开始之前,请确保您已完成以下操作:
如果您不熟悉Linux服务器管理,您可能会对我们的Linux基础知识指南,初学者指南和管理基础知识指南感兴趣。
个人更推荐您使用免费的腾讯云开发者实验室进行试验,学会安装后再购买服务器
当请求到达NGINX前端代理服务器时,以下是发生的过程的概述:
在本节中,您将配置Apache以侦听备用端口,以便它可以响应NGINX前端。
注意本指南假设您使用的是Apache 2.4。如果您使用的是旧版本,则某些路径名称会略有不同。
/etc/apache2/ports.conf
文件进行编辑,并按如下所示进行配置:
sudo nano /etc/apache2/ports.conf
NameVirtualHost *:8000
Listen 8000
<IfModule mod_ssl.c>
# If you add NameVirtualHost *:443 here, you will also have to change
# the VirtualHost statement in /etc/apache2/sites-available/default-ssl
# to <VirtualHost*:443>
# Server Name Indication for SSL named virtual hosts is currently not
# supported by MSIE on Windows XP.
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule><VirtualHost *:>
行以使用端口8000。
sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:8000>
ServerAdmin webmaster@example.com
ServerName www.example.com
DocumentRoot /var/www/html/example.com
<Directory />
Options FollowSymLink
AllowOverride None
</Directory>
<Directory /var/www/html/example.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>/etc/apache2/apache2.conf
文件中,注释掉该LogFormat {User-Agent}
行。然后,添加一个转发,以便Apache将在访问日志中记录原始用户的IP地址,而不是NGINX的IP地址(将列为127.0.0.1)。
sudo nano /etc/apache2/apache2.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedlibapache2-mod-rpaf
,负责记录正确的IP地址。
sudo apt-get install libapache2-mod-rpaf/etc/nginx/proxy_params
文件。这些设置是从NGINX到Apache最佳转发代理请求的良好起点:
sudo nano /etc/nginx/proxy_params
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100M;
client_body_buffer_size 1m;
proxy_intercept_errors on;
proxy_buffering on;
proxy_buffer_size 128k;
proxy_buffers 256 16k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_max_temp_file_size 0;
proxy_read_timeout 300;/etc/nginx/sites-available/example.com
创建NGINX 虚拟主机文件example.com
。确保在此处指定与Apache相同的文档根目录(例如,/var/www/html/example.com
)。这将确保NGINX可以直接提供静态文件,而无需将请求传递给Apache。使用NGINX可以比Apache更快地提供静态文件(如JavaScript,CSS,图像,PDF文件,静态HTML文件等)。
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
server_name www.example.com example.com;
root /var/www/html/example.com;
if ($http_host != "www.example.com") {
rewrite ^ www.example.com$request_uri permanent;
}
index index.php index.html;
location / {
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
}
location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
}
}在文件location
的server
部分中还有一些额外的指令要添加/etc/nginx/sites-available/example.com
。您可能需要这些指令,但您可能不需要这些指令,具体取决于您的nginx和Apache配置。location
指令,使NGINX拒绝所有以字符开头的文件请求.ht
。几乎每个默认的Apache配置都有类似的指令。如果您的Apache部署依赖于.htaccess
和的设置,则此指令很有用.htpasswd
。
location ~ /\.ht {
deny all;
}http://example.com/
传递给以192.168.3.105
路径运行的服务器/teams/~example/
,则应编写以下location
块:
/etc/nginx/sites-available/example.com1 2 3 4
location / { rewrite ^(.*)$ /teams/~example/$1 break; proxy_pass http://192.168.3.105; }这里,重写规则(^(.*)$
)捕获整个请求字符串,并将it($1
)附加到新服务器(/teams/~example/
)上的路径。以下是这将如何发挥作用:
请求: http://example.com/images/images.png
响应: http://192.168.3.105/teams/~example/images/images.png
请求: http://example.com/wiki/PracticeSchedule/
响应: http://192.168.3.105/teams/~example/wiki/PracticeSchedule/
proxy_redirect
规范location
。该指令重写NGINX从代理服务器接收的HTTP头,使它们看起来好像是由NGINX服务器生成的。
example.com.vhost代理位置指令1 2 3 4
location /pictures/ {
proxy_pass http://192.168.3.106:8080;
proxy_redirect http://192.168.3.106:8080 http://example.com/pictures/;
}在此示例中,请求下面的资源http://example.com/pictures/
,然后传递给8080
在LAN IP地址的端口上运行的服务器192.168.3.106
。如果没有该proxy_redirect
指令,Location:
HTTP响应的标头将返回http://example.com/team.jpg
as 的请求的位置http://192.168.3.106:8080/team.jpg
。通过添加proxy_redirect
指令,代理请求将返回预期的Location:
标头。
除了使用NGINX作为前端代理将请求传递给其他Web服务器之外,NGINX还可以作为服务器集群的前端,甚至可以作为软件负载均衡器。
在本例中,我们将向您展示如何构建一个appcluster
使用简单循环负载均衡器命名的集群。以下是该/etc/nginx/sites-available/example.com
文件的适当摘录:
/etc/nginx/sites-available/example.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | server { listen 80; server_name example.com www.example.com; location / { proxy_pass http://appcluster; include /etc/nginx/proxy_params; } } upstream appcluster { server linode.example.com:8801; server linode.example.com:8802; server linode.example.com:8803 down; server linode.example.com:8804; server galloway.example.com:8801; server galloway.example.com:8802; server galloway.example.com:8803; server galloway.example.com:8804; } # [...] |
---|
在此示例中,在server
指令块中,NGINX配置为侦听特定IP地址和端口(例如192.0.2.0
和80
)上的请求,并响应对域example.com
和的请求www.example.com
。此域(例如/
)上的所有资源请求都将传递到指令中http://appcluster
建立的服务器upstream
。
该upstream
指令建立了循环负载平衡器。在此块中,列出了八个服务器,每个服务器都运行在不同的主机名和端口组合上。
upstream
配置必须发生在顶层http
的的块/etc/nginx/sites-available/example.com
文件。8801
通过8804
所述服务器linode.example.com
和galloway.example.com
将接收由上游的请求的相等部分appcluster
。down
参数排除该服务器被代理。在其中一台服务器关闭时使用它。NGINX还允许您控制upstream
资源集群的行为,而不仅仅是简单的循环设置。最简单的修改是将ip_hash
指令添加到配置块。这会将来自同一IP地址的请求路由到同一后端服务器。请考虑以下示例摘录:
/etc/nginx/sites-available/example.com
1 2 3 4 5 6 7 | upstream appcluster { ip_hash; server linode.example.com:8801; server linode.example.com:8802; server galloway.example.com:8801 down; server galloway.example.com:8802; } |
---|
这里,该ip_hash
指令使NGINX尝试将来自单个IP地址的请求与相同的后端组件进行匹配。如果组件服务器无法访问,NGINX会将这些连接路由到备用组件。
注意:如果服务器需要在较长时间内脱机,请附加
down
参数,如条目中所示galloway.example.com:8801
。这将防止错过的连接尝试命中关闭的服务器组件。
这是一个更高级的配置,其中在服务器上的唯一端口上运行的七个服务器组件linode.example.com
包含appcluster
上游:
/etc/nginx/sites-available/example.com
1 2 3 4 5 6 7 8 9 | upstream appcluster { server linode.example.com:8801; server linode.example.com:8802 weight=1; server linode.example.com:8803 weight=2 max_fails=2; server linode.example.com:8804 weight=2 max_fails=2 fail_timeout=20; server linode.example.com:8805 weight=4; server linode.example.com:8806 weight=4 fail_timeout=4; server linode.example.com:8807 weight=2 fail_timeout=20; } |
---|
使用这些参数,您可以使用NGINX来管理服务器集群中的负载行为和分布:
1
。该论点weight=[number]
设定了一个特定的权重。数字越大,重量越大。
在上面的例子中,组件上的端口运行8801
和8802
由NGINX相同处理,作为默认值weight
是1
。组件上运行8803
,8804
以及8807
将获得两倍多的流量作为前两个部分。组件上运行8805
,并8806
会收到四倍多的流量对那些8801
与8802
和两倍多的流量上的组件8803
,8804
和8807
。max_fails=[number]
指定在被认为不起作用之前与上游组件通信的尝试失败的次数。为防止组件被标记为无效,即使它们无法访问,也请将此值设置为0
。max_fails
is 的默认值1
。
在上面的例子中,在端口组件服务器8801
,8802
,8805
,8806
,和8807
只能拒绝一次被标记为不可操作前的连接。组件8803
和8804
允许被标记为不可操作之前失败了两次。fail_timeout=[time-in=seconds]
参数确定max_fails
必须发生不成功尝试次数的时间跨度,以便标记服务器的组件不起作用。请注意,返回404响应的服务器被视为可操作。此外,此值不会影响已建立的代理连接的超时。
默认情况下,所有组件都具有自己的故障计数器每10秒,覆盖部件复位8801
,8802
,8803
,和8805
。在上面的例子中,组件上运行8804
并8807
具有其失败计数器每20秒复位,而8806
具有其计数器每4秒复位。ip_hash
指令不能与上面示例中显示的其他参数组合使用。有关此主题的其他信息,您可能需要参考以下资源。虽然提供这些是希望它们有用,但请注意,我们无法保证外部托管材料的准确性或及时性。
更多教程请前往腾讯云+社区学习更多知识。
参考文献:《Use NGINX as a Front-end Proxy and Software Load Balancer》