如何使用nginx将api路径代理传递到端口



我想代理根位置(/(到端口3000,将/api位置到端口5000,这是完全可能的,对吧?

我的nginx配置文件:

server {
listen       80;
server_name  mywebsite.com;
location /api {
proxy_pass http://localhost:5000;
}
location / {
proxy_pass http://localhost:3000;
}
}

如果我在本地执行api请求,我可以获得预期的输出:

myuser@myserver [conf.d]# curl localhost:5000
Hello, World!myuser@myserver [conf.d]#

但是使用api客户端就不行了,从根路径到端口3000的proxy_pass在浏览器和api客户端中运行良好

注:

  • 我没有忘记用sudo systemctl reload nginx重新加载nginx
  • 防火墙允许两个端口的流量,im使用ufw
  • 服务器操作系统为centos 7

我认为您正在使用React和nodejs。我使用以下配置。

server {
listen       80;
server_name  mywebsite.com;
location / {
# My react 
proxy_pass http://localhost: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;
}
location /api{
# This is my nodejs API 
proxy_pass http://localhost:5000;
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;
}
}

最新更新