Nginx proxy_pass返回 502 Bad Gateway

分享于 

4分钟阅读

互联网

  繁體

问题:

我正在使用Spring和Docker为reverse_proxy构建Nginx

这是配置。

nginx.conf


user proxy;


worker_processes auto;


error_log logs/error.log;


error_log logs/error.log notice;


error_log logs/error.log info;


pid logs/nginx.pid;



events {


 worker_connections 1024;


}



http {


 log_format main '$remote_addr - $remote_user [$time_local] "$request" '


 '$status $body_bytes_sent "$http_referer" '


 '"$http_user_agent" "$http_x_forwarded_for"';


 access_log logs/access.log main;


 sendfile on;


 tcp_nopush on;


 tcp_nodelay on;


 keepalive_timeout 65;


 types_hash_max_size 2048;



 include mime.types;


 default_type application/octet-stream;



 include /home/wertkey/nginx/conf/conf.d/*.conf;



 client_max_body_size 200M;


 client_header_buffer_size 48k;



 server {


 listen 7070 default_server;


 listen [::]:7070 default_server;


 server_name _;


 root /usr/share/nginx/html;



 location / {


 }



 error_page 404 /404.html;


 location = /404.html {


 }



 error_page 500 502 503 504 /50x.html;


 location = /50x.html {


 }


 }


}




conf.d/default.conf


server {


 listen 7070;


 server_name something.com;



 proxy_connect_timeout 900s;


 proxy_send_timeout 900s;


 proxy_read_timeout 900s;


 send_timeout 900s;



 location ^~ /editor/ {


 proxy_pass http://localhost:8081;


 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


 proxy_set_header X-Real-IP $remote_addr;


 proxy_set_header Host $host;



 rewrite ^/editor/(/.*)$ $1 break;


 }


}



Docker


CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES


a6a6a98a25a9 editor "/bin/sh -c /usr/l..." 8 minutes ago Up 8 minutes 0.0.0.0:8081->8080/tcp editor



spring服务器在7070端口运行,

  • request something.com:7070/api
  • nginx proxy_pass to 8081
  • docker 8081->8080

但是响应总是返回502 Bad Gateway


答案1:

当进程内部Docker尝试连接到localhost:8081时,该进程转换为"Docker容器上的端口8081",而不是"主机上的端口8081"

尝试docker-compose,使用docker-compose,每个容器都可以按名称访问(例如:如果容器名为backendnginx,就后者可以通过backend:8081访问前者)。

如果在开发期间使用它们,这尤其方便,因为docker-compose允许你快速启动所有容器,并通过一个接口管理它们。


答案2:

问题可能与location块中的rewrite指令相关,


 location ^~ /editor/(.*)$ {


 proxy_pass http://localhost:8081/$1;


 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


 proxy_set_header X-Real-IP $remote_addr;


 proxy_set_header Host $host;


 }





Proxy  Nginx  GATE  Gateway  
相关文章