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自動跳轉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 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM