簡體   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