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

2025-06-08

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

作者:Jesin A

作者选择电子前沿基金会作为Write for DOnations计划的一部分接受捐赠

介绍

Apache 和 Nginx 是两个经常与 PHP 配合使用的开源 Web 服务器。当托管多个需求各异的网站时,在同一虚拟机上运行这两个服务器会非常有用。在单个系统上运行两个 Web 服务器的通用解决方案是使用多个 IP 地址或不同的端口号。

可以将同时拥有 IPv4 和 IPv6 地址的服务器配置为使用一种协议为 Apache 网站提供服务,使用另一种协议为 Nginx 网站提供服务,但这目前并不实际,因为 ISP 尚未广泛采用 IPv6。为第二台 Web 服务器设置不同的端口号(例如81或 )8080是另一种解决方案,但使用端口号共享 URL(例如http://example.com:81)并不总是合理或理想的。

在本教程中,您将把 Nginx 配置为 Web 服务器和 Apache 的反向代理 - 所有这些都在一台服务器上进行。

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

我们将在一台服务器上托管四个域名。其中两个域名将由 Nginx 提供服务:(example.com默认虚拟主机)和sample.org。其余两个域名(foobar.nettest.io)将由 Apache 提供服务。我们还将配置 Apache 使用 PHP-FPM 来服务 PHP 应用程序,PHP-FPM 的性能优于mod_php

先决条件

要完成本教程,您需要以下内容:

第1步 - 安装Apache和PHP-FPM

让我们从安装 Apache 和 PHP-FPM 开始。

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

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

sudo apt update

Enter fullscreen mode Exit fullscreen mode

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

sudo apt install apache2 php-fpm

Enter fullscreen mode Exit fullscreen mode

FastCGI Apache 模块在 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

Enter fullscreen mode Exit fullscreen mode

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

第2步 - 配置Apache和PHP-FPM

在此步骤中,我们将更改 Apache 的端口号,8080并将其配置为使用该mod_fastcgi模块与 PHP-FPM 一起使用。重命名 Apache 的ports.conf配置文件:

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

Enter fullscreen mode Exit fullscreen mode

创建一个新ports.conf文件,并将端口设置为8080

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

Enter fullscreen mode Exit fullscreen mode

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

接下来,我们将为 Apache 创建一个虚拟主机文件。<VirtualHost>此文件中的指令将设置为仅在端口 上为网站提供服务8080

禁用默认虚拟主机:

sudo a2dissite 000-default

Enter fullscreen mode Exit fullscreen mode

然后创建一个新的虚拟主机文件,使用现有的默认站点:

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

Enter fullscreen mode Exit fullscreen mode

现在打开新的配置文件:

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

Enter fullscreen mode Exit fullscreen mode

将监听端口更改为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>

Enter fullscreen mode Exit fullscreen mode

保存文件并激活新的配置文件:

sudo a2ensite 001-default

Enter fullscreen mode Exit fullscreen mode

然后重新加载Apache:

sudo systemctl reload apache2

Enter fullscreen mode Exit fullscreen mode

验证 Apache 现在正在监听8080

sudo netstat -tlpn

Enter fullscreen mode Exit fullscreen mode

输出应类似于以下示例,并apache2开启监听8080

OutputActive 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

Enter fullscreen mode Exit fullscreen mode

一旦您验证 Apache 正在监听正确的端口,您就可以配置对 PHP 和 FastCGI 的支持。

第3步 - 配置Apache以使用mod_fastcgi

Apache 默认使用 PHP 页面提供服务mod_php,但需要额外的配置才能与 PHP-FPM 配合使用。

注意:如果您在现有的带有 mod_php 的 LAMP 安装上尝试本教程,请先使用 禁用它sudo a2dismod php7.2

我们将添加一个mod_fastcgi依赖于的配置块mod_actionmod_action默认情况下是禁用的,因此我们首先需要启用它:

sudo a2enmod actions

Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode

创建一个新的配置文件:

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

Enter fullscreen mode Exit fullscreen mode

将以下指令添加到文件以将.php文件请求传递到 PHP-FPM UNIX 套接字:

/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>

Enter fullscreen mode Exit fullscreen mode

保存更改并进行配置测试:

sudo apachectl -t

Enter fullscreen mode Exit fullscreen mode

如果显示Syntax OK,则重新加载 Apache :

sudo systemctl reload apache2

Enter fullscreen mode Exit fullscreen mode

如果您看到警告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 服务。

第4步 - 验证PHP功能

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

/var/www/html/info.php创建包含函数调用的文件phpinfo

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

Enter fullscreen mode Exit fullscreen mode

要在浏览器中查看该文件,请转到http://your_server_ip:8080/info.php。这将提供 PHP 正在使用的配置设置列表。您将看到类似如下的输出:

phpinfo 服务器 API

phpinfo PHP 变量

在页面顶部,检查服务器 API是否显示为FPM/FastCGI。在页面向下约三分之二处,“PHP 变量”部分会显示SERVER_SOFTWARE是 Ubuntu 上的 Apache。这确认该服务mod_fastcgi处于活动状态,并且 Apache 正在使用 PHP-FPM 来处理 PHP 文件。

第5步 - 为Apache创建虚拟主机

让我们为域名foobar.net和创建Apache虚拟主机文件test.io。为此,我们首先为两个站点创建文档根目录,并在这些目录中放置一些默认文件,以便我们轻松测试配置。

首先,创建文档根目录:

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

Enter fullscreen mode Exit fullscreen mode

然后为每个站点创建一个index文件:


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

Enter fullscreen mode Exit fullscreen mode

然后为每个站点创建一个phpinfo()文件,以便我们可以测试 PHP 是否配置正确。


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

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

Enter fullscreen mode Exit fullscreen mode

现在为域创建虚拟主机文件foobar.net

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

Enter fullscreen mode Exit fullscreen mode

将以下代码添加到文件来定义主机:

/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>

Enter fullscreen mode Exit fullscreen mode

该线路AllowOverride All可提供.htaccess支持。

这些只是最基本的指令。有关在 Apache 中设置虚拟主机的完整指南,请参阅如何在 Ubuntu 16.04 上设置 Apache 虚拟主机

保存并关闭文件。然后为 创建一个类似的配置test.io。首先创建文件:

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

Enter fullscreen mode Exit fullscreen mode

然后在文件中添加配置:

/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>

Enter fullscreen mode Exit fullscreen mode

保存文件并退出编辑器。

现在两个 Apache 虚拟主机都已设置完毕,请使用以下a2ensite命令启用站点。这将在目录中创建指向虚拟主机文件的符号链接sites-enabled


sudo a2ensite foobar.net

sudo a2ensite test.io

Enter fullscreen mode Exit fullscreen mode

再次检查 Apache 是否存在配置错误:

sudo apachectl -t

Enter fullscreen mode Exit fullscreen mode

如果没有错误,您将看到“语法正常” 。如果您发现其他错误,请检查配置并重试。

一旦您的配置没有错误,请重新加载 Apache 以应用更改:

sudo systemctl reload apache2

Enter fullscreen mode Exit fullscreen mode

要确认网站正常运行,请http://foobar.net:8080http://test.io:8080浏览器中打开并验证每个网站是否显示其index.html文件。

您将看到以下结果:

foob​​ar.net 索引页

test.io索引页

另外,通过访问每个站点的info.php文件来确保 PHP 正常运行。在浏览器中访问http://foobar.net:8080/info.php和。http://test.io:8080/info.php

您将在每个站点上看到与步骤 4 中相同的 PHP 配置规范列表。

我们现在在 Apache 上托管了两个网站,端口为8080。接下来让我们配置 Nginx。

第6步 - 安装和配置Nginx

在此步骤中,我们将安装 Nginx,并将域example.com和配置sample.org为 Nginx 的虚拟主机。有关在 Nginx 中设置虚拟主机的完整指南,请参阅如何在 Ubuntu 18.04 上设置 Nginx 服务器块(虚拟主机)

使用包管理器安装 Nginx:

sudo apt install nginx

Enter fullscreen mode Exit fullscreen mode

然后删除默认虚拟主机的符号链接,因为我们不再使用它:

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

Enter fullscreen mode Exit fullscreen mode

我们稍后将创建自己的默认站点(example.com)。

现在我们将使用与 Apache 相同的步骤为 Nginx 创建虚拟主机。首先为两个网站创建文档根目录:

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

Enter fullscreen mode Exit fullscreen mode

我们将 Nginx 网站保留在 中/usr/share/nginx,这是 Nginx 默认设置的位置。您可以将它们/var/www/html与 Apache 网站放在一起,但这种分离方式可能有助于您将网站与 Nginx 关联起来。

与 Apache 虚拟主机一样,在设置完成后创建index和文件以进行测试:phpinfo()


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

Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode

Nginx 调用server {. . .}配置文件服务器块的区域。为主虚拟主机创建一个服务器块。example.comdefault_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;
    }
}

Enter fullscreen mode Exit fullscreen mode

保存并关闭文件。现在为Nginx的第二个域名创建一个虚拟主机文件sample.org

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

Enter fullscreen mode Exit fullscreen mode

将以下内容添加到文件:

/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;
    }
}

Enter fullscreen mode Exit fullscreen mode

保存并关闭文件。

然后通过创建指向目录的符号链接来启用两个站点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

Enter fullscreen mode Exit fullscreen mode

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

sudo nginx -t

Enter fullscreen mode Exit fullscreen mode

如果没有错误,则重新加载 Nginx:

sudo systemctl reload nginx

Enter fullscreen mode Exit fullscreen mode

现在通过访问http://example.com/info.phphttp://sample.org/info.php在Web浏览器中访问phpinfo()Nginx虚拟主机的文件。再次查看PHP变量部分。

Nginx PHP 变量

["SERVER_SOFTWARE"]应该显示为nginx,表明这些文件由 Nginx 直接提供服务。["DOCUMENT_ROOT"]应该指向您在此步骤中为每个 Nginx 站点创建的目录。

至此,我们已经安装了 Nginx 并创建了两个虚拟主机。接下来,我们将配置 Nginx 来代理托管在 Apache 上的域名的请求。

第7步 - 为Apache的虚拟主机配置Nginx

让我们在指令中创建一个额外的 Nginx 虚拟主机,其中包含多个域名server_name。对这些域名的请求将被代理到 Apache。

创建一个新的 Nginx 虚拟主机文件以将请求转发到 Apache:

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

Enter fullscreen mode Exit fullscreen mode

添加以下代码块,指定两个 Apache 虚拟主机域的名称,并将其请求代理到 Apache。请记住在 中使用公网 IP 地址proxy_pass

/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;
    }
}

Enter fullscreen mode Exit fullscreen mode

保存文件并通过创建符号链接启用这个新的虚拟主机:

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

Enter fullscreen mode Exit fullscreen mode

测试配置以确保没有错误:

sudo nginx -t

Enter fullscreen mode Exit fullscreen mode

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

sudo systemctl reload nginx

Enter fullscreen mode Exit fullscreen mode

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

通过 Nginx 查看 Apache 的 phpinfo

变量SERVER_SOFTWAREDOCUMENT_ROOT确认此请求已由 Apache 处理。变量HTTP_X_REAL_IPHTTP_X_FORWARDED_FOR由 Nginx 添加,应该显示用于访问 URL 的计算机的公网 IP 地址。

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

第8步 - 安装和配置mod_rpaf

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

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

sudo apt install unzip build-essential apache2-dev

Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode

解压下载的文件:

unzip stable.zip

Enter fullscreen mode Exit fullscreen mode

切换到包含以下文件的新目录:

cd mod_rpaf-stable

Enter fullscreen mode Exit fullscreen mode

编译并安装模块:


make

sudo make install

Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode

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

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

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

Enter fullscreen mode Exit fullscreen mode

保存文件并退出编辑器。

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

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

Enter fullscreen mode Exit fullscreen mode

将以下代码块添加到 configure 中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>

Enter fullscreen mode Exit fullscreen mode

以下是每个指令的简要说明。更多信息请参阅mod_rpaf README文件。

  • RPAF_Header - 用于客户端真实 IP 地址的标头。
  • RPAF_ProxyIPs - 用于调整 HTTP 请求的代理 IP。
  • RPAF_SetHostName - 更新 vhost 名称ServerNameServerAlias运行。
  • RPAF_SetHTTPS -HTTPS根据中包含的值设置环境变量X-Forwarded-Proto
  • RPAF_SetPort - 设置SERVER_PORT环境变量。当 Apache 位于 SSL 代理之后时很有用。

保存rpaf.conf并启用模块:

sudo a2enmod rpaf

Enter fullscreen mode Exit fullscreen mode

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

sudo apachectl -t

Enter fullscreen mode Exit fullscreen mode

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

sudo systemctl reload apache2

Enter fullscreen mode Exit fullscreen mode

在浏览器中访问phpinfo()页面http://foobar.net/info.php并检查PHP 变量部分。REMOTE_ADDR变量现在也将是您本地计算机的公网 IP 地址。http://test.io/info.php

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

第 9 步 — 使用 Let's Encrypt 设置 HTTPS 网站(可选)

在此步骤中,我们将为 Apache 上托管的两个域配置 TLS/SSL 证书。我们将通过 [Let's Encrypt]( https://letsencrypt.org ) 获取证书。Nginx 支持 SSL 终止,因此我们无需修改 Apache 的配置文件即可设置 SSL。该mod_rpaf模块确保在 Apache 上设置所需的环境变量,以使应用程序能够在 SSL 反向代理后无缝运行。

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

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

Enter fullscreen mode Exit fullscreen mode

修改文件,使其看起来像这样,并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;
        }
    }

Enter fullscreen mode Exit fullscreen mode

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

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

sudo add-apt-repository ppa:certbot/certbot

Enter fullscreen mode Exit fullscreen mode

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

sudo apt update

Enter fullscreen mode Exit fullscreen mode

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

sudo apt install python-certbot-nginx

Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode

此命令告诉 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 fullscreen mode Exit fullscreen mode

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

现在对第二个域执行命令:

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

Enter fullscreen mode Exit fullscreen mode

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

phpinfo ssl

查看PHP 变量部分。变量SERVER_PORT已设置为443HTTPS已设置为on,就像直接通过 HTTPS 访问 Apache 一样。设置这些变量后,PHP 应用程序无需进行特殊配置即可在反向代理后运行。

现在让我们禁用对 Apache 的直接访问。

第 10 步 — 阻止直接访问 Apache(可选)

由于 Apache 正在监听8080公网 IP 地址上的端口,因此任何人都可以访问它。您可以通过在防火墙规则集中添加以下 IPtables 命令来阻止它。

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

Enter fullscreen mode Exit fullscreen mode

请务必使用服务器的 IP 地址代替红色示例。一旦8080防火墙阻止了端口,请测试 Apache 是否无法访问。打开 Web 浏览器,尝试访问端口 上的某个 Apache 域名8080。例如:http://example.com: 8080

浏览器应该会显示“无法连接”或“网页不可用”的错误消息。tcp-reset启用 IPtables 选项后,外部人员将看不到端口8080与没有任何服务的端口之间的区别。

注意:默认情况下,IPtables 规则在系统重启后会失效。有多种方法可以保留 IPtables 规则,但最简单的方法是使用iptables-persistentUbuntu 的仓库。阅读本文,了解更多关于如何配置 IPtables 的信息。

现在让我们配置 Nginx 为 Apache 站点提供静态文件。

第11步 - 使用Nginx提供静态文件(可选)

当 Nginx 代理 Apache 域名的请求时,它会将该域名的每个文件请求都发送到 Apache。Nginx 在提供图像、JavaScript 和样式表等静态文件方面比 Apache 更快。因此,让我们配置 Nginx 的apache虚拟主机文件,使其直接提供静态文件,但将 PHP 请求发送到 Apache。

/etc/nginx/sites-available/apache在编辑器中打开该文件:

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

Enter fullscreen mode Exit fullscreen mode

您需要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;
    }
}

Enter fullscreen mode Exit fullscreen mode

如果您还希望使用 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;
}

Enter fullscreen mode Exit fullscreen mode

try_files指令使 Nginx 在文档根目录中查找文件并直接提供服务。如果文件带有.php扩展名,则请求将传递给 Apache。即使在文档根目录中找不到该文件,请求也会传递给 Apache,以便永久链接等应用程序功能能够正常运行。

警告:该指令非常重要;这会阻止 Nginx 提供包含敏感信息的location ~ /\.htApache 配置文件的内容,例如.htaccess和。.htpasswd

保存文件并执行配置测试:

sudo nginx -t

Enter fullscreen mode Exit fullscreen mode

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

sudo service nginx reload

Enter fullscreen mode Exit fullscreen mode

为了验证一切是否正常,您可以检查 Apache 的日志文件,/var/log/apache2并查看和文件GET的请求。使用命令查看文件的最后几行,并使用开关监视文件的更改:info.phptest.iofoobar.nettail-f

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

Enter fullscreen mode Exit fullscreen mode

现在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"

Enter fullscreen mode Exit fullscreen mode

然后访问index.html每个站点的页面,您将看不到任何来自 Apache 的日志条目。Nginx 正在为它们提供服务。

观察完日志文件后,按CTRL+C停止跟踪。

使用此设置,Apache 将无法限制对静态文件的访问。静态文件的访问控制需要在 Nginx 的apache虚拟主机文件中配置,但这超出了本教程的范围。

结论

现在,您已经拥有一台 Ubuntu 服务器,其中 Nginx 负责服务example.comsample.org,Apache 负责服务foobar.nettest.io。虽然 Nginx 充当 Apache 的反向代理,但 Nginx 的代理服务是透明的,与 Apache 域名的连接看起来就像直接由 Apache 本身提供的。您可以使用此方法来提供安全的静态网站。

在 DigitalOcean 上构建、测试和部署新应用——开发者及其团队钟爱的一体化云平台。新用户即可免费获得 100 美元账户信用额度:do.co/devto

知识共享许可
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行授权

鏂囩珷鏉ユ簮锛�https://dev.to/digitalocean/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-1804-server-2eib
PREV
如何在 DigitalOcean Kubernetes 上设置 Prometheus、Grafana 和 Alertmanager 监控堆栈
NEXT
如何使用 React 和 TypeScript 构建客户列表管理应用程序