【问题标题】:Why does my apache2 setup always result in 301 Moved Permanently为什么我的 apache2 设置总是导致 301 Moved Permanently
【发布时间】:2014-06-30 15:48:19
【问题描述】:

我最近在 VPS 上设置了一个 apache2 (2.4) 服务器,试图让它运行带有 Phusion Passenger 的 rails 应用程序。它几乎是 Ubuntu 14.04 和 apache2 的全新安装。我正在尝试通过启用站点的方式配置 VirtualHost 条目。一旦我运行 a2ensites,对http://my.site 的任何访问都会自动重定向到https://my.site。我在任何地方都没有任何 .htaccess 文件。以下说明了我遇到的问题。

这里是 /etc/apache2/,目前还没有启用虚拟主机站点

[appuser@prod apache2]$  ls -l
total 84
-rw-r--r-- 1 root root  7115 Jan  7 08:23 apache2.conf
drwxr-xr-x 2 root root  4096 May  8 21:46 conf-available
drwxr-xr-x 2 root root  4096 May  8 21:46 conf-enabled
-rw-r--r-- 1 root root  1782 Jan  3 09:48 envvars
-rw-r--r-- 1 root root 31063 Jan  3 09:48 magic
drwxr-xr-x 2 root root 12288 May  8 22:11 mods-available
drwxr-xr-x 2 root root  4096 May 13 00:28 mods-enabled
-rw-r--r-- 1 root root   320 Jan  7 08:23 ports.conf
drwxr-xr-x 2 root root  4096 May 12 23:37 sites-available
drwxr-xr-x 2 root root  4096 May 13 00:40 sites-enabled
drwxr-xr-x 2 root root  4096 May  8 22:24 ssl
[appuser@prod apache2]$  ls -l sites-enabled/
total 0

这里是 /etc/sites-available/bulletin.vhost.conf:

[appuser@prod apache2]$  cat sites-available/bulletin.vhost.conf
#NameVirtualHost *:80
#ServerName bulletin.xorg

<VirtualHost *:80>
  ServerName bulletin.xorg:80
  DocumentRoot /home/appuser/www/bulletin/public
  <Directory /home/appuser/www/bulletin/public>
    AllowOverride all
    Options -MultiViews
    Require all granted
    PassengerRuby /home/appuser/.rvm/gems/ruby-1.9.3-p545/wrappers/ruby
  </Directory>

</VirtualHost>

如果不启用此站点 (bulletin.xorg),我可以毫无问题地执行 wget http://xorg:

[appuser@prod apache2]$  wget http://xorg
--2014-05-13 00:53:46--  http://xorg/
Resolving xorg (xorg)... 127.0.0.1
Connecting to xorg (xorg)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 739 [text/html]

现在我正在启用这个虚拟主机:

[appuser@prod apache2]$  sudo a2ensite bulletin.vhost
[sudo] password for appuser: 
Enabling site bulletin.vhost.
To activate the new configuration, you need to run:
  service apache2 reload
[appuser@prod apache2]$ )`reload': sudo service apache2 reload
 * Reloading web server apache2
[appuser@prod apache2]$  sudo service apache2 restart
 * Restarting web server apache2
AH00558: apache2: Could not reliably determine the server's fully 
  qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to
  suppress this message

现在我无法访问 http://xorg 或 http://bulletin.xorg。我总是得到“301 永久移动”状态。

[appuser@prod apache2]$  wget http://xorg
--2014-05-13 00:58:45--  http://xorg/
Resolving xorg (xorg)... 127.0.0.1
Connecting to xorg (xorg)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://xorg/ [following]
--2014-05-13 00:58:45--  https://xorg/
Connecting to xorg (xorg)|127.0.0.1|:443... connected.
OpenSSL: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
Unable to establish SSL connection.

[appuser@prod apache2]$  wget http://bulletin.xorg
--2014-05-13 01:01:18--  http://bulletin.xorg/
Resolving bulletin.xorg (bulletin.xorg)... 127.0.0.1
Connecting to bulletin.xorg (bulletin.xorg)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://bulletin.xorg/ [following]
--2014-05-13 01:01:19--  https://bulletin.xorg/
Connecting to bulletin.xorg (bulletin.xorg)|127.0.0.1|:443... connected.
OpenSSL: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
Unable to establish SSL connection.

如果有任何兴趣,我启用了以下模块并在之前创建了一个自签名 ssl 证书(但我看不出它是如何相关的):

[appuser@prod apache2]$  ls -l ssl/
total 8
-rw-r--r-- 1 root root 1350 May  8 22:24 redmine.crt
-rw-r--r-- 1 root root 1704 May  8 22:24 redmine.key

[appuser@prod apache2]$  ls -l mods-enabled/
.
.
lrwxrwxrwx 1 root root 30 May 13 00:28 rewrite.load -> ../mods-available/rewrite.load
.
.
lrwxrwxrwx 1 root root 26 May  8 21:47 ssl.conf -> ../mods-available/ssl.conf
lrwxrwxrwx 1 root root 26 May  8 21:47 ssl.load -> ../mods-available/ssl.load
.
.

我真的很困惑。提前致谢。

【问题讨论】:

    标签: ubuntu apache2 http-status-code-301 http-redirect


    【解决方案1】:

    我终于明白了。总而言之,应用程序服务器(Phusion Passenger)采用了我的应用程序需要 ssl 的生产设置。

    这是一个 Rails 3.2 应用程序,它有以下条目

    config/environments/production.rb:  
                    # Force all access to the app over SSL, use 
                    # Strict-Transport-Security, and use secure cookies.
    config/environments/production.rb:  
                    config.force_ssl = true
    

    所以不是 apache2 重写 mod 强制 301 Moved Permanent 状态,而是我的应用程序服务器这样做了。另外,因为我在 apache2 中只有一个 VirtualHost,并且我禁用了默认站点(根据 apache2.4 安装附带的 000-default.conf 配置)。对 *.xorg 的任何访问都会失败并由第一个(也是唯一的)VirtualHost(即 bulletin.xorg)提供服务。然后由于 Rails 中的 bulletin.xorg 应用程序的 force_ssl 设置,http -> https://anything.xorg 总是发生。

    【讨论】:

    • 感谢您回答您自己的问题。帮助我解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2017-06-29
    • 2017-06-13
    • 1970-01-01
    • 2014-12-30
    相关资源
    最近更新 更多