最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Linux:在nginx上设置node.js.

IT培训 admin 3浏览 0评论

Linux:在nginx上设置node.js.

我刚给自己一个新的覆盆子pi 3并在其上安装了带节点的nginx。 nginx webserver已启动并正在运行。但不知何故,我没有得到我网站的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              #
# | ...                                 #
#########################################

当你去我的网站()时,显示/ www / var /的主页。它是一种中心,我可以访问服务器上托管的所有其他站点。您可以通过将其文件夹名称附加到链接来访问不同的网站:例如'/proj1'或'/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 /;
#       proxy_redirect off;
    }

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

我试图让节点与我的网站一起工作的所有东西都被注释掉了,所以我至少可以看到网站本身。

回答如下:

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

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

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

$ node app.js

之后,您的服务器将侦听端口。让我们说在这种情况下是3000

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

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

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

server {
    listen 80;

    server_name example;

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

}

Linux:在nginx上设置node.js.

我刚给自己一个新的覆盆子pi 3并在其上安装了带节点的nginx。 nginx webserver已启动并正在运行。但不知何故,我没有得到我网站的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              #
# | ...                                 #
#########################################

当你去我的网站()时,显示/ www / var /的主页。它是一种中心,我可以访问服务器上托管的所有其他站点。您可以通过将其文件夹名称附加到链接来访问不同的网站:例如'/proj1'或'/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 /;
#       proxy_redirect off;
    }

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

我试图让节点与我的网站一起工作的所有东西都被注释掉了,所以我至少可以看到网站本身。

回答如下:

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

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

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

$ node app.js

之后,您的服务器将侦听端口。让我们说在这种情况下是3000

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

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

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

server {
    listen 80;

    server_name example;

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

}

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论