我正在使用MEAN Stack,我已经可以运行我的前端了。在ng build之后,我将dist文件夹中的所有内容都移到了var/www/html中,这样就可以访问我的网站了。我正在使用Apache,我的前端现在可以在线使用了。现在问题出在我的后台了。我正在使用Node.js,但我不知道如何让每个人都能“在线”使用它。使用npm start server.js或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
编辑:
我可以使用nginx为我的后端提供服务,但我必须停止我的Apache服务器,但我的apache服务器正在为angular前端提供服务……
我该怎么办?
EDIT2:
感谢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;
}
}正如你所看到的,网站是可用的,但后端仍然不能在我的内部网之外运行。对我来说,后端正在加载,但对你们来说,我不这么认为
发布于 2021-05-16 05:08:24
当您运行npm start server.js时,您是在单独运行一个服务器。
如果您希望所有请求都通过Apache,那么您可以使用simple reverse proxy从Apache连接到您的nodejs后端(假设您的node js服务器在端口8080上运行):
ProxyPass "/api" "http://localhost:8080/"linux中的Apache配置文件通常位于以下位置:/etc/apache2/sites-available/。
如果你想使用Nginx,你可以使用它进行serve your static files,同时也可以使用proxying the requests to the 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的情况下,到nodejs服务端的请求路径中会包含/api路径。要删除该部分路径,您将需要一个重写规则:
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,因为我假设您在同一台机器上运行所有东西。
有关更多详细信息和配置,我建议查看链接文档。
https://stackoverflow.com/questions/67550957
复制相似问题