我的服务器上有NGINX反向代理,用于处理对http://apcompdoc.com的请求。它在端口80上侦听,并且可以成功返回Vue Dist,但是,我有一个在8081端口上运行的后端节点API,另一个在8082端口上运行的节点进程。用户从不直接在8082上请求任何内容,而是在8081上直接请求任何内容有时会在8082上请求该过程,所以我假设我根本不需要将其公开给Nginx,但我不太确定。但是,主要的问题是我相信API从未达到。我拥有它,以便当您命中端点http://apcompdoc.com/api/ *时,它应该代理节点进程。我正在使用PM2保持该进程的运行并对其进行监视,并确保它正在运行。这是我的NGINX apcompdoc.com配置文件:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name: apcompdoc.com www.apcompdoc.com;
charset utf-8;
root /var/www/apcompdoc/dist;
index index.html index.htm;
# Always serve index.html for any request;
location / {
root /var/www/apcompdoc/dist;
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://localhost:8081;
}
error_log /var/log/nginx/vue-app-error.log;
access_log /var/log/nginx/vue-app-access.log;
}
我试图在/ api / *处获取对我的API的所有请求,以将其重定向到localhost:8081处的API,然后返回给用户。我看到了一些有关将代理重定向回去的信息,我必须这样做吗?我也不知道是否需要在NGINX配置文件中执行/api/*
。
我真的是NGINX的新手,但我只想将对http://apcompdoc.com/api/ *的请求重定向到端口8081上的节点进程。
答案 0 :(得分:1)
我不确定是好的还是不好的做法,但是我总是将后端定义为upstream
。
例如,您的文件将如下所示:
upstream nodeprocess {
server localhost:8081;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name: apcompdoc.com www.apcompdoc.com;
charset utf-8;
root /var/www/apcompdoc/dist;
index index.html index.htm;
# Always serve index.html for any request;
location / {
root /var/www/apcompdoc/dist;
try_files $uri /index.html;
}
location ^~ /api {
proxy_pass http://nodeprocess;
}
error_log /var/log/nginx/vue-app-error.log;
access_log /var/log/nginx/vue-app-access.log;
}
请注意,我在api的位置添加了^~
,并删除了结尾的/