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@server01 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@server01 nginx]# ll
总用量 20
-rw-r--r-- 1 root root 2118 1月 19 17:10 default.conf
-rw-r--r-- 1 root root 2113 1月 19 15:18 default.conf.bak
-rw-r--r-- 1 root root 1048 1月 19 14:55 nginx.conf
-rw-r--r-- 1 root root 1363 1月 19 15:40 tls.crt
-rw-r--r-- 1 root root 1704 1月 19 15:40 tls.key
[root@server01 nginx]#
[root@server01 nginx]# cat default.conf
server {
server_name cnbi.jiaxin365.cn; #域名
listen 80; #侦听80端口
rewrite ^(.*) https://$server_name$1 permanent; #${server_name}可以换成$host
} #设置http自动跳转https
server {
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 run --name nginx -p 80:80 -p 443:443 -p 8080:8080 -v /nginx/nginx.conf:/etc/nginx/nginx.conf -v /nginx/default.conf:/etc/nginx/conf.d/default.conf -v /nginx/tls.crt:/etc/nginx/certs/tls.crt -v /nginx/tls.key:/etc/nginx/certs/tls.key -d nginx:latest
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-01-19 Git详解及 github与gitlab使用
2020-01-19 mininet命令
2020-01-19 Mininet简介
2020-01-19 Mininet实验 源码安装Mininet