相同的代理传递配置在两个实例上不起作用

相同的代理传递配置在两个实例上不起作用

我有一个配置 /etc/nginx/sites-available/test,它只代理一个服务器,第一个(博客),第二个(api)返回 nginx 404,我尝试过排除故障,但无济于事

我尝试检查语法或拼写错误,复制了工作指令,只替换了服务器路径和名称,删除了一条指令,但运行正常,但当两条指令都存在时则不行

upstream blog {
    server 127.0.0.1:3001;
}

upstream api {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    listen [::]:80;
    
    server_name local.net www.local.net;
    
    location /blog/ {
        rewrite ^/blog(.*)$ $1 break;
        proxy_pass http://blog/;
        include proxy_params;
    }
}

server {
    listen 80;
    listen [::]:80;
    
    server_name api.local.net;
    
    location /api/ {
        rewrite ^/api(.*)$ $1 break;
        proxy_pass http://api/;
        include proxy_params;
    }
}

答案1

我最终通过/etc/conf.d/*ie下的单独配置进行了反向代理

/etc/nginx/conf.d/example.com.conf

server {
  listen 80;
  server_name example.com;

  location / {
    proxy_pass http://destination;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

相关内容