如何在Ubuntu 18.04服务器上将Nginx配置为Web服务器和Apache的反向代理

如何在Ubuntu 18.04服务器上将Nginx配置为Web服务器和Apache的反向代理
如何在Ubuntu 18.04服务器上将Nginx配置为Web服务器和Apache的反向代理

简介

Apache和Nginx是两个非常流行的开源Web服务器,它们经常单独与PHP一起来构建网站服务器环境。当在同一台服务器上,托管具有不同要求的多个网站时,如果能同时发挥两个web服务器的优势,那么对于提升网站的性能是非常有帮助的。要在同一个服务器上,同时运行Apache和Nginx两个Web服务器,一般是使用多个IP地址或同一个IP地址使用不同的端口号的方式。

对于同时提供IPv4和IPv6地址的服务器来说,可以使用IPv4配置Apache站点,而使用IPv6来配置Nginx站点。但是因为ISP尚未广泛推广IPv6的应用,所以对于只有一个IPv4地址的服务器,我们可以采用另外一种解决方案,通过配置服务器上不同端口号(例如,818080)的方式。

在本教程中,我们将向您介绍,如何在同一台服务器上,既将Nginx既配置为Web服务器,又将其配置为Apache的反向代理的方式。

根据Web应用程序的不同,可能需要更改代码以保持Apache反向代理意识,尤其是在站点配置了SSL时。为避免这种情况,我们将安装一个名为mod_rpaf的Apache模块,该模块将重写某些环境变量,看起来像是Apache在直接处理来自Web客户端的请求。

我们将在一台服务器上部署四个网站。Nginx将为其中两个站点提供服务:example.comsample.org。Apache则为其余两个站点提供服务:foobar.net和和test.io。另外,我们安装php-fpm而不是mod_php,因为php-fpm有更优的性能,能更好的支持http/2。

第一步 — 安装 Apache 和 PHP-FPM

首先,安装Apache和PHP-FPM。

除了Apache和PHP-FPM,我们还将安装libapache2-mod-fastcgi模块,以支持FastCGI Web应用程序。

首先,更新系统软件包列表,以确保系统拥有最新的软件包。

sudo apt update

接下来,安装Apache和PHP-FPM软件包:

sudo apt install apache2 php-fpm

libapache2-mod-fastcgi模块没有在Ubuntu的软件包列表中,因此需要从kernel.org下载该模块,并使用以下dpkg命令进行安装。

wget https://mirrors.edge.kernel.org/ubuntu/pool/multiverse/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb
sudo dpkg -i libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb

接下来,让我们将Apache的默认配置,更改为使用PHP-FPM。

第二步 — 配置 Apache 和 PHP-FPM

在这一步中,我们将Apache的端口号更改为8080,并通过mod_fastcgi模块,配置Apache与PHP-FPM一起工作。重命名Apache的ports.conf配置文件:

sudo mv /etc/apache2/ports.conf /etc/apache2/ports.conf.default

创建一个新的ports.conf端口配置文件,然后配置监听8080端口:

echo "Listen 8080" | sudo tee /etc/apache2/ports.conf

注意:在配置反向代理时,通常将Web服务器设置为监听127.0.0.1:8080,但是这样做会将PHP的环境变量SERVER_ADDR的值设置为环回IP地址(127.0.0.1),而不是服务器的公共IP。我们以这样的方式配置Apache,使网站前端看不到反向代理服务器。因此,我们配置所有IP地址都侦听8080端口。

接下来,我们将为Apache创建一个虚拟主机文件。该文件的<VirtualHost>标签将设置为8080端口

禁用默认虚拟主机:

sudo a2dissite 000-default

然后,使用默认Apache配置文件创建一个新的虚拟主机配置文件001-default.conf:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/001-default.conf

编辑新创建的配置文件:

sudo nano /etc/apache2/sites-available/001-default.conf

配置监听 8080 端口:

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:8080>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

保存文件,并通过下面的命令启用配置文件:

sudo a2ensite 001-default

重新加载Apache2:

sudo systemctl reload apache2

验证Apache是否正在监听8080端口:

sudo netstat -tlpn

输入内容如下,Apache正在监听8080端口:

Output
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address     Foreign Address      State    PID/Program name
tcp        0      0 0.0.0.0:22        0.0.0.0:*            LISTEN   1086/sshd
tcp6       0      0 :::8080           :::*                 LISTEN   4678/apache2
tcp6       0      0 :::22             :::*                 LISTEN   1086/sshd

第三步 — 使用mod_fastcgi配置 Apache

默认情况下,Apache使用mod_php模块,我们需要做些配置以使用PHP-FPM。

注意:如果已经安装了带有mod_php模块的LAMP堆栈,要想关闭该模块,需要执行下面的命令先关闭php

sudo a2dismod php7.2

我们将为mod_fastcgi模块进行一定的配置,该模块运行依赖于mod_action模块。默认情况下,mod_action模块处于禁用状态,因此我们需要启用它:

sudo a2enmod actions

重命名以备份现有的 FastCGI 配置文件:

sudo mv /etc/apache2/mods-enabled/fastcgi.conf /etc/apache2/mods-enabled/fastcgi.conf.default

创建一个新的配置文件:

sudo nano /etc/apache2/mods-enabled/fastcgi.conf

将以下指令添加到文件中,以将对.php文件的请求传递到PHP-FPM:

/etc/apache2/mods-enabled/fastcgi.conf

<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi
  FastCgiIpcDir /var/lib/apache2/fastcgi
  AddType application/x-httpd-fastphp .php
  Action application/x-httpd-fastphp /php-fcgi
  Alias /php-fcgi /usr/lib/cgi-bin/php-fcgi
  FastCgiExternalServer /usr/lib/cgi-bin/php-fcgi -socket /run/php/php7.2-fpm.sock -pass-header Authorization
  <Directory /usr/lib/cgi-bin>
    Require all granted
  </Directory>
</IfModule>

保存更改,并测试配置:

sudo apachectl -t

如果显示语法没有问题,重启Apache2:

sudo systemctl reload apache2

如果看到如下警告:Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message.,暂时将其忽略。稍后我们将配置服务器名称。

现在,确保Apache可以正常工作。

第四步 — 检验 PHP 功能

让我们通过创建一个phpinfo()文件,并从Web浏览器访问它来验证PHP能否正常工作。

创建/var/www/html/info.php文件,文件包含该代码段:<?php phpinfo(); ?>:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

请注意,如果服务器已启用了Apache防火墙,确保防火墙开放了8080端口访问 。在步骤10中,我们将限制对该端口的公共访问。

设置防火墙允许8080端口:

sudo ufw allow 8080

由于我们还要开启https,因此我们还要确保,防火墙允许443端口访问。

执行如下命令,允许80端口和443端口:

sudo ufw allow "Apache Full"

检查防火墙的状态:

sudo ufw status

可能输出如下内容:

OutputTo                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
8080                       ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)
8080 (v6)                  ALLOW       Anywhere (v6)

可以看到 8080 和 Apache Full 已经被防火墙放行,接下来访问 info.php 页面。

通过浏览器访问 http://your_server_ip:8080/info.php,将详细展示php的配置信息。

phpinfo Server API
phpinfo PHP Variables

在页面的顶部,检查服务器API显示为FPM / FastCGI。在页面的大约三分之二的位置,“ PHP变量”部分将告诉您服务器软件是Ubuntu上的Apache。这些说明mod_fastcgi模块已经启用,并且Apache正在使用PHP-FPM处理PHP文件。

第五步 — 为Apache配置虚拟主机(Virtual Hosts)

Let’s create Apache virtual host files for the domains foobar.net and test.io. To do that, we’ll first create document root directories for both sites and place some default files in those directories so we can easily test our configuration.

让我们为域名foobar.nettest.io创建Apache虚拟主机配置文件。为此,我们将首先为两个站点创建各创建一个文件夹,然后在这些文件夹内,创建一个用于测试的html文件。

首先,分别为两个网站创建根目录:

sudo mkdir -v /var/www/foobar.net /var/www/test.io

在每个网站根目录下,个创建一个“index.html”文件,用于测试:

echo "<h1 style='color: green;'>Foo Bar</h1>" | sudo tee /var/www/foobar.net/index.html
echo "<h1 style='color: red;'>Test IO</h1>" | sudo tee /var/www/test.io/index.html

在每个站点的根目录下,分别创建一个“info.php”文件,用来测试PHP配置是否正常:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/foobar.net/info.php
echo "<?php phpinfo(); ?>" | sudo tee /var/www/test.io/info.php

接下来,为foobar.net 这个网站创建一个配置文件:

sudo nano /etc/apache2/sites-available/foobar.net.conf

将下面的代码复制到配置文件中:

/etc/apache2/sites-available/foobar.net.conf

<VirtualHost *:8080>
    ServerName foobar.net
    ServerAlias www.foobar.net
    DocumentRoot /var/www/foobar.net
    <Directory /var/www/foobar.net>
        AllowOverride All
    </Directory>
</VirtualHost>

代码中的 AllowOverride All ,开启网站支持.htaccess配置文件。

保存并关闭文件后,再为网站test.io 创建一个配置文件:

sudo nano /etc/apache2/sites-available/test.io.conf

将以下代码复制到配置文件中:

/etc/apache2/sites-available/test.io.conf

<VirtualHost *:8080>
    ServerName test.io
    ServerAlias www.test.io
    DocumentRoot /var/www/test.io
    <Directory /var/www/test.io>
        AllowOverride All
    </Directory>
</VirtualHost>

保存并退出。

现在,就已经成功创建了Apache的虚拟主机配置文件,使用 a2ensite 命令启用配置文件。 执行后,将在 sites-enabled 文件夹下创建一个软连接(symbolic link):

sudo a2ensite foobar.net
sudo a2ensite test.io

检查Apache配置文件是否有配置错误:

sudo apachectl -t

如果没有错误,会看到显示“语法正确”。如果看到其他内容,请检查配置,然后重试。

配置无误后,重新加载Apache,使配置生效:

sudo systemctl reload apache2

To confirm the sites are working, open http://foobar.net:8080 and http://test.io:8080 in your browser and verify that each site displays its index.html file.

要确认这些站点正常运行,请在浏览器中打开http://foobar.net:8080和http://test.io:8080,并确认每个站点都正确显示了index.html文件内容。

可以看到如下的页面:

foobar.net index page
test.io index page

在浏览器中分别访问 http://foobar.net:8080/info.php 和 http://test.io:8080/info.php,以确保PHP文件可以正常工作。

我们看到的PHP配置文件的内容,与第四步所看到的类似的内容。

现在我们已经成功使用Apache在8080端口部署了两个网站,接下来我们通过Nginx来部署另外两个网站。

第六步 — 安装和配置 Nginx

在这一步中,我们将安装Nginx并配置Nginx的虚拟主机,已部署域名example.comsample.org

安装Nginx:

sudo apt install nginx

接下来删除默认的虚拟主机软连接:

sudo rm /etc/nginx/sites-enabled/default

我们将创建自己的虚拟主机配置文件 (example.com).

现在,我们要为Nginx创建虚拟主机。首先为两个网站创建文档根目录:

sudo mkdir -v /usr/share/nginx/example.com /usr/share/nginx/sample.org

我们将Nginx网站部署在/usr/share/nginx文件夹中,这是Nginx默认部署网站的目录。也可以将它们与Apache站点一起放在/var/www/html,但是放在不同的文件夹中,更方便我们进行管理。

同样在网站根目录下,创建“index.html”文件和“phpinfo().php”文件,以用来测试部署是否成功。

echo "<h1 style='color: green;'>Example.com</h1>" | sudo tee /usr/share/nginx/example.com/index.html
echo "<h1 style='color: red;'>Sample.org</h1>" | sudo tee /usr/share/nginx/sample.org/index.html
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/example.com/info.php
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/sample.org/info.php

现在为域名example.com创建一个虚拟主机配置文件:

sudo nano /etc/nginx/sites-available/example.com

与Apache不同的是,Nginx使用server {. . .}标签块来配置网站。为网站example.com进行初步配置,使用default_server配置指令,指定该虚拟主机来处理来自HTTP的请求。

/etc/nginx/sites-available/example.com

server {
    listen 80 default_server;

    root /usr/share/nginx/example.com;
    index index.php index.html index.htm;

    server_name example.com www.example.com;
    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        include snippets/fastcgi-php.conf;
    }
}

保存并关闭文件。现在为第二个域 sample.org 创建虚拟主机配置i文件:

sudo nano etc/nginx/sites-available/sample.org

添加以下内容到配置文件:

/etc/nginx/sites-available/sample.org

server {
    root /usr/share/nginx/sample.org;
    index index.php index.html index.htm;

    server_name sample.org www.sample.org;
    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        include snippets/fastcgi-php.conf;
    }
}

保存并关闭文件。

然后,通过创建指向sites-enabled目录的符号链接来启用两个站点:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
sudo ln -s /etc/nginx/sites-available/sample.org /etc/nginx/sites-enabled/sample.org

然后测试Nginx配置,以确保配置没有问题:

sudo nginx -t

如果没有提示错误,重新启动Nginx使配置生效:

在浏览器中访问 http://example.com/info.php 和 http://sample.org/info.php ,以访问Nginx网站根目录下的phpinfo()文件,查看php变量信息。

Nginx PHP Variables

[“SERVER_SOFTWARE”] 应该是 nginx,表面是使用Nginx服务器。[“DOCUMENT_ROOT”] 应该指向我们为Nginx配置的网站目录。

至此,我们已经安装了Nginx,并创建了两个虚拟主机。接下来,我们将配置Nginx代理来自Apache网站的请求。

第七步 — 配置Nginx反向代理Apache网站

创建一个新的Nginx虚拟主机配置文件,将Apache配置的网站域名添加到 server_name 指令处。来自这些域名的请求将被转发到Apache。

创建一个新的Nginx虚拟主机配置文件,用来将对应的请求转发到Apache:

sudo nano /etc/nginx/sites-available/apache

添加以下代码块,请记住在 proxy_pass 处使用公共IP地址:

/etc/nginx/sites-available/apache

server {
    listen 80;
    server_name foobar.net www.foobar.net test.io www.test.io;

    location / {
        proxy_pass http://your_server_ip:8080;
        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_set_header X-Forwarded-Proto $scheme;
    }
}

保存文件,并创建符号链接来启用这个新的虚拟主机配置文件:

sudo ln -s /etc/nginx/sites-available/apache /etc/nginx/sites-enabled/apache

测试Nginx配置,确保没有出错:

sudo nginx -t

如果没有提示Nginx错误,重启Nginx:

sudo systemctl reload nginx

打开浏览器,然后在浏览器中访问http://foobar.net/info.php 。向下滚动到“ PHP变量部分,然后检查显示的值。

phpinfo of Apache via Nginx

通过变量SERVER_SOFTWAREDOCUMENT_ROOT,可以确认此请求已由Apache处理。变量HTTP_X_REAL_IPHTTP_X_FORWARDED_FOR由Nginx添加,并且应显示用于访问URL的计算机的公共IP地址。

我们已成功设置Nginx,将对特定域的请求代理到Apache。接下来,让我们配置Apache来设置REMOTE_ADDR变量,就像它直接处理这些请求一样。

第八步 — 安装和配置 mod_rpaf

在此步骤中,我们将安装一个名为 mod\_rpaf 的Apache模块,该模块根据反向代理提供的值,重写REMOTE_ADDRHTTPSHTTP_PORT的值。没有此模块,某些PHP应用程序将需要更改代码,才能在代理转发后正常地工作。该模块libapache2-mod-rpaf在Ubuntu的存储库中存在,但已过时,并且不支持某些配置指令。相反,我们将从源代码安装它。

安装构建模块所需的软件包:

sudo apt install unzip build-essential apache2-dev

从GitHub下载最新的稳定版本:

wget https://github.com/gnif/mod_rpaf/archive/stable.zip

解压下载的文件:

unzip stable.zip

转到包含文件的新目录:

cd mod_rpaf-stable

编译安装模块:

make
sudo make install

接下来,在mods-available目录中创建一个新文件,以加载rpaf模块的:

sudo nano /etc/apache2/mods-available/rpaf.load

将以下代码添加到文件中以加载模块:

/etc/apache2/mods-available/rpaf.load

LoadModule rpaf_module /usr/lib/apache2/modules/mod_rpaf.so

保存文件并退出编辑器。

在此目录中创建另一个名为rpaf.conf的文件,该文件包含以下mod_rpaf 的配置指令:

sudo nano /etc/apache2/mods-available/rpaf.conf

添加以下代码块以配置 mod_rpaf,确保指定服务器的IP地址:

/etc/apache2/mods-available/rpaf.conf

<IfModule mod_rpaf.c>
    RPAF_Enable             On
    RPAF_Header             X-Real-Ip
    RPAF_ProxyIPs           your_server_ip
    RPAF_SetHostName        On
    RPAF_SetHTTPS           On
    RPAF_SetPort            On
</IfModule>

这是每个指令的简要说明。有关更多信息,请参见mod_rpaf README文件。

  • RPAF_Header – 用于客户端的真实IP地址的标头。
  • RPAF_ProxyIPs – 调整HTTP请求访问的代理IP。
  • RPAF_SetHostName – 更新主机名,让 ServerName 和 ServerAlias 正常工作。
  • RPAF_SetHTTPS – Sets the HTTPS environment variable based on the value contained in X-Forwarded-Proto.
  • RPAF_SetPort – 设置 SERVER_PORT 环境变量, 当Apache使用SSL代理时非常有用。

保存并启用rpaf.conf模块:

sudo a2enmod rpaf

这将在mods-enabled目录中创建rpaf.loadrpaf.conf文件的符号链接。现在进行配置测试:

sudo apachectl -t

如果没有错误,请重新加载Apache:

sudo systemctl reload apache2

在浏览器中访问 http://foobar.net/info.php 和 http://test.io/info.php 的phpinfo()文件,检查“ PHP变量”部分。REMOTE_ADDR变量现在也将是你的本地计算机的公网IP地址。

现在,我们为每个站点设置TLS / SSL加密。

第九步 — 使用Let’s Encrypt配置网站HTTPS

在此步骤中,我们将为Apache上托管的两个域配置TLS / SSL证书。我们将获得通过[咱们加密]证书(https://letsencrypt.org。Nginx的支持SSL终端,所以我们可以建立SSL而不需要修改Apache的配置文件。该mod_rpaf模块确保所需的环境变量是在Apache的设置,使应用程序在SSL反向代理后面无缝地工作。

首先,我们将server {...}两个域的块分开,以便每个域都可以拥有自己的SSL证书。/etc/nginx/sites-available/apache在编辑器中打开文件:

sudo nano /etc/nginx/sites-available/apache

修改文件,使其看起来像这样,foobar.nettest.io在和各自的server块中:

/etc/nginx/sites-available/apache

server {
    listen 80;
    server_name foobar.net www.foobar.net;

    location / {
        proxy_pass http://your_server_ip:8080;
        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_set_header X-Forwarded-Proto $scheme;
    }
}
server {
    listen 80;
    server_name test.io www.test.io;

    location / {
        proxy_pass http://your_server_ip:8080;
        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_set_header X-Forwarded-Proto $scheme;
    }
}

我们将使用Certbot生成TLS / SSL证书。它的Nginx插件将负责重新配置Nginx并在必要时重新加载配置。

首先,添加官方的Certbot存储库:

sudo add-apt-repository ppa:certbot/certbot

ENTER提示确认时要添加新的存储库。然后更新软件包列表以获取新存储库的软件包信息:

sudo apt update

然后使用以下命令安装Certbot的Nginx软件包apt

sudo apt install python-certbot-nginx

安装完成后,使用 certbot命令为foobar.net和生成证书www.foobar.net

sudo certbot --nginx -d foobar.net -d www.foobar.net

此命令告诉Certbot使用该nginx插件,-d用于指定我们希望证书对其有效的名称。

如果这是您第一次运行certbot,将提示您输入电子邮件地址并同意服务条款。这样做之后,certbot将与Let’s Encrypt服务器通信,然后进行质询以验证您是否控制了要为其申请证书的域。

接下来,Certbot将询问您如何配置HTTPS设置:

OutputPlease choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

选择您的选择,然后按ENTER。配置将被更新,并且Nginx将重新加载以获取新设置。

现在,为第二个域执行命令:

sudo certbot --nginx -d test.io -d www.test.io

使用https://前缀在浏览器中访问Apache的一个域;造访,您会看到以下内容:https://foobar.net/info.php

phpinfo ssl

在“ PHP变量”部分中查找。变量SERVER_PORT已设置为443HTTPS设置为on,就像通过HTTPS直接访问Apache一样。设置了这些变量后,不必对PHP应用程序进行特殊配置即可在反向代理后面工作。

第十步 — 禁止直接访问 Apache

由于Apache正在公共IP地址上的8080端口上进行侦听,因此每个人都可以访问。可以通过在防火墙规则集中,使用以下IPtables命令来阻止它。

sudo iptables -I INPUT -p tcp --dport 8080 ! -s your_server_ip -j REJECT --reject-with tcp-reset

确保使用服务器的IP地址代替红色的示例。一旦在防火墙中阻止8080端口,请测试Apache是​​否不可访问。打开您的Web浏览器,访问一个8080端口域名,例如:http://example.com:8080。

浏览器应显示“无法连接”或“网页不可用”的错误消息。

注意:默认情况下,IPtables规则无法在系统重新引导后继续运行。保留IPtables规则的方法有多种,但最简单的方法是iptables-persistent在Ubuntu的存储库中使用。浏览本文以了解有关如何配置IPTables的更多信息。

现在,我们将配置Nginx以为Apache站点提供代理访问静态文件。

第十一步 — 使用Nginx处理静态文件

当Nginx代理请求Apache域名的时候,它将把对该域名下每个文件的请求发送到Apache。Nginx在处理静态文件(如图像,JavaScript和样式表等)请求方面比Apache快。因此,让我们配置Nginx代理apache服务器的静态文件请求,然后将PHP动态请求发送到Apache。

编辑/etc/nginx/sites-available/apache文件:

sudo nano /etc/nginx/sites-available/apache

在该文件中,额外添加两个location 配置区块,并对现有location区块进行修改。另外,我们需要告诉Nginx,每个站点的静态文件在哪里。

如果不打算使用SSL和TLS证书,请修改配置文件,看起来类似这样:

/etc/nginx/sites-available/apache

server {
    listen 80;
    server_name test.io www.test.io;
    root /var/www/test.io;
    index index.php index.htm index.html;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        proxy_pass http://your_server_ip:8080;
        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_set_header X-Forwarded-Proto $scheme;
    }

    location ~ /\.ht {
        deny all;
    }
}

server {
    listen 80;
    server_name foobar.net www.foobar.net;
    root /var/www/foobar.net;
    index index.php index.htm index.html;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        proxy_pass http://your_ip_address:8080;
        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_set_header X-Forwarded-Proto $scheme;
    }

    location ~ /\.ht {
        deny all;
    }
}

如果希望为网站配置HTTPS,请改用以下配置:

/etc/nginx/sites-available/apache

server {
    listen 80;
    server_name test.io www.test.io;
    root /var/www/test.io;
    index index.php index.htm index.html;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        proxy_pass http://your_server_ip:8080;
        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_set_header X-Forwarded-Proto $scheme;
    }

    location ~ /\.ht {
        deny all;
    }

    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/test.io/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/test.io/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
    listen 80;
    server_name foobar.net www.foobar.net;
    root /var/www/foobar.net;
    index index.php index.htm index.html;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        proxy_pass http://your_ip_address:8080;
        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_set_header X-Forwarded-Proto $scheme;
    }

    location ~ /\.ht {
        deny all;
    }

    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/foobar.net/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/foobar.net/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

try_files指令让Nginx在文档根目录中查找文件并直接提供服务。如果文件具有.php扩展名,则该请求将传递到Apache来处理。即使在文档根目录中找不到该文件,该请求也会传递给Apache,这样网站的固定链接等功能就可以正常工作了。

警告:location ~ /\.ht指令非常重要;它可以防止Nginx处理Apache的.htaccess.htpasswd等配置文件,这些配置文件包含很多重要信息,但是Nginx默认不支持这些配置文件。

保存文件并测试配置信息:

sudo nginx -t

如果测试成功,重新加载Nginx:

sudo service nginx reload

为了验证一切正常,你可以检查Apache的日志文件/var/log/apache2,查看对站点test.iofoobar.net的info.php文件GET的请求。使用tail命令查看文件的最后几行,并使用-f 参数监控文件的更改:

sudo tail -f /var/log/apache2/other_vhosts_access.log

现在,在浏览器中访问http://test.io/info.php,然后查看输出的日志内容。我们会看到确实是Apache在应答:

Output    
test.io:80 your_server_ip - - [01/Jul/2016:18:18:34 -0400] "GET /info.php HTTP/1.0" 200 20414 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36"

然后访问每个站点的index.html页面,就不会看到来自Apache的任何日志变化。因为是Nginx正在处理网站的静态文件请求。

When you’re done observing the log file, press CTRL+C to stop tailing it.

观察完日志文件后,请按CTRL+C退出日志。

使用此设置,Apache将无法控制对静态文件的访问。而是Nginx的apache虚拟主机文件,配置对静态文件的访问控制。

总结

现在,我们的Ubuntu服务器上,Nginx服务器管理example.comsample.org两个网站,并且与Apache一起管理foobar.nettest.io这两个网站。尽管Nginx充当Apache的反向代理,但与直接访问Apache服务器是没有差别的,由于Nginx只处理静态请求,反而会更加的高效。

作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/linux/configure-nginx-reverse-proxy-for-apache/

(0)
牛奇网牛奇网
上一篇 2021年3月24日 下午7:20
下一篇 2021年3月27日 下午2:14

相关推荐

发表回复

登录后才能评论
很多新手不知道如何选择外贸独立站主机,这里推荐一款使用量最大,性价比最高的国外独立站主机Hostinger,立即获取优惠