使用两个停靠合成容器(Flask和Django)配置Nginx

9njqaruj  于 2023-03-01  发布在  Nginx
关注(0)|答案(1)|浏览(139)

我尝试公开两个类似API的地址。一个在Django中,另一个在Flask中,它们是Docker-compose容器。
我需要配置Nginx在两个不同的子域中公开两个容器。
这是我的Nginx.conf:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
 worker_connections  1024;  ## Default: 1024, increase if you have lots of clients
 }    

http {
  include       /etc/nginx/mime.types;
  # fallback in case we can't determine a type
  default_type  application/octet-stream;

 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  /var/log/nginx/access.log  main;

sendfile        on;
#tcp_nopush     on;

keepalive_timeout  65;

upstream app {
 server django:5000;
}

upstream app_server {
 server flask:5090;
}

server {
 listen 5090;
 location / {
    proxy_pass http://app_server;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Scheme $scheme;
  }
}

server {
 listen 5000;
 location / {
    proxy_pass http://app;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Scheme $scheme;
 }
 }

}

还有我的制作。yml

Nginx:
  build: ./compose/production/nginx
  image: *image
  ports:
    - 80:80
  depends_on:
    - flask
    - django

我的集装箱都装满了。

htrmnn0y

htrmnn0y1#

我使用proxy_pass

server {
    listen <port>;
    location / {
        proxy_pass http://<container-host-name>:<port>;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Scheme $scheme;
    }
}

您的nginx容器只连接到计算机上的80端口和容器上的80端口,但您的nginx服务器侦听50005090端口:)

相关问题