在Debian或Ubuntu上为Apache主服务器设置Nginx反向代理

作者: 丘山 分类: Vultr中文文档 发布时间: 2018-06-23 00:12

Nginx是一个轻量级的Web服务器,已被证明比Apache更快地提供静态文件。本教程将指导您如何将Nginx配置为反向代理,使其与Apache配合工作。

要求

您已在服务器上安装了Apache。Apache已经在端口80上运行一个站点。

更改Apache侦听端口

编辑/etc/apache2/ports.conf以使Apache监听端口8080而不是默认端口80。

查找以下行:

NameVirtualHost *:80 Listen 80

将其更改为:

NameVirtualHost *:8080 Listen 8080

不要忘记/etc/apache2/sites-enabled/*现有vhost侦听端口

更改:

<VirtualHost *:80>

至:

<VirtualHost *:8080>

在Apache中禁用Unuse模块

由于HTTP请求现在由Nginx处理,我们可以在Apache.Edit /etc/apache2/apache2.conf禁用KeepAlive并更改:

KeepAlive Off

另外,运行以下命令禁用不再需要的模块。

a2dismod deflate a2dismod cgi a2dismod autoindex a2dismod negotiation a2dismod ssl

安装转发模块

在Apache中安装mod_rpaf以将访客IP转发给Apache。否则,您的脚本将读取REMOTE_ADDR值作为服务器IP。

apt-get install libapache2-mod-rpaf

停止Apache服务

/etc/init.d/apache2 restart

设置Nginx

安装Nginx。

apt-get install nginx

删除默认虚拟主机以防止冲突。

rm -rf /etc/nginx/sites-enabled/*

创建一个新的默认虚拟主机:

cat >/etc/nginx/sites-available/000-default <<EOF server { access_log off; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_pass http://127.0.0.1:8080; } } EOF ln -s /etc/nginx/sites-available/000-default /etc/nginx/sites-enabled/000-default

为现有网站创建虚拟主机以将请求转发给Apache:

cat >/etc/nginx/sites-available/domain.com <<EOF server { server_name www.domain.com domain.com; root /var/www/domain.com/; access_log off; # Static contents location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ { expires max; } # Dynamic content, forward to Apache location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_pass http://127.0.0.1:8080; } } EOF ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/domain.com

重新启动Nginx并完成。

/etc/init.d/nginx restart

回复 匿名 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注