繁体   English   中英

Linux:在Nginx上设置node.js

[英]Linux: Setting up node.js on nginx

我刚刚给自己买了一个新的raspberry pi 3,并安装了带有node的nginx。 Nginx Web服务器已启动并正在运行。 但是不知何故,我无法获得网站工作的JavaScript。 我需要为我的Web服务器的每个位置启用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通常用作侦听端口,提供静态文件并具有将在服务器中运行javascript的API端点的Web服务器。 为了实现这一点,开发人员可以使用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;
    }

}

暂无
暂无

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

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