AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 970180
Accepted
James Newton
James Newton
Asked: 2017-10-29 02:15:38 +0800 CST2017-10-29 02:15:38 +0800 CST 2017-10-29 02:15:38 +0800 CST

使用 nginx 作为反向代理运行 Apache2

  • 772

我的目标是从新的 Ubuntu 16.04 服务器提供安全的电子邮件服务。为了做到这一点,我想设置 Apache2 运行 nginx 作为其反向代理。

我的直接问题是 Apache 无法启动,声称没有分配 SSL 证书(请参阅本问题末尾的终端输出),即使我(尚未)尝试使用 HTTPS。

我的问题:

  • 如果 Apache 在本地端口上运行,nginx 知道任何 SSL 证书还不够吗?
  • 我需要做什么才能让 Apache 开始在本地地址监听?

这是我对我需要做的事情的理解。如果您能指出我的理解错误的地方,我将不胜感激。

  • 安装 nginx 和 Apache2
  • 创建一个 nginx 服务器块以:
    • 监听端口 80(和 443)
    • (将流量从 80 端口重定向到 443 端口)
    • (处理 SSL 认证)
    • 将 PHP 文件的任何请求转发到 127.0.0.1:8080 的 Apache
  • 将 Apache 设置为:
    • 只监听 8080 端口,不监听 443 端口
    • 对任何 SSL 认证一无所知

我喜欢一次处理一个问题,所以我目前搁置 SSL 证书的问题,并试图让 Apache 使用 nginx 作为其反向代理仅在端口 80 上运行。


以下是我现有的配置文件:

nginx

$ sudo nano /etc/nginx/sites-available/webmail

server {
  listen   80;

  root /var/www/webmail/web;
  index index.php index.html index.htm;

  server_name webmail.mydomain.com;

  # Look for...
  # * the exact path
  # * a default (index.*) file, considering the path to be a directory
  # ... and if that fails:
  # * get the index.php script at the root to deal with the request

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

  # If the chosen path leads to a PHP fie:
  # * forward the request to Apache running at 127.0.0.1:8080
  # * after having modified certain headers

  location ~ \.php$ {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080;
  }

  # If there are any Apache HT files in the chosen directory
  # ignore any direct requests for them

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

阿帕奇2

$ sudo nano /etc/apache2/sites-available/webmail.conf 

<VirtualHost 127.0.0.1:8080>
        ServerName webmail.mydomain.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/webmail/web
        <Directory "/var/www/webmail/web">
            Options FollowSymLinks
            AllowOverride All
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Apache2 端口

$ sudo nano /etc/apache2/ports.conf

Listen 127.0.0.1:8080

#<IfModule ssl_module>
#       Listen 443
#</IfModule>

#<IfModule mod_gnutls.c>
#       Listen 443
#</IfModule>

请注意,我没有明确要求 Apache 监听端口 443。在其他配置文件中是否有另一个地方告诉 Apache 监听端口 443?


这是我的占位符 PHP 和 HTML 文件,在它们的预期位置:

索引.php

$ sudo nano /var/www/webmail/web/index.php

<?php
  echo "PHP from /var/www/webmail/web/index.php\n";
?>

索引.html

$ sudo nano /var/www/webmail/web/index.html

<html>
  HTML from /var/www/webmail/web/index.html
</html>

在浏览器中,对http://webmail.mydomain.com/index.html的请求按预期成功,但对http://webmail.mydomain.com/index.php的请求导致文件被下载,而不是执行.


这是我尝试启动 Apache2 时发生的情况:

$ sudo /etc/init.d/apache2 restart
[ ok ] Restarting apache2 (via systemctl): apache2.service.

$ sudo service apache2 status
* apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           `-apache2-systemd.conf
   Active: inactive (dead) since <Time>; 3s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 16059 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
  Process: 16043 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)

systemd[1]: Starting LSB: Apache2 web server...
apache2[16043]:  * Starting Apache httpd web server apache2
apache2[16043]: Action 'start' failed.
apache2[16043]: The Apache error log may have more information.
apache2[16043]:  *
apache2[16059]:  * Stopping Apache httpd web server apache2
apache2[16059]:  *
systemd[1]: Started LSB: Apache2 web server.

这是error.log的内容:

$ sudo nano /var/log/apache2/error.log

[ssl:emerg] [pid 15943] AH02572: Failed to configure at least one certificate and key for myhostname.hostingservice.com:443
[ssl:emerg] [pid 15943] SSL Library Error: error:140A80B1:SSL routines:SSL_CTX_check_private_key:no certificate assigned
[ssl:emerg] [pid 15943] AH02311: Fatal error initialising mod_ssl, exiting. See /var/log/apache2/error.log for more information

(这在 var/log/apache2/error.log 中,没有更多信息。)

我错过了什么?


注意:此服务器的主要目的是提供 Meteor 应用程序。我选择使用 Phusion Passenger 和 Nginx 来做这件事。除了电子邮件服务之外,Apache 还将用于提供 WordPress 站点。

apache2
  • 1 1 个回答
  • 1107 Views

1 个回答

  • Voted
  1. Best Answer
    James Newton
    2017-11-02T11:49:33+08:002017-11-02T11:49:33+08:00

    我现在已经开始工作了。我忽略了删除/etc/apache2/sites-disabled/default-ssl.conf,所以 Apache 试图运行一个不需要的安全站点。

    我随后曾经sudo certbot --nginx获得过 Nginx 的证书,现在一切正常。

    • 1

相关问题

  • 如何使用 fastcgi 和简单的测试脚本设置 apache?

  • 新贵监督的 Apache 初始化脚本?

  • http://localhost/ 不工作

  • 访问启用的虚拟主机时出现 403 禁止错误

  • 如何设置一台机器来将我的网站托管到世界各地 - 使用我自己的网址?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve