Http 如何使用nginx将api路径代理传递到端口
我想代理\u将根位置(/)传递到端口3000,将/api位置传递到端口5000,这是完全可能的,对吗 我的nginx配置文件:Http 如何使用nginx将api路径代理传递到端口,http,nginx,port,backend,reverse-proxy,Http,Nginx,Port,Backend,Reverse Proxy,我想代理\u将根位置(/)传递到端口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请求,我可以获
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的代理传递在浏览器和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;
}
}
您还发送了URI,因此应该测试:
curlhttp://localhost:5000/api
这正是我所做的,谢谢,但对于需求,我现在必须使用apache