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

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

介绍

Apache和Nginx是两种常用于PHP的流行的开源Web服务器。 当托管具有不同要求的多个网站时,在同一虚拟机上运行它们会很有用。 在单个系统上运行两个Web服务器的一般解决方案是使用多个IP地址或不同的端口号。

具有IPv4和IPv6地址的服务器可以配置为在一个协议和Nginx站点上为Apache站点提供服务,但这目前还不实用,因为ISP的IPv6采用仍然不普遍。 为第二个Web服务器设置不同的端口号(如818080是另一种解决方案,但是使用端口号(例如http://example.com:81 )共享URL并不总是合理或理想的。

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

根据Web应用程序的不同,可能需要更改代码以保持Apache反向代理感知,尤其是在配置SSL站点时。 为了避免这种情况,您将安装一个名为mod_rpaf的Apache模块,它重写某些环境变量,以便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

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

sudo apt install apache2 php-fpm

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

接下来,让我们更改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

创建一个端口设置为8080的新ports.conf文件:

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

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

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

禁用默认虚拟主机:

sudo a2dissite 000-default

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

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

然后重新加载Apache:

sudo systemctl reload apache2

验证Apache现在正在监听8080

sudo netstat -tlpn

输出应该类似于以下示例, 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

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

第3步 - 配置Apache以使用mod_fastcgi

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

注意:如果您在使用mod_php的现有LAMP安装上尝试本教程,请首先使用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 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>

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

sudo apachectl -t

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

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. 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功能

让我们确保PHP的工作原理是创建一个phpinfo()文件并从Web浏览器访问它。

创建文件/var/www/html/info.php ,其中包含对phpinfo函数的调用:

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

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

phpinfo Server API

phpinfo PHP变量

在页面顶部,检查Server API是否显示FPM / FastCGI 大约三分之二的页面, PHP变量部分将告诉你SERVER_SOFTWARE是Ubuntu上的Apache。 这些确认mod_fastcgi是活动的,而Apache正在使用PHP-FPM来处理PHP文件。

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

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

首先,创建文档根目录:

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

然后为每个站点创建一个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

然后为每个站点创建一个phpinfo()文件,以便我们可以测试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支持。

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

保存并关闭文件。 然后为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目录中创建指向虚拟主机文件的符号链接:

sudo a2ensite foobar.net
sudo a2ensite test.io

再次检查Apache的配置错误:

sudo apachectl -t

如果没有错误,您将看到语法OK 如果您看到其他内容,请查看配置并重试。

重新加载Apache以在配置无错误后应用更改:

sudo systemctl reload apache2

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

您将看到以下结果:

foob​​ar.net索引页面

test.io索引页面

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

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

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

第6步 - 安装和配置Nginx

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

使用包管理器安装Nginx:

sudo apt install nginx

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

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

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

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

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

我们将Nginx网站保留在/usr/share/nginx ,这是Nginx默认需要的地方。 您可以将它们放在带有Apache站点的/var/www/html ,但这种分离可以帮助您将站点与Nginx相关联。

正如您对Apache的虚拟主机所做的那样,在安装完成后创建indexphpinfo()文件以进行测试:

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

Nginx调用server {. . .} server {. . .} 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;
    }
}

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

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:

sudo systemctl reload nginx

现在,访问http://example.com/info.phphttp://sample.org/info.php ,在Web浏览器中访问Nginx虚拟主机的phpinfo()文件。 再看一下PHP Variables部分。

Nginx PHP变量

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

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

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

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

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

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

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

在/ etc / nginx的/网站可用/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

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

sudo nginx -t

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

sudo systemctl reload nginx

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

通过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

在此步骤中,您将安装名为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文件,该文件将包含rpaf.conf的配置指令:

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 自述文件。

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

保存rpaf.conf并启用该模块:

sudo a2enmod rpaf

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

sudo apachectl -t

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

sudo systemctl reload apache2

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

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

第9步 - 使用Let加密设置HTTPS网站(可选)

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

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

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

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

在/ etc / nginx的/网站可用/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

然后用apt安装Certbot的Nginx包:

sudo apt install python-certbot-nginx

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

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

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

如果这是您第一次运行certbot ,系统将提示您输入电子邮件地址并同意服务条款。 执行此操作后, certbot将与Let的加密服务器通信,然后运行质询以验证您是否控制了您要为其申请证书的域。

接下来,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 ,就像Apache直接通过HTTPS访问一样。 通过设置这些变量,PHP应用程序不必专门配置为在反向代理后面工作。

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

第10步 - 阻止对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上访问Apache的一个域名。 例如:http:// example.com:8080

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

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

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

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

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

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

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

您需要为每个服务器块添加两个额外的location块,以及修改现有的location部分。 此外,您需要告诉Nginx在哪里可以找到每个站点的静态文件。

如果您决定不使用SSL和TLS证书,请修改您的文件,使其如下所示:

在/ etc / nginx的/网站可用/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的/网站可用/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提供包含敏感信息的.htaccess.htpasswd等Apache配置文件的内容。

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

sudo nginx -t

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

sudo service nginx reload

要验证一切正常,您可以检查/var/log/apache2 Apache日志文件,并查看test.iofoobar.netinfo.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正在为他们服务。

完成观察日志文件后,按CTRL+C停止拖尾。

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

结论

您现在有一个Ubuntu服务器,其中Nginx服务于example.comsample.org ,以及Apache服务foobar.nettest.io 虽然Nginx充当Apache的反向代理,但Nginx的代理服务是透明的,与Apache域的连接似乎直接来自Apache本身。 您可以使用此方法来提供安全和静态站点。

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏