NGINX 作为 http 和 ssh 的反向代理

问题描述

我正在尝试将 Nginx 设置为 HTTP 和 SSL 的反向代理。

这是/etc/Nginx/conf.d/default.conf中的一个配置:

upstream sample-client {
  server sample-client:3006;
}

upstream sample-server {
  server sample-server:3000;
}

upstream ssh {
  server sample-server:22;
}

server {
  listen 80;
       
  location / {
    proxy_pass http://sample-client;
  }

  location /api {
    rewrite /api/(.*) /$1 break;
    proxy_pass http://sample-server;
    client_max_body_size 100M;
    proxy_read_timeout 300;
    proxy_connect_timeout 300;
    proxy_send_timeout 300;
  }
     
  error_page 405 =200 @405; 

  location @405 {
    root /usr/share/Nginx/html;
      proxy_pass http://sample-client; 
  }  
}

server {
  listen 22;
  proxy_pass ssh;
}

但是它抛出了下一个错误

Nginx: [emerg] "proxy_pass" directive is not allowed here in /etc/Nginx/conf.d/default.conf:60

怎么了?

解决方法

proxy_pass 指令应该在位置块内 https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

中描述

要将请求传递到 HTTP 代理服务器, proxy_pass 指令在一个 location .

这意味着第二个服务器位置必须包含一个位置块 大概类似于

位置 / { proxy_pass SSH; }