在AWS LightSail上不断收到来自WordPress的401错误

时间:2021-07-07 作者:Pierogi

我正在尝试使用节点wpapi发布到AWS Lightsail实例上的WordPress服务器。但是,服务器返回401错误。

我已经有了.htaccess 使用文件RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}] 给我的.htaccess 文件和我已经使用了“应用程序密码”插件。

如何使用节点wpapi访问服务器?

此处是我的节点wpapi设置。

const wp = new WPAPI({
    endpoint: \'http://localhost/wp-json\',
    username: \'user\', //This is a default admin user.
    password: \'*************************\', //This is a password for application passwords plugin 
    auth: true,
});
我的.htaccess 文件在此处。

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
此处显示错误消息。

 code: \'rest_cannot_create\',
  message: \'Sorry, you are not allowed to create new posts.\',
  data: { status: 401 }
“我的用户配置文件”页面一直显示以下消息。

> Due to a potential server misconfiguration, it seems that HTTP Basic Authorization may not work for the REST API on this site: `Authorization` headers are not being sent to WordPress by the webserver. You can learn more about this problem, and a possible solution, on our GitHub Wiki.

2 个回复
最合适的回答,由SO网友:Pierogi 整理而成

我找到了解决办法。

由AWS Lightsail实例图像制作的WordPress是bitnami WordPress。默认情况下,bitnami WordPress禁用基本身份验证。所以需要对其进行一些修改/opt/bitnami/apps/WordPress/conf/httpd-app.conf 启用基本身份验证。此修改将在下面添加3行。

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
httpd应用程序。conf在下面结束。

RewriteEngine On
RewriteRule /<none> / [L,R]

<IfDefine USE_PHP_FPM>
    <Proxy "unix:/opt/bitnami/php/var/run/wordpress.sock|fcgi://wordpress-fpm" timeout=300>
    </Proxy>
</IfDefine>

<Directory "/opt/bitnami/apps/wordpress/htdocs">
    Options +MultiViews +FollowSymLinks
    AllowOverride None
    <IfVersion < 2.3 >
        Order allow,deny
        Allow from all
    </IfVersion>
    <IfVersion >= 2.3>
        Require all granted
    </IfVersion>
    
    

    <IfDefine USE_PHP_FPM>
       <FilesMatch \\.php$>
         SetHandler "proxy:fcgi://wordpress-fpm"
       </FilesMatch>
    </IfDefine>

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
    
    RewriteEngine On
    #RewriteBase /wordpress/
    RewriteRule ^index\\.php$ - [S=1]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]

    Include "/opt/bitnami/apps/wordpress/conf/banner.conf"
</Directory>

Include "/opt/bitnami/apps/wordpress/conf/htaccess.conf"
                    
然后重新启动apache或实例本身。然后我安装了Application Passwords 插件和我将其作为一个正常的过程使用
配置文件页面上显示的插件的以下消息已消失。

 Due to a potential server misconfiguration, it seems that HTTP Basic Authorization may not work for the REST API on this site: `Authorization` headers are not being sent to WordPress by the webserver. You can learn more about this problem, and a possible solution, on our GitHub Wiki.
中的HTTP\\u AUTHORIZATION环境变量。htaccess文件不需要替换为REMOTE\\u用户。以防万一,我展示我的。htaccess文件。

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
此解决方案来自this page.
此解决方案页面与上面我的解决方案的区别在于我使用Application Passwords 插件,但他们使用JSON Basic authentication plugin.

SO网友:MrWhite

RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
除非您有特定的要求,否则通常会设置HTTP_AUTHORIZATION 此处为环境变量,而不是REMOTE_USER, 为了基本上通过Authorization HTTP请求头一直到PHP/WordPress。(这似乎就是错误所暗示的?)

UPDATE: 同时尝试设置CGIPassAuth On.htaccess 文件看见my answer 在以下情况下related question on StackOverflow.

相关推荐

重新保存固定链接后,.htaccess重定向消失

我希望我的WordPress网站加载https+非www,并且没有斜杠。我输入了以下代码.htaccess 文件:<IfModule mod_rewrite.c> RewriteEngine On # Remove trailing slash from non-filepath urls RewriteCond %{REQUEST_URI} /(.+)/$ RewriteCond %{REQUEST_FILENAME} !-d Rewrite