首页 > 解决方案 > Apache HTTPD:如何正确设置虚拟主机

问题描述

我已经在我的 Mac 上安装了Apache httpd并且“它可以工作”。

现在我需要配置一个虚拟主机,以便在其前面使用 httpd 作为反向代理来公开我的应用程序(Java Spring)。

这就是我在/usr/local/etc/httpd/extra/httpd-vhosts.conf文件中的内容

<VirtualHost *:443>
   ServerName my.domain.it:443
    SSLEngine on
    SSLCertificateFile /path/to/cert.crt
    SSLCertificateKeyFile /path/to/cert.key
    ProxyPreserveHost On
    ProxyPass / http://localhost:8080/myapp
    ProxyPassReverse / http://localhost:8080/myapp
</VirtualHost>

/etc/hosts我以这种方式映射到服务器地址:

127.0.0.1       my.domain.it

并且 Tomcat 连接器配置是:

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" proxyPort="443" scheme="https"/>

如果我使用 Eclipse 运行应用程序,它会在 http://localhost:8080/myapp/ 正确响应,但如果我尝试调用https://my.domain.it/myapp/它不起作用,Google Chrome 会告诉我: “无法访问此站点”。

我的配置有什么问题?

PS httpd Apache 实例配置为监听 80 端口

#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80

谢谢。

编辑:已解决不幸的是,我无法使用 Apache Httpd 解决,但现在使用 Nginx 可以解决问题。感谢您的回答

标签: apachereverse-proxyvirtualhosthttpd.conf

解决方案


在 mod_ssl.conf (文件名可能不同)中,您应该有以下行

Listen 443

它告诉 apache 侦听端口 443(当您安装 mod_ssl 以便能够通过 HTTPS 公开您的站点时,它包含在配置文件中)。同一个配置文件应该包含所有关于 TLS 的共享配置。

如果 apache 正在运行,您可以使用 netstat 检查端口 443 是否正在侦听:

netstat -an | grep -i list

输出应该包括端口 80 和 443(以及其他一些根据服务器上运行的服务,例如 ssh)。

如果端口显示在列表中,下一步是检查 apache 日志是否有错误。


推荐阅读