红色警报 [AWS]

Red alert [AWS]

简而言之,我希望能够使用 htaccess 以便我可以执行各种重写命令。我正在使用 AWS elasticbeanstalk,下面是我在尝试让 htaccess 工作时遇到的困难。

我的实例上传失败,收到红色提示如下图:

基本上我已经覆盖所有使用 .ebextensions 以允许 htaccess 使用以下代码:

files:          
  "/etc/httpd/conf.d/enable_mod_rewrite.conf": 
     mode: "644"
     owner: root
     group: root
     content: |
       AllowOverride All

但这不是导致问题的原因。运行问题是这样的:

RewriteEngine On

位于 htaccess 中。几乎就像它不能接受正在打开的重写引擎。

更新:

在/var/log/eb-activity.log:

Command CMD-TailLogs succeeded.
[2015-07-10T10:25:09.784Z] INFO  [25589] - [CMD-TailLogs] : Starting activity...
[2015-07-10T10:25:10.206Z] INFO  [25589] - [CMD-TailLogs/AddonsBefore] : Starting activity...
[2015-07-10T10:25:10.207Z] INFO  [25589] - [CMD-TailLogs/AddonsBefore] : Completed activity.
[2015-07-10T10:25:10.207Z] INFO  [25589] - [CMD-TailLogs/TailLogs] : Starting activity...
[2015-07-10T10:25:10.207Z] INFO  [25589] - [CMD-TailLogs/TailLogs/TailLogs] : Starting activity...

在/var/log/eb-commandprocessor.log

   [2015-07-10T10:37:19.644Z] DEBUG [25873] : Checking if the command processor should execute...
[2015-07-10T10:37:19.645Z] DEBUG [25873] : Checking whether the command is applicable to instance (i-80bbbd77)..
[2015-07-10T10:37:19.645Z] INFO  [25873] : Command is applicable to this instance (i-80bbbd77)..
[2015-07-10T10:37:19.645Z] DEBUG [25873] : Checking if the received command stage is valid..
[2015-07-10T10:37:19.645Z] INFO  [25873] : No stage_num in command. Valid stage..
[2015-07-10T10:37:19.645Z] INFO  [25873] : Command processor should execute command.
[2015-07-10T10:37:19.645Z] DEBUG [25873] : Storing current stage..
[2015-07-10T10:37:19.645Z] DEBUG [25873] : Stage_num does not exist. Not saving null stage. Returning..
[2015-07-10T10:37:19.646Z] INFO  [25873] : Executing command: CMD-TailLogs...
[2015-07-10T10:37:19.646Z] DEBUG [25873] : Reading config file: /etc/elasticbeanstalk/.aws-eb-stack.properties
[2015-07-10T10:37:19.647Z] DEBUG [25873] : Refreshing metadata..
[2015-07-10T10:37:20.061Z] DEBUG [25873] : Refreshed environment metadata.
[2015-07-10T10:37:20.061Z] DEBUG [25873] : Retrieving metadata for key: AWS::ElasticBeanstalk::Ext||_ContainerConfigFileContent||commands..
[2015-07-10T10:37:20.062Z] DEBUG [25873] : Retrieving metadata for key: AWS::ElasticBeanstalk::Ext||_API||_Commands..
[2015-07-10T10:37:20.064Z] INFO  [25873] : Found enabled addons: ["logpublish"].
[2015-07-10T10:37:20.066Z] INFO  [25873] : Updating Command definition of addon logpublish.
[2015-07-10T10:37:20.066Z] DEBUG [25873] : Loaded definition of Command CMD-TailLogs.
[2015-07-10T10:37:20.066Z] INFO  [25873] : Executing command CMD-TailLogs activities...
[2015-07-10T10:37:20.066Z] DEBUG [25873] : Setting environment variables..
[2015-07-10T10:37:20.066Z] INFO  [25873] : Running AddonsBefore for command CMD-TailLogs...
[2015-07-10T10:37:20.067Z] DEBUG [25873] : Running stages of Command CMD-TailLogs from stage 0 to stage 0...
[2015-07-10T10:37:20.067Z] INFO  [25873] : Running stage 0 of command CMD-TailLogs...
[2015-07-10T10:37:20.067Z] DEBUG [25873] : Loaded 1 actions for stage 0.
[2015-07-10T10:37:20.067Z] INFO  [25873] : Running 1 of 1 actions: TailLogs...

更新 3 我的 .htaccess 文件的内容

RewriteEngine On
RewriteCond %{THE_REQUEST} /searchPage.php\?crs_category=([^\s]+) [NC]
RewriteRule ^ category/%1? [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/]+)/?$  searchPage.php?crs_category= [QSA,L,NC]

AllowOverride is valid only in <Directory> sections specified without regular expressions, not in <Location>, <DirectoryMatch> or <Files> sections.

Source: Apache Core Module

您将 AllowOverride 放入 /etc/httpd/conf.d/enable_mod_rewrite.conf。可能会导致Apache启动失败。

我刚刚尝试使用默认的 Elastic Beanstalk 示例 PHP 配置(参见 Supported Platforms and Sample Application)。我还将两个新文件添加到示例 PHP app:

phpinfo.php

<?php
    phpinfo();

.htaccess

RewriteEngine on
RewriteRule ^/?info.html$ phpinfo.php [L]

结果是:

默认情况下,Elastic Beanstalk PHP 环境已经将 mod_rewriteAllowOverride All 启用到 PHP 应用程序目录。

我也尝试添加您的 .ebextensions 脚本(添加)。

files:             "/etc/httpd/conf.d/enable_mod_rewrite.conf": 
     mode: "644"
     owner: root
     group: root
     content: |
       AllowOverride All

部署后Apache服务器启动失败,无法上网。这是由错误的配置引起的。错误是我在回答之前说过。

解决方案可能是删除 enable_mod_rewrite.conf 并使用默认环境。

更新

我尝试使用简单的 searchPage.php 和你的 .htaccess,但稍作修改,你在 ^ 和 [=33= 之间多了一个 space ] 在第二行。

.htaccess

RewriteEngine On
RewriteCond %{THE_REQUEST} searchPage.php\?crs_category=([^\s]+) [NC]
RewriteRule ^category/? [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/]+)/?$ searchPage.php?crs_category= [QSA,L,NC]

searchPage.php

<?php
    header('Content-Type: text/plain');
    print_r($_SERVER);
    print_r($_REQUEST);

我尝试卷曲:

$ curl -v 'http://default-environment-zjqxqumicq.elasticbeanstalk.com/searchPage.php?crs_category=test'

* Hostname was NOT found in DNS cache
*   Trying 54.173.237.116...
* Connected to default-environment-zjqxqumicq.elasticbeanstalk.com (54.173.237.116) port 80 (#0)
> GET /searchPage.php?crs_category=test HTTP/1.1
> User-Agent: curl/7.35.0
> Host: default-environment-zjqxqumicq.elasticbeanstalk.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/plain;charset=UTF-8
< Date: Tue, 21 Jul 2015 13:27:56 GMT
* Server Apache is not blacklisted
< Server: Apache
< Content-Length: 1490
< Connection: keep-alive
< 
Array
(
    [PHP_MEMORY_LIMIT] => 256M
    [PHP_MAX_EXECUTION_TIME] => 60
    [PHP_DISPLAY_ERRORS] => Off
    [PHP_COMPOSER_OPTIONS] => 
    [PHP_ALLOW_URL_FOPEN] => On
    [PHP_ZLIB_OUTPUT_COMPRESSION] => Off
    [PHP_DOCUMENT_ROOT] => /
    [PHP_DATE_TIMEZONE] => UTC
    [HTTP_HOST] => default-environment-zjqxqumicq.elasticbeanstalk.com
    [HTTP_ACCEPT] => */*
    [HTTP_USER_AGENT] => curl/7.35.0
    [HTTP_X_FORWARDED_FOR] => 149.129.122.128
    [HTTP_X_FORWARDED_PORT] => 80
    [HTTP_X_FORWARDED_PROTO] => http
    [HTTP_CONNECTION] => keep-alive
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
    [SERVER_SIGNATURE] => 
    [SERVER_SOFTWARE] => Apache
    [SERVER_NAME] => default-environment-zjqxqumicq.elasticbeanstalk.com
    [SERVER_ADDR] => 172.31.5.238
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 172.31.30.235
    [DOCUMENT_ROOT] => /var/www/html/
    [REQUEST_SCHEME] => http
    [CONTEXT_PREFIX] => 
    [CONTEXT_DOCUMENT_ROOT] => /var/www/html/
    [SERVER_ADMIN] => root@localhost
    [SCRIPT_FILENAME] => /var/www/html/searchPage.php
    [REMOTE_PORT] => 24890
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => crs_category=test
    [REQUEST_URI] => /searchPage.php?crs_category=test
    [SCRIPT_NAME] => /searchPage.php
    [PHP_SELF] => /searchPage.php
    [REQUEST_TIME_FLOAT] => 1437485276.635
    [REQUEST_TIME] => 1437485276
)
Array
(
    [crs_category] => test
)
* Connection #0 to host default-environment-zjqxqumicq.elasticbeanstalk.com left intact

$ curl -v 'http://default-environment-zjqxqumicq.elasticbeanstalk.com/category/test'                   

* Hostname was NOT found in DNS cache
*   Trying 52.5.119.104...
* Connected to default-environment-zjqxqumicq.elasticbeanstalk.com (52.5.119.104) port 80 (#0)
> GET /category/test HTTP/1.1
> User-Agent: curl/7.35.0
> Host: default-environment-zjqxqumicq.elasticbeanstalk.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/plain;charset=UTF-8
< Date: Tue, 21 Jul 2015 13:30:13 GMT
* Server Apache is not blacklisted
< Server: Apache
< Content-Length: 1918
< Connection: keep-alive
< 
Array
(
    [REDIRECT_PHP_MEMORY_LIMIT] => 256M
    [REDIRECT_PHP_MAX_EXECUTION_TIME] => 60
    [REDIRECT_PHP_DISPLAY_ERRORS] => Off
    [REDIRECT_PHP_COMPOSER_OPTIONS] => 
    [REDIRECT_PHP_ALLOW_URL_FOPEN] => On
    [REDIRECT_PHP_ZLIB_OUTPUT_COMPRESSION] => Off
    [REDIRECT_PHP_DOCUMENT_ROOT] => /
    [REDIRECT_PHP_DATE_TIMEZONE] => UTC
    [REDIRECT_STATUS] => 200
    [PHP_MEMORY_LIMIT] => 256M
    [PHP_MAX_EXECUTION_TIME] => 60
    [PHP_DISPLAY_ERRORS] => Off
    [PHP_COMPOSER_OPTIONS] => 
    [PHP_ALLOW_URL_FOPEN] => On
    [PHP_ZLIB_OUTPUT_COMPRESSION] => Off
    [PHP_DOCUMENT_ROOT] => /
    [PHP_DATE_TIMEZONE] => UTC
    [HTTP_HOST] => default-environment-zjqxqumicq.elasticbeanstalk.com
    [HTTP_ACCEPT] => */*
    [HTTP_USER_AGENT] => curl/7.35.0
    [HTTP_X_FORWARDED_FOR] => 149.129.122.128
    [HTTP_X_FORWARDED_PORT] => 80
    [HTTP_X_FORWARDED_PROTO] => http
    [HTTP_CONNECTION] => keep-alive
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
    [SERVER_SIGNATURE] => 
    [SERVER_SOFTWARE] => Apache
    [SERVER_NAME] => default-environment-zjqxqumicq.elasticbeanstalk.com
    [SERVER_ADDR] => 172.31.5.238
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 172.31.11.66
    [DOCUMENT_ROOT] => /var/www/html/
    [REQUEST_SCHEME] => http
    [CONTEXT_PREFIX] => 
    [CONTEXT_DOCUMENT_ROOT] => /var/www/html/
    [SERVER_ADMIN] => root@localhost
    [SCRIPT_FILENAME] => /var/www/html/searchPage.php
    [REMOTE_PORT] => 16148
    [REDIRECT_QUERY_STRING] => crs_category=test
    [REDIRECT_URL] => /category/test
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => crs_category=test
    [REQUEST_URI] => /category/test
    [SCRIPT_NAME] => /searchPage.php
    [PHP_SELF] => /searchPage.php
    [REQUEST_TIME_FLOAT] => 1437485413.02
    [REQUEST_TIME] => 1437485413
)
Array
(
    [crs_category] => test
)
* Connection #0 to host default-environment-zjqxqumicq.elasticbeanstalk.com left intact

成功了。