Linux:在Nginx上设置node.js [英] Linux: Setting up node.js on nginx
问题描述
我刚刚给自己买了一个新的raspberry pi 3,并安装了带有node的nginx. Nginx Web服务器已启动并正在运行.但是不知何故,我无法获得网站工作的JavaScript.我需要为我的Web服务器的每个位置启用javascript吗,还是可以按照我只为服务器上的每个站点工作的方式来设置节点?
I just got myself a new raspberry pi 3 and installed nginx with node onto it. The nginx webserver is up and running. But somehow I don't get the javascripts of my website work. 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?
在这里您可以看到我网站的目录层次结构:
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 #
# | ... #
#########################################
当您访问我的网站( http://strtpg.xyz )时,主页位于/www/var之外/显示蜂鸣声.这是一个集线器,可以访问服务器上托管的所有其他站点.您可以通过将其文件夹名称附加到链接来访问其他站点: ' http://strtpg.xyz/proj1 '或'
When you go to my website (http://strtpg.xyz) the mainpage out of /www/var/ is beeing shown. 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: e.g. 'http://strtpg.xyz/proj1' or 'http://strtpg.xyz/proj5'. Some of the sites got javascript and some don't.
这是我来自/etc/nginx/sites-available/
的配置文件:
And this is my config file from /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通常用作侦听端口,提供静态文件并具有将在服务器中运行javascript的API端点的Web服务器.为了实现这一点,开发人员使用ExpressJS之类的应用程序框架.
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. To achieve that developers use application frameworks like ExpressJS.
将nginx指向类似/var/www
的目录是错误的.
Pointing nginx to a directory like /var/www
is wrong.
通常,您必须像这样运行您的NodeJS应用
Usually you will have to run your NodeJS app like this
$ node app.js
之后,您的服务器将侦听端口.可以说在这种情况下是3000
After that you server will listen to a port. Lets say that in this case is 3000
,因此使用curl http://localhost:3000/
将获得您的根站点.您可以从app.js
内部来设置应用程序将要服务的所有静态和动态内容.例如:如果要发送index.html
,则必须执行sendfile
.
so using curl http://localhost:3000/
will get your root site. Its your job from inside app.js
to setup all your static and dynamic content that your application will serve. Example: if you want to send index.html
you have to do a sendfile
.
在nginx方面,您唯一要做的就是创建反向代理.
In the nginx side the only thing you will have to do is to create a reverse proxy.
现在,nginx将把对example.com
的所有请求代理到http://localhost:3000
Now nginx will proxy all requests that are made to example.com
to your node application server at 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;
}
}
这篇关于Linux:在Nginx上设置node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!