虚拟机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端口