拉去nginx镜像

# docker pull nginx

运行nginx容器config用于拷贝nginx配置文件

# docker run --name nginxconfig -d docker.io/nginx

# docker cp nginxconfig:/etc/nginx/ /root/

删除

# docker stop nginxconfig

# docker rm nginxconfig

创建服务nginx容器

# docker run --name nginx -p 80:80 -p 443:443 -v /root/nginx/:/etc/nginx/ -d docker.io/nginx

  • 映射端口443,用于https请求
  • 映射端口80,用于http请求

nginx配置文件如下(不做任何修改)

[root@iZm5eclei4hhnwn6mo9va6Z ~]# ls
mysql nginx redis
[root@iZm5eclei4hhnwn6mo9va6Z ~]#
[root@iZm5eclei4hhnwn6mo9va6Z ~]# cd nginx
[root@iZm5eclei4hhnwn6mo9va6Z nginx]#
[root@iZm5eclei4hhnwn6mo9va6Z nginx]# ls
certs conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
[root@iZm5eclei4hhnwn6mo9va6Z nginx]#
[root@iZm5eclei4hhnwn6mo9va6Z nginx]# cat nginx.conf
user nginx; #运行nginx的用户
worker_processes ; #启动进程设置成和CPU数量相等 error_log /var/log/nginx/error.log warn; #全局错误日志
pid /var/run/nginx.pid; #PID文件的位置 #工作模式及连接数上限
events {
worker_connections ; #单个后台work进程最大并发数设置为1024
}
http {
include /etc/nginx/mime.types; #设定mime类型
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 ; #设置连接超时的事件 #gzip on; #开启GZIP压缩 include /etc/nginx/conf.d/*.conf;
}
[root@iZm5eclei4hhnwn6mo9va6Z nginx]#

拷贝申请的阿里云ssl证书

[root@iZm5eclei4hhnwn6mo9va6Z nginx]# cd certs/
[root@iZm5eclei4hhnwn6mo9va6Z certs]#
[root@iZm5eclei4hhnwn6mo9va6Z certs]# ls
2032088_cnbi.jiaxin365.cn.key 2032088_cnbi.jiaxin365.cn.pem
[root@iZm5eclei4hhnwn6mo9va6Z certs]#
[root@iZm5eclei4hhnwn6mo9va6Z certs]# pwd
/root/nginx/certs

配置http自动跳往https

[root@iZm5eclei4hhnwn6mo9va6Z nginx]# cd conf.d/
[root@iZm5eclei4hhnwn6mo9va6Z conf.d]#
[root@iZm5eclei4hhnwn6mo9va6Z conf.d]# pwd
/root/nginx/conf.d
[root@iZm5eclei4hhnwn6mo9va6Z conf.d]#
[root@iZm5eclei4hhnwn6mo9va6Z conf.d]# ls
default.conf
[root@iZm5eclei4hhnwn6mo9va6Z conf.d]#
[root@iZm5eclei4hhnwn6mo9va6Z conf.d]# cat default.conf
server {
server_name cnbi.jiaxin365.cn; #域名
listen ; #侦听80端口
rewrite ^(.*) https://$server_name$1 permanent; #${server_name}可以换成$host
} #设置http自动跳转https server {
listen ssl; #侦听443端口
server_name cnbi.jiaxin365.cn; #域名 #charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
# 增加ssl
ssl on; #如果强制HTTPs访问,这行要打开
ssl_certificate /etc/nginx/certs/2032088_cnbi.jiaxin365.cn.pem;
ssl_certificate_key /etc/nginx/certs/2032088_cnbi.jiaxin365.cn.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1.; # 指定密码为openssl支持的格式
ssl_ciphers HIGH:!aNULL:!MD5; # 密码加密方式
ssl_prefer_server_ciphers on; # 依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码
location / { # 定义首页索引目录和名称
root /usr/share/nginx/html;
index index.html index.htm;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html { #重定向错误页面到 /50x.html
root /usr/share/nginx/html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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;
#}
}

重启容器

# docker restart nginx

查看容器是否启动成功

# docker ps -a

打开浏览器测试

参考博客:

nginx访问http80端口跳转https443端口

https://yq.aliyun.com/articles/556481

docker安装nginx并配置通过https访问

https://www.jianshu.com/p/5f9bd492f186

nginx配置ssl证书实现https访问

https://www.cnblogs.com/tianhei/p/7726505.html

nginx 80端口重定向到443端口

https://www.cnblogs.com/lxwphp/p/9820005.html

https://yq.aliyun.com/articles/601562

https://blog.csdn.net/weixin_31655741/article/details/82226688

docker上部署nginx容器80端口自动转443端口的更多相关文章

  1. docker - compose 部署 Nginx

    主要介绍 docker 中 Nginx 的部署及项目目录挂载券的方法.docker 中部署一个服务,有三种方法,分别是 docker run.Dockerfile.docker-compose . 下 ...

  2. 在Docker下部署Nginx

    在Docker下部署Nginx 在Docker下部署Nginx,包括: 部署一个最简单的Nginx,可以通过80端口访问默认的网站 设置记录访问和错误日志的路径 设置静态网站的路径 通过proxy_p ...

  3. docker上启动nginx,并配置修改nginx的配置文件

    1.使用docker 下载nginx 镜像  docker pull nginx 2.启动nginx docker run --name nginx -p 80:80 -d nginx 这样就简单的把 ...

  4. 阿里云服务器安装Docker并部署nginx、jdk、redis、mysql

    阿里云服务器安装Docker并部署nginx.jdk.redis.mysql 一.安装Docker 1.安装Docker的依赖库 yum install -y yum-utils device-map ...

  5. 定期从Docker上部署的MySQL备份数据

    前段时间公司停电,正巧赶上周一领导要开会要过一遍项目,然而项目所依赖的MySQL数据库是直接部署在宿主机,且因为各人部署方式不同的原因,花了很久才在开会前启动起来.于是开完会后,我第一件事就是把原先依 ...

  6. Docker Compose部署 nginx代理Tomcat集群

    一.简介 使用Docker镜像部署Nginx代理的多个Tomcat集群: 使用Dockerfile构建nginx镜像 使用Dockerfile构建tomcat镜像 mysql镜像使用docker hu ...

  7. docker上构建redis容器

    1.查看docker上的镜像 [root@holly ~]# docker images 2.搜索docker上的redis镜像,选择下载的版本 [root@holly ~]# docker sear ...

  8. 在Docker上部署自动更新ssl证书的nginx + .NET CORE

    突发奇想要搞一个ssl的服务器,然后我就打起了docker的主意,想着能不能搞一个基于Docker的服务器,这样维护起来也方便一点. 设想 想法是满足这么几点: .NET CORE on Docker ...

  9. docker一键搭建Nginx+PHP环境(含自动部署命令)

    文章的主要部分是一步一步的教程,文章的最后是我整理好的一键安装命令,自动下载并安装docker,构建镜像,启动容器集群(压缩包内注释覆盖范围达到80%) 大家可以看完教程亲自尝试下,也可以直接执行一键 ...

随机推荐

  1. 中国历史人物传记数据库 CBDB 若干表简介

    ''' 推荐使用SQLite版本的CBDB数据库 推荐使用SQlite Studio进行数据库的操作 免费,可视化操作,轻量级应用,无需配置,学习扩展性好,非常适合广大历史系学生. ''' 一 人物基 ...

  2. 禁用cookie后的方法

    保存session id的方式可以采用cookie,这样在交互过程中浏览器可以自动的按照规则把这个标识发送给 服务器.一般这个cookie的名字都是类似于SEEESIONID.但cookie可以被人为 ...

  3. linux软件管理 YUM命令

    yum的优点 将所有软件包放到官方服务器上,当进行yum在线安装时可以自动解决依赖性问题 yum源文件 [root@ssgao1987 yum.repos.d]# cd /etc/yum.repos. ...

  4. bzoj1452

    题解: 二位树状数组 然后我开了300*300*300就T了 代码: #include<bits/stdc++.h> using namespace std; ; ],q; int fin ...

  5. 【转载】Oracle 中count(1) 、count(*) 和count(列名) 函数的区别

    1)count(1)与count(*)比较: 1.如果你的数据表没有主键,那么count(1)比count(*)快2.如果有主键的话,那主键(联合主键)作为count的条件也比count(*)要快3. ...

  6. 软工作业No.7 甜美女孩第五周--测试与发布

    Alpha版本发布说明 一.功能介绍 本团队所做的多模式自定义2048是用来进行纸牌模式以及正常基础模式版本的2048小游戏的.Alpha版本具有的功能大体如下: 初始界面- 纸牌模式- 基础模式- ...

  7. slick插件一些配置

  8. asp.net 网页跳转的几种常用方法

    Response.Redirect("http://www.baidu.com",false); 后面的bool值为是否停止执行当前页.跳转向新的页面,原窗口被代替.浏览器中的UR ...

  9. 帝国cms自动保存图片

    <?=ECMS_ShowEditorVar("newstext",$ecmsfirstpost==1?"":stripSlashes($r[newstex ...

  10. XXS level9

    (1)查看PHP源代码 <?php ini_set("display_errors", 0); $str = strtolower($_GET["keyword&q ...