为什么我的apache2设置总是导致301 Moved Permanently

时间:2014-05-13 05:19:02

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

我最近在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),我可以在没有问题的情况下执行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。我总是得到&#34; 301永久移动&#34;状态。

[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.

如果有任何兴趣,我可以启用以下mod并提前创建一个自签名证书(我不知道它是如何相关的):

[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
.
.

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

1 个答案:

答案 0 :(得分: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 app的force_ssl设置,http - &gt; https://anything.xorg总是发生。