nginx代理docker容器服务

编程入门 行业动态 更新时间:2024-10-28 10:27:55

nginx代理docker<a href=https://www.elefans.com/category/jswz/34/1771431.html style=容器服务"/>

nginx代理docker容器服务

场景描述

避免暴力服务端口,使用nginx代理

一个前端,一个后端,docker方式部署到服务器,使用docker创建的nginx代理端口请求到前端端口

过程

1 docker 安装nginx

1.1 安装一个指定版本的nginx

docker pull nginx#启动一个nginx
docker run --name nginx -d -p 80:80 nginx

1.2 进入容器,将conf下的文件和conf.d的文件复制出来

可以直接使用1.4的文件,则跳过1.2 1.3

可以使用docker cp命令进行拷贝

#进入容器 
docker exec -it 9ff2ecdf6245 /bin/bash
#进入/etc/nginx目录
cd /etc/nginx
# 查看nginx.conf 并拷贝
# 查看conf.d目录下的 default.conf 并拷贝

1.3 在/home下创建docker/nginx目录,用于存放nginx配置文件

# 在/home/docker/nginx目录下创建三个文件夹
# 分别是 conf  conf.d  logs# 在conf中创建nginx.conf,并将刚才nginx容器内的粘贴到此# 在conf.d目录下创建 default.conf 并将刚才nginx容器内的粘贴到此

1.4 拷贝的文件

default.conf 

server {listen       80;listen  [::]:80;server_name  填写你的服务器ip或者域名请求uri,如 http://localhost/remark#access_log  /var/log/nginx/host.access.log  main;location / {# root   /usr/share/nginx/html;# index  index.html index.htm;proxy_pass http://172.17.0.4:9011; # 填写你的需要代理服务的ip和端口,见1.5}#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;#}
}

nginx.conf

user  nginx;
worker_processes  auto;error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/mime.types;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;#gzip  on;include /etc/nginx/conf.d/*.conf;
}

1.5 查看前端服务的docker运行ip

docker内代理,需要使用docker的ip或者容器名称

注意IPAddress位置即是当前容器在docker中的ip地址,将其写入nginx的server proxy中,端口为容器内部端口,非服务器端口

docker inspect 容器名/id

1.6 重新启动nginx容器

删掉刚才的nginx容器

启动时映射配置文件

# 服务器80 对应nginx80
# 映射配置文件 conf目录下的nginx.conf文件 到 /etc/nginx/nginx.conf
# 映射配置文件 conf.d目录 到 /etc/nginx/conf.d
# 映射配置文件 logs 目录 到 /var/log/nginxdocker run -d -p 80:80  --name nginxContainer -v /home/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/docker/nginx/conf.d:/etc/nginx/conf.d -v /home/docker/nginx/logs:/var/log/nginx nginx

启动完成 在浏览器中访问 服务器ip或者域名 跳转到前端服务

这里访问的ip或者域名即对应着 default.conf 下的server的server_name

更多推荐

nginx代理docker容器服务

本文发布于:2023-11-15 00:00:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1590097.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   nginx   docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!