使用htaccess重定向到Node.js应用
我正在尝试使用永远在服务器上运行在nodejs中编写的小应用程序一个>.当我这样启动我的应用程序时:
I'm trying to run a small app I've wrote in nodejs on our server with forever. When I start my app like this:
forever app.js
在我的文件夹/home/me/apps/myapp/
中,该应用程序正在侦听端口61000
In my folder /home/me/apps/myapp/
and the app is listening on port 61000
mydomain.me/myapp/
下的.htaccess文件的内容应该是什么?
What should be the content of my .htaccess file under mydomain.me/myapp/
?
当前.htaccess
内容(不起作用):
Current .htaccess
content (not working):
RewriteEngine On
# Redirect a whole subdirectory:
RewriteRule ^myapp/(.*) http://localhost:61000/$1 [P]
您应使用Apache mod_proxy而非mod_rewrite在Apache中运行Node.js应用程序:
You should use Apache mod_proxy not mod_rewrite to run a Node.js app in Apache:
<VirtualHost :80>
ServerName example.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location /myapp>
ProxyPass http://localhost:61000/
ProxyPassReverse http://localhost:61000/
</Location>
</VirtualHost>
如果您无法为Node应用添加虚拟主机,则可以尝试使用htaccess和类似的方法:
If you can't add a virtual host for your Node app you can try with htaccess and something like this:
RewriteEngine On
RewriteRule ^/myapp$ http://127.0.0.1:61000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/myapp/(.*)$ http://127.0.0.1:61000/$1 [P,L]