博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
docker上部署nginx容器80端口自动转443端口
阅读量:4560 次
发布时间:2019-06-08

本文共 4726 字,大约阅读时间需要 15 分钟。

拉去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 ~]# lsmysql  nginx   redis[root@iZm5eclei4hhnwn6mo9va6Z ~]#[root@iZm5eclei4hhnwn6mo9va6Z ~]# cd nginx[root@iZm5eclei4hhnwn6mo9va6Z nginx]#[root@iZm5eclei4hhnwn6mo9va6Z nginx]# lscerts  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.confuser  nginx;                                #运行nginx的用户worker_processes  1;                        #启动进程设置成和CPU数量相等error_log  /var/log/nginx/error.log warn;   #全局错误日志pid        /var/run/nginx.pid;              #PID文件的位置#工作模式及连接数上限events {    worker_connections  1024;               #单个后台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  65;                 #设置连接超时的事件    #gzip  on;                             #开启GZIP压缩    include /etc/nginx/conf.d/*.conf;}[root@iZm5eclei4hhnwn6mo9va6Z nginx]#

 

拷贝申请的阿里云ssl证书

[root@iZm5eclei4hhnwn6mo9va6Z nginx]# cd certs/[root@iZm5eclei4hhnwn6mo9va6Z certs]#[root@iZm5eclei4hhnwn6mo9va6Z certs]# ls2032088_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]# lsdefault.conf[root@iZm5eclei4hhnwn6mo9va6Z conf.d]#[root@iZm5eclei4hhnwn6mo9va6Z conf.d]# cat default.confserver {        server_name  cnbi.jiaxin365.cn;    #域名        listen 80;                         #侦听80端口        rewrite ^(.*) https://$server_name$1 permanent;       #${server_name}可以换成$host    }                                                         #设置http自动跳转httpsserver {
listen 443 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.2; # 指定密码为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 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { #重定向错误页面到 /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; #}}

 重启容器

# 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

转载于:https://www.cnblogs.com/djlsunshine/p/10671709.html

你可能感兴趣的文章
模版语言
查看>>
ural-1099-Work Scheduling(裸带花树)
查看>>
计算机四级网络工程师--《操作系统(Operating System)》重点内容学习
查看>>
Xamarin.Android-用ZXing实现二维码扫描以及连续扫描
查看>>
IDE theme(such as : Eclipse)
查看>>
【ALV】关于ALV
查看>>
CentOS7上解决tomcat不能被外部浏览访问
查看>>
使用Loadrunner进行http接口压力测试
查看>>
存储过程中set什么什么的讲解
查看>>
自己开发的csdn手机客户端
查看>>
ossfs挂载oss到ECS本地并设置权限
查看>>
基于zookeeper的MySQL主主负载均衡的简单实现
查看>>
使用struts2实现文件下载
查看>>
一个扫雷游戏和一个自动玩扫雷游戏的程序
查看>>
crontab 每月最后一天
查看>>
Cisco路由器DHCP配置浅析
查看>>
潭州Java中级班(day_06)
查看>>
使pdfLatex生成的文件支持复制
查看>>
[leedcode 147] Insertion Sort List
查看>>
JAVA分页
查看>>