Apache重定向到https:// www,然后代理到localhost

时间:2017-09-04 15:30:26

标签: node.js apache .htaccess proxy plesk

我在localhost:8180上运行了一个node.js应用。

我希望只能通过https://www.example.com从网络访问该应用。

目标是将转到http://example.comhttp://www.example.comhttps://example.com的所有流量重定向到https://www.example.com

我可以访问Plesk面板v12.5.30。在此面板中,我可以为http和https请求配置单独的.htaccess规则。小组看起来像这样:

Plesk Apache directives panel

我可以使用此面板实现目标吗?

我已经为http和https尝试了以下规则但是却出现了代理错误。

HTTP

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com$1 [L,R=301]`

HTTPS

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://localhost:8180$1 [P]
ProxyPassReverse / http://localhost:8180/

1 个答案:

答案 0 :(得分:0)

我强烈推荐使用Nginx指令,而不是这样:

if ($scheme != https) {
    return 301 https://www.example.com$request_uri;
}

Redirect 301 using Nginx

您还可以在所有.htaccess文件代码之上尝试更好的兼容性htaccess代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>