nodejs domain without port number

2019-01-21 20:37发布

I have deployed my nodejs app on a VPS(ubuntu 10.04). I have hosted it on a subdomain (subdomain.myapp.com:3000) and I just have one IP address

By default port 80 is been used by apache as default. I just want that my app should run on port 3000 but URL should be (subdomain.myapp.com)

Is this possible in nodejs or do I have to tweak my virtual host or apache's files.

标签: node.js url port
3条回答
聊天终结者
2楼-- · 2019-01-21 21:03

I needed to do the same thing and @drinchev's answer almost worked for me, but because I run several vhosts on my dev box it didn't quite. A couple minor tweaks kept it from clobbering all my other vhosts.

<Proxy *>

Needed to be

<Proxy mynodejs.mydevbox.local>

And

<VirtualHost *:80>

Needed to be

<VirtualHost mynodejs.mydevbox.local:80>

So where my local dev machine is named "mydevbox.local" and my node vhost is listening on port 3000, my final config looked more like:

<VirtualHost mynodejs.mydevbox.local:80>
    DocumentRoot "/Library/WebServer/Documents/mynodejs"
    ServerName mynodejs.mydevbox.local
    ServerAlias mynodejs.mydevbox.local

    ProxyRequests off

    <Proxy mynodejs.mydevbox.local>
            Order allow,deny
            Allow from all
    </Proxy>

    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
    ProxyPreserveHost on
    ErrorLog "/private/var/log/apache2/mynodejs_error_log"
    CustomLog "/private/var/log/apache2/mynodejs_access_log" common
</VirtualHost>

<VirtualHost myothervhost.mydevbox.local:80>
    DocumentRoot "/Library/WebServer/Documents/myothervhost"
    ServerName myothervhost.mydevbox.local
    ServerAlias myothervhost.mydevbox.local
    ErrorLog "/private/var/log/apache2/myothervhost_error_log"
    CustomLog "/private/var/log/apache2/myothervhost_access_log" common
</VirtualHost>

Other vhosts could follow

查看更多
太酷不给撩
3楼-- · 2019-01-21 21:07

Yes it is possible

In your apache virtual host file configure with the following

<VirtualHost *:80>
    ServerName subdomain.myapp.com

    ProxyRequests off

    <Proxy *>
            Order allow,deny
            Allow from all
    </Proxy>

    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
    ProxyPreserveHost on
</VirtualHost>

You should have

NameVirtualHost *:80

on top of your file and also Proxy module installed for apache ( I think it is a default configuration for ubuntu )

LoadModule proxy_module lib/httpd/modules/mod_proxy.so

it must be in your httpd.conf file

then you should restart apache and it must be fine!

查看更多
干净又极端
4楼-- · 2019-01-21 21:12

Just an update of @drinchev answer with Apache 2.4.*

Enable the proxy mode :

a2ensite proxy_http
a2ensite proxy

Then :

<VirtualHost *:80>
    ServerName subdomain.myapp.com

    ProxyRequests off

    <Proxy *>
            Require all granted
    </Proxy>

    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
    ProxyPreserveHost on
</VirtualHost>
查看更多
登录 后发表回答