『中級篇』docker之執行nginx跳轉到本地的jar並配置https(番外篇)(80)

IT人故事會發表於2018-11-05

原創文章,歡迎轉載。轉載請註明:轉載自IT人故事會,謝謝!
原文連結地址:『中級篇』docker之執行nginx跳轉到本地的jar並配置https(番外篇)(80)

真實的環境,一個1核2g記憶體,安裝了mysql容器,java容器,nginx容器。具體安裝docker我就不說了,沒啥講的重複很多遍的。mysql 也之前講過。

nginx的安裝

  • 新建立nginx的目錄
nginx
複製程式碼
  • 防止出現下面的錯誤
docker: Error response from daemon: oci runtime error: container_linux.go:262: starting container process caused "process_linux.go:339: container init caused "rootfs_linux.go:57: mounting \"/root/conf/nginx/nginx.conf\" to rootfs 
\"/var/lib/docker/aufs/mnt/edd3b9a8923f13404d42aa73f509b625c7370791c8
25424427eacae6432fecc8\" at \"/var/lib/docker/aufs/mnt/edd3b9a8923f13404d42aa73f509b625c7370791c8
25424427eacae6432fecc8/etc/nginx/nginx.conf\" caused \"not a directo
![](http://upload-images.jianshu.io/upload_images/11223715-3407e1c7ac8d7935?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)ry\"""
: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
複製程式碼
  • 命令建立

確定資料夾存在

touch conf/nginx.conf
echo 1 > conf/nginx.conf
複製程式碼

建立容器

docker run -d -p 80:80 \
--name five-nginx \
-p 443:443 \
-v /home/user/nginx/cert:/etc/nginx/cert \
-v /home/user/nginx/config/nginx.conf:/etc/nginx/nginx.conf \
-v /home/user/nginx/html:/usr/share/nginx/html \
-v /home/user/nginx/log:/var/log/nginx \
hub.c.163.com/library/nginx
複製程式碼
  • 配置nginx檔案
『中級篇』docker之執行nginx跳轉到本地的jar並配置https(番外篇)(80)
#user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    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;
    server {
        listen       80;
        server_name  www.abc.xyz;
        rewrite ^ https://$http_host$request_uri? permanent;
    }
    server {
        listen 443;
        server_name www.abc.xyz;
        ssl on;
        ssl_certificate /etc/nginx/cert/www.idig8.xyz.crt;
        ssl_certificate_key /etc/nginx/cert/www.idig8.xyz.key;
        ssl_session_timeout 5m;
        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://172.17.0.3:8888/;
            proxy_redirect off;
        }
    }
}

複製程式碼
  • java專案啟動使用的是8888埠

nginx的配置 proxy_pass 不能使用127.0.0.1:8888
必須使用java容器內部的ip地址http://172.17.0.3:8888/; 可以通過docker inspect 來進行檢視。

  • https的配置
『中級篇』docker之執行nginx跳轉到本地的jar並配置https(番外篇)(80)

把cert阿里雲中下載的crt,key放入指定的目錄cert中。

PS:基本的nginx容器配置祕鑰和key,跳轉返回的時候,訪問java容器的IP。

『中級篇』docker之執行nginx跳轉到本地的jar並配置https(番外篇)(80)


相關文章