用Docker搭建【nginx+uwsgi+django】,端口怎么配置?

目录

  • 一、问题
  • 1-1 请问
  • 1-2 补充
  • 二、环境
  • 2-1 系统环境
  • 2-2 Docker环境
  • 三、补充说明
  • 3-1 概述
  • 3-2 uWSGI的配置
  • 3-3 Nginx.conf


一、问题

期望用Docker在云服务器上搭建一个网站服务,计划的结构如下图:

img

1-1 请问

【nginx.conf】【uwsgi.ini】这两个关键的配置文件应该怎么写?

或者换个表述方式:
【nginx的server块】与【uwsgi的端口】应该怎么写?

最好能帮列出两个配置文件的写法,谢谢

1-2 补充

nginx、docker运行在同一台主机上,其中docker容器中运行着django+uWSGI。


二、环境

2-1 系统环境

  • OS —Ubuntu 20.04.4 LTS
  • Python —3.8.10
  • Django —4.0.1
  • uWSGI —2.0.20
  • Nginx —1.18.0 (Ubuntu)

2-2 Docker环境

# 第一项:docker启动指令
sudo docker run -it -d --name django_9001 -p 9001:9001 -v /home/gavin/docker/django/mysite:/code 23ab6d08ae45 /bin/bash

# 第二项:docker inspect 节选
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",

三、补充说明

以下供参考,期望能帮助理解问题

3-1 概述

在一台云服务器上,采用【Nginx + uWSGI + Django】的方式(不使用Docker),已经成功搭建起一个可正常运行的网站服务。(加入docker后,就不知道该怎么配置了。)

结构如下图:

img

两个关键配置文件【uwsgi.ini】和【nginx.conf】如下。


3-2 uWSGI的配置

[uwsgi]

chdir = ./
wsgi-file = mysite/wsgi.py
module = mysite.wsgi:application
master = true
processes = 2
harakiri = 30
max-requests = 1000
socket = 127.0.0.1:9001
vacuum = true
daemonize = uwsgi.log
pidfile = uwsgi.pid

3-3 Nginx.conf

user                www-data;                           
worker_processes    auto;                               
pid                 /run/nginx.pid;                     
include             /etc/nginx/modules-enabled/*.conf;

events {    
    worker_connections      512;     
}

http {
    sendfile                on;                        
    tcp_nopush              on;                          
    tcp_nodelay             on;                        
    keepalive_timeout       65;                        
    types_hash_max_size     2048;                       
    include                 /etc/nginx/mime.types;      
    default_type            application/octet-stream;   
    server_tokens           off;                        
    gzip                    on;                         
    
    include         /etc/nginx/conf.d/*.conf;
    include         /etc/nginx/sites-enabled/*;
    include         /home/gavin/nginx/*.conf;            

    # 端口转发
    server{
              listen 80;
              server_name www.example.com;
              
              location / {
                    proxy_set_header Host $host;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_pass http://127.0.0.1:9011;
              }              
         }
    
    # 连接Nginx-uWSGI
    server {                                                    
            listen          9011;                                     
            server_name     www.example.com;         
            charset         UTF-8;                                  
           
            location / { 
                include             /etc/nginx/uwsgi_params;                   
                uwsgi_pass          127.0.0.1:9001;                
                uwsgi_param         UWSGI_SCRIPT mysite.wsgi;       
                uwsgi_param         UWSGI_CHDIR /home/gavin/docker/djando/mysite;                         
            }   
         } 
}

需要看一下容器网络他们如果在同一个网络下那就应该会满足你的需求
https://azang.blog.csdn.net/article/details/120144349

https://blog.csdn.net/weixin_30514745/article/details/102164770

可以参考:
https://pythonmana.com/2021/12/202112230957510385.html