我有一个Angular+Node.JS应用程序。当我在本地运行该程序时,我在我的Angular应用程序中定义了一个baseurl = http://localhost:3000/
,并在程序定义的链接中使用该前缀访问我的NodeJS后端,但现在当我想在远程服务器上部署我的应用程序时,我们将baseurl
定义更改为baseurl = http://111.222.333.444:3000/
(例如,111.222.333.444
是我的服务器ip地址(,但它不起作用!
我应该如何将我的Angular应用程序连接到远程服务器上的NodeServer?
编辑:这是我的/etc/nginx/nginx.conf
文件内容:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /demo/stock-front9/dist/strategy;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
#proxy_pass http://localhost:3000;
#proxy_http_version 1.1;
# First attempt to serve request as file, then
# as directory, then redirect to index(angular) if no file found.
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
我不会,我认为最好用PM2这样的工具运行Node应用程序,然后在它前面放置一个使用Nginx的反向代理,PM2将充当您服务的协调器,而Nginx将仅通过标准web端口(80443(提供访问。
在Angular的情况下,在编译时,它应该生成一个静态web应用程序,你可以使用相同的Nginx反向代理为其服务,这样你就可以省去配置CORS、API路由等的精力,一切都将通过Nginx。
Nginx配置文件示例的更新
server {
listen 80;
server_name example.org;
location /api {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_redirect off;
proxy_http_version 1.1;
}
location / {
root /path/to/angular/compiled/app;
index index.html;
}
}
然后angular应用程序应该指向同一个主机。
祝你好运并欢呼:(
您仍然可以在本地运行您的angular应用程序。对于后端服务器,您可以使用代理。请看一下这个。https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md#using-公司代理