欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

docker上部署nginx容器80端口自动转443端口

发布时间:2023/12/15 编程问答 25 豆豆
生活随笔 收集整理的这篇文章主要介绍了 docker上部署nginx容器80端口自动转443端口 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

拉去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 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]# 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; #侦听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;
# 增加sslssl 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.htmlroot /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

总结

以上是生活随笔为你收集整理的docker上部署nginx容器80端口自动转443端口的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。