虚拟机linux下用docker运行nginx,nginx负载均衡代理本地django服务

虚拟机linux下用docker运行nginx部署本地django

docker拉取nginx

ningx官网地址:https://nginx.org/en/download.html

docker pull nginx:1.22.1  # nginx:版本号

启动nginx容器

# 查看镜像
docker images

# 启动nginx  --name给容器取的名称  -d -p 映射端口:宿主机端口 镜像名称:版本号
docker run -it --name nginx -d -p 80:80 docker.io/nginx:1.22.1

# 查看ngnix容器启动情况
docker ps 

虚拟机启动两个django服务

0.0.0.0:8000
0.0.0.0:8001

进入容器

docker exec -it nginx bash

查看nginx配置文件

cd /etc/nginx/conf.d/
vi default.conf

由于我们是用docker安装的,所以此处Nginx中如果配置代理127.0.0.1的8080端口,其实是代理的docker容器中的服务器的8080端口,而不是我们的Linux服务器本来的8080端口,这里应该改成你服务器的真实ip

# 负载均衡代理服务   weight 权重(根据服务器的性能)
upstream python-test {
	server 192.168.33.10:8000 weight=3; # 虚拟本地ip是192.168.33.10, 需要修改
	server 192.168.33.10:8001 weight=1;
}

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass http://python-test; # 请求的服务地址
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

ngnix -t 检查ngnix文件规范

输出一下为正常
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载配置

ngnix -s reload

本地访问ngnix代理的服务

http://192.168.33.10/index/ # 三次请求到8000端口,一次到8001端口

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值