我想将请求重定向到/api到运行在localhost上的节点应用程序:3000使用NGINX
我试图使用proxy_pass重定向请求
server {
listen 80;
server_name localhost;
root /var/www/mydomain/html;
index index.html index.htm;
location /api {
proxy_pass http://127.0.0.1:3000/;
}
}如果我在浏览器中访问localhost,就会看到前端应用程序,所以这是正确的。
如果我点击http://127.0.0.1:3000 (或localhost:3000),就会从节点API中得到一个响应,所以这是正确的。
但是,当我试图访问localhost/api时,会得到以下错误:
错误6#6:*3 connect()失败(111:连接被拒绝)而连接>上游,客户端: 172.18.0.1,服务器: localhost,请求:"GET /api >HTTP/1.1",上游:"http://127.0.0.1:3000/",主机:"localhost“
编辑:
我正在使用一个码头组合来管理服务:
services:
nginx:
container_name: nginx-local
build:
context: .
dockerfile: ./nginx.dev.dockerfile
volumes:
...
ports:
- '80:80'
networks:
- local-network
node:
container_name: node-api
build:
context: ./api
dockerfile: .docker/node.dockerfile
ports:
- '3000:3000'
networks:
- local-network发布于 2019-08-09 16:27:59
基于docker-compose文件,需要将nginx配置更改为
server {
listen 80;
server_name localhost;
root /var/www/mydomain/html;
index index.html index.htm;
location /api {
proxy_pass http://node-api:3000/;
}
}这是由于节点应用程序的独立docker映像造成的。因此,您需要使用代理通行证来指向正确的位置。
https://stackoverflow.com/questions/57433677
复制相似问题