[英]Setup Node.JS Server on Ubuntu
我正在使用 MEAN Stack,我已經可以運行我的前端了。 在ng build
之后,我將dist
文件夾中的所有內容移動到var/www/html
並且可以訪問我的網站。 我正在使用 Apache,我的前端現在可以在線使用。 問題現在是我的后端。 我正在使用 Node.js 但我不知道如何讓每個人都“在線”。 使用npm start server.js
或 pm2 start pm2 start server.js
我可以讓我的 Node.js 服務器運行,一切正常,但它只對我有用。 當我的朋友訪問我的網站時,后端沒有運行(僅前端)。
所以我的問題是,我怎樣才能讓我的 node.js 服務器公開? 有沒有辦法在 Apache 中運行 node.js ?
實際上我做了一個 apache 代理,甚至使用 nginx 但似乎還沒有工作......
我從這里開始執行此步驟: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04
也從這里: https://medium.com/@pierangelo1982/using-nodejs-app-with-apache-374b37c6140d
編輯:
I am able to server my backend with nginx but I have to stop my Apache server but my apache server is serving the angular frontend...
我應該怎么辦?
編輯2:
感謝 Gouveia 的幫助,我能夠使用 NGINX 為前端和后端服務
server {
listen 80;
server_name http://travelinked.vm.mi.hdm-stuttgart.de;
location / {
root /var/www/html;
}
location /api {
proxy_pass http://141.62.65.110: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;
}
}
如您所見,該站點可用,但后端仍未在我的 Intranet 之外運行。 對我來說,后端正在加載,但對你們來說,我認為不是
當您運行npm start server.js
時,您正在運行自己的服務器。
如果您希望通過 Apache 對 go 的所有請求,那么您可以使用 簡單的反向代理從 Apache 連接到您的 nodejs 后端(假設您的節點 js 服務器在 port8008 上運行):
ProxyPass "/api" "http://localhost:8080/"
linux 中的 Apache 配置文件通常位於: /etc/apache2/sites-available/
。
如果您想使用 Nginx 代替,您可以使用它提供 static 文件,同時將請求代理到 API :
server {
location / {
# Path to your angular dist folder
root /path/to/static/files;
}
location /api {
# Address of your nodejs server
proxy_pass http://localhost:8080/;
}
}
linux 中的配置文件通常位於: /etc/nginx/nginx.conf
。
使用這兩種方法,訪問/api/*
路徑上的 Apache/Nginx 服務器將向 nodejs 服務器發出請求。
在 Nginx 情況下,路徑 /api 包含在對 nodejs 服務器的請求路徑中。 要刪除該部分路徑,您將需要一個重寫規則:
location /api {
# Address of your nodejs server
proxy_pass http://localhost:8080/;
# This removes the /api part in the request to the backend
rewrite /api/(.*) /$1 break;
}
我在示例中使用了 localhost,因為我假設您在同一台機器上運行所有內容。
有關更多詳細信息和配置,我建議查看鏈接文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.