简体   繁体   English

Linux:在Nginx上设置node.js

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

I just got myself a new raspberry pi 3 and installed nginx with node onto it. 我刚刚给自己买了一个新的raspberry pi 3,并安装了带有node的nginx。 The nginx webserver is up and running. Nginx Web服务器已启动并正在运行。 But somehow I don't get the javascripts of my website work. 但是不知何故,我无法获得网站工作的JavaScript。 Do I need to enable the javascript for each location of my webserver or is there the possibility to set node up in a way I just works for every site on my server? 我需要为我的Web服务器的每个位置启用javascript吗,还是可以按照我只为服务器上的每个站点工作的方式来设置节点?

Here you can see the directory hierarchy of my websites: 在这里,您可以看到我的网站的目录层次结构:

#########################################
###            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              #
# | ...                                 #
#########################################

When you go to my website ( http://strtpg.xyz ) the mainpage out of /www/var/ is beeing shown. 当您访问我的网站( http://strtpg.xyz )时,会显示/ www / var /之外的主页。 It's kind of a hub where I got access to all other sites hosted on the server. 这是一个集线器,可以访问服务器上托管的所有其他站点。 And you can access the different sites by appending it's folder name to the link: eg ' http://strtpg.xyz/proj1 ' or ' http://strtpg.xyz/proj5 '. 您可以通过将其文件夹名称附加到链接来访问不同的站点:例如“ http://strtpg.xyz/proj1 ”或“ http://strtpg.xyz/proj5 ”。 Some of the sites got javascript and some don't. 有些网站有JavaScript,有些则没有。

And this is my config file from /etc/nginx/sites-available/ : 这是来自/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;
    }
}

All the stuff I tried out to get node working with my websites is commented out, so I can at least see the website itself. 我尝试使节点与我的网站一起工作的所有内容都已被注释掉,因此我至少可以看到网站本身。

Node is usually used as a Web server that listens to a port, serves static files and has API endpoints that will run javascript in the server. Node通常用作侦听端口,提供静态文件并具有将在服务器中运行javascript的API端点的Web服务器。 To achieve that developers use application frameworks like ExpressJS. 为了实现这一点,开发人员可以使用ExpressJS之类的应用程序框架。

Pointing nginx to a directory like /var/www is wrong. 将nginx指向/var/www类的目录是错误的。

Usually you will have to run your NodeJS app like this 通常,您必须像这样运行您的NodeJS应用程序

$ node app.js

After that you server will listen to a port. 之后,您的服务器将侦听端口。 Lets say that in this case is 3000 可以说在这种情况下是3000

so using curl http://localhost:3000/ will get your root site. 因此使用curl http://localhost:3000/将获得您的根站点。 Its your job from inside app.js to setup all your static and dynamic content that your application will serve. 您可以在app.js内部完成设置应用程序将要服务的所有静态和动态内容的工作。 Example: if you want to send index.html you have to do a sendfile . 示例:如果要发送index.html ,则必须执行sendfile

In the nginx side the only thing you will have to do is to create a reverse proxy. 在Nginx方面,您唯一要做的就是创建反向代理。

Now nginx will proxy all requests that are made to example.com to your node application server at http://localhost:3000 现在,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