我应该将Node JS应用放在哪里,以便可以通过主网站进行访问? [英] Where do I put my Node JS app so it is accessible via the main website?

查看:77
本文介绍了我应该将Node JS应用放在哪里,以便可以通过主网站进行访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在home/myusername/myappname目录中安装了一个nodejs应用程序(基石)应用程序.

I've recently installed a nodejs app (keystone) app in my home/myusername/myappname directory.

当我访问www.mydomain.com时,什么都没有显示-即使在打开我的nodejs应用程序之后.

When I visit www.mydomain.com, nothing displays - even after turning on my nodejs app.

这些文件应该在哪里?

我正在运行ubuntu 16.04.

I am running ubuntu 16.04.

过去我曾经使用过var/www文件夹,但是我没有使用apache-我需要手动创建此文件夹吗?

In the past I have worked with a var/www folder, but I am not using apache - do I need to manually create this folder?

谢谢!

推荐答案

为使您的应用程序可见,该应用程序必须正在运行(显然)并且可以在端口80上访问(如果您希望不添加端口号就可以使用它) URL).

For your app to be visible it has to be running (obviously) and accessible on port 80 (if you want it to be available without adding a port number to the URL).

只要磁盘在运行,它在磁盘上的哪个位置都没关系.

It doesn't matter where it is on the disk as long as it's running.

您不需要Apache或nginx或任何其他服务器.您的Node应用程序可以在端口80上侦听.但是,它也可以在其他端口上侦听,而您的其他服务器(Apache,nginx等)可以将请求代理到该端口.

You don't need Apache or nginx or any other server. Your Node app may listen on port 80. But alternatively it can listen on some other port and your other server (Apache, nginx, etc.) can proxy the requests to that port.

但是,如果您的应用正在监听,例如端口3000,那么您应该可以使用http://www.example.com:3000/来访问它.

But if your app is listening on, e.g. port 3000 then you should be able to access it as http://www.example.com:3000/.

此外,请确保您的域配置正确.它是www子域的IPv4(或IPv6的AAAA)记录,应等于服务器的公共可访问IP地址.

Also, make sure that your domain is configured correctly. It's A record for IPv4 (or AAAA for IPv6) of the www subdomain should be equal to the publicly accessible IP address of your server.

并确保您使用的端口未被防火墙阻止.

And make sure that the port you use is not blocked by the firewall.

要查看如何使用Keystone设置端口,请参阅:

To see how you can set the port with Keystone, see:

可以在配置中更改它,也可以使用以下命令运行您的应用程序:

It can be either changed in the config or you can run your app with:

PORT=80 node yourApp.js

代替:

node yourApp.js

但是请记住,要使用低于1024的端口号,通常需要该程序以root用户身份运行(或添加更复杂的特殊特权).

but keep in mind that to use the port number below 1024 you will usually need the program to run as root (or add a special privilege which is more complicated).

这也意味着即使您拥有更多的域名,这也将是您可以在此服务器上运行的唯一应用程序.

It will also mean that this will be the only application that you can run on this server, even if you have more domain names.

如果您不想以root身份运行或想要托管更多应用程序,则最简单的方法是安装nginx并代理请求.这样的配置称为反向代理"-最好使用该短语来搜索信息和教程.

If you don't want to run as root or you want to host more application, it is easiest to install nginx and proxy the requests. Such a configuration is called a "reverse proxy" - it's good to search for info and tutorials using that phrase.

最简单的nginx配置如下所示:

The simplest nginx config would be something like this:

server {
    listen 80;
    server_name www.example.com;
    location / {
        proxy_pass http://localhost:3000;
    }
}

您可以在以下位置进行设置:

You can set it in:

  • /etc/nginx/sites-available/default

或位于其他文件中,例如:

or in a different file as e.g.:

  • /etc/nginx/sites-available/example

,然后链接为/etc/nginx/sites-enabled/example

and then symlinked as /etc/nginx/sites-enabled/example

您需要在更改配置后重新启动nginx.

You need to restart nginx after changing the config.

您可以在此处找到有关配置反向代理的更多选项:

You can find more options on configuring reverse proxies here:

这篇关于我应该将Node JS应用放在哪里,以便可以通过主网站进行访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆