我正在尝试使用节点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.
最合适的回答,由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.