简体   繁体   English

使用htaccess重定向到Node.js应用

[英]Redirecting with htaccess to nodejs app

I'm trying to run a small app I've wrote in nodejs on our server with forever . 我正在尝试永远运行在服务器上的nodejs中编写的小应用程序。 When I start my app like this: 当我这样启动我的应用程序时:

forever app.js

In my folder /home/me/apps/myapp/ and the app is listening on port 61000 在我的文件夹/home/me/apps/myapp/ ,该应用程序正在侦听端口61000

What should be the content of my .htaccess file under mydomain.me/myapp/ ? mydomain.me/myapp/下的.htaccess文件的内容应该是什么?

Current .htaccess content (not working): 当前的.htaccess内容 (不起作用):

RewriteEngine On
# Redirect a whole subdirectory:
RewriteRule ^myapp/(.*) http://localhost:61000/$1 [P]

You should use Apache mod_proxy not mod_rewrite to run a Node.js app in Apache: 您应该使用Apache mod_proxy而不是mod_rewrite在Apache中运行Node.js应用程序:

<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>

If you can't add a virtual host for your Node app you can try with htaccess and something like this: 如果您无法为Node应用添加虚拟主机,则可以尝试使用htaccess和类似的方法:

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]

I will answer my own question, even though I think Michelems answer is also right. 即使我认为Michelems的回答也是正确的,我也会回答我自己的问题。

My initial .htaccess file works. 我最初的.htaccess文件有效。

RewriteEngine On
RewriteRule ^myapp/(.*) http://localhost:61000/$1 [P]

The thing I did wrong was creating actually the folder mydomain.de/myapp/ and put the .htaccess there. 我做错了的是实际上创建了文件夹mydomain.de/myapp/并将.htaccess放在那里。 I have now the rewirte settings in my DocumentRoot/.htaccess, no public folder called myapp and it works fine. 现在,我的DocumentRoot / .htaccess中有rewirte设置,没有名为myapp公用文件夹,并且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM