【问题标题】:Linux: Setting up node.js on nginxLinux:在 nginx 上设置 node.js
【发布时间】:2018-06-04 23:13:42
【问题描述】:

我刚给自己买了一个新的树莓派 3,并在上面安装了带有节点的 nginx。 nginx 网络服务器已启动并正在运行。但不知何故,我没有让我的网站工作的 javascripts。我是否需要为我的网络服务器的每个位置启用 javascript,或者是否有可能以我只适用于我服务器上的每个站点的方式设置节点?

在这里你可以看到我的网站的目录层次结构:

#########################################
###            Directories            ###
#########################################
# /www/var/                             #
# | - > index.html (the hub/mainpage)   #
# | proj1/                              #
# | | - > index.html (website 1)        #
# | proj2/                              #
# | | - > index.html (website 2)        #
# | | - > js/somescript.js              #
# | proj3/                              #
# | | - > index.html (website 3)        #
# | | - > js/anotherscript.js           #
# | proj4/                              #
# | | - > index.html (website 4)        #
# | proj5/                              #
# | | - > index.html (website 5)        #
# | | - > js/morescript.js              #
# | ...                                 #
#########################################

当您访问我的网站 (http://strtpg.xyz) 时,会显示 /www/var/ 的主页。这是一个集线器,我可以访问服务器上托管的所有其他站点。您可以通过将其文件夹名称附加到链接来访问不同的站点:例如“http://strtpg.xyz/proj1”或“http://strtpg.xyz/proj5”。有些网站有 javascript,有些没有。

这是我来自/etc/nginx/sites-available/的配置文件:

# Server configuration
#upstream pnkpnd {
#   server localhost:3420;
#   keepalive 8;
#}

server {
    listen 0.0.0.0:80;
    listen [::]:80;
    server_name localhost;
    index index.html index.htm;

    location / {
        root /var/www;
        try_files $uri $uri/ =404;
        # Default root of site won't exist.
        #return 410;
    }

    location /strtpg {
        root /var/www;
        try_files $uri $uri/ =404;
    }

    location /dshbrd {
        root /var/www;
        try_files $uri $uri/ =404;
    }

    location /pnkpnd {
        root /var/www;
        try_files $uri $uri/ =404;
#       proxy_http_version 1.1;
#       proxy_set_header Upgrade $http_upgrade;
#       proxy_set_header Connection 'upgrade';
#       proxy_set_header X-Real-IP $remote_addr;
#       proxy_set_header X-Forward-For $proxy_add_x_Forwarded_for;
#       proxy_set_header Host $http_host;
#       proxy_set_header X-NginX-Proxy true;
#       proxy_pass http://strtpg.xyz/;
#       proxy_redirect off;
    }

    location ~ /\.ht {
        deny all;
    }
}

我尝试让 node 与我的网站一起工作的所有内容都被注释掉了,所以我至少可以看到网站本身。

【问题讨论】:

  • 这一切与 node.js 有什么关系?我似乎找不到与 nodeJS 的连接。你把我弄糊涂了。
  • 我不需要 node.js 在我的服务器上运行 javascript 吗?

标签: node.js linux nginx raspberry-pi3


【解决方案1】:

Node 通常用作 Web 服务器,它侦听端口、提供静态文件并具有将在服务器中运行 javascript 的 API 端点。为了实现这一点,开发人员使用 ExpressJS 等应用程序框架。

将 nginx 指向像 /var/www 这样的目录是错误的。

通常你必须像这样运行你的 NodeJS 应用程序

$ node app.js

之后,您的服务器将监听一个端口。假设在这种情况下是3000

所以使用curl http://localhost:3000/ 将获得您的根站点。您的工作是在app.js 内部设置您的应用程序将提供的所有静态和动态内容。示例:如果您想发送index.html,您必须发送sendfile

在 nginx 端,您唯一需要做的就是创建一个反向代理。

现在 nginx 会将向example.com 发出的所有请求代理到您的节点应用服务器http://localhost:3000

server {
    listen 80;

    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

}

【讨论】:

  • 我是否必须为每个使用 JavaScript 的位置设置代理设置,如果需要,每个位置是否需要有不同的端口?
  • 没有没有必要。你应该从阅读“什么是”和“它是如何工作的”开始,因为你的理解是肤浅的。
  • 好吧,看来我应该明年再看看。 ^^ 在我看来,删除 nginx 并安装 apache 让 javascripts 在服务器端运行会容易得多。这会让我现在更容易实现我的目标。
猜你喜欢
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-20
  • 2017-01-14
  • 2013-09-12
  • 2021-01-10
相关资源
最近更新 更多