{"id":388,"date":"2019-05-30T10:03:38","date_gmt":"2019-05-30T01:03:38","guid":{"rendered":"http:\/\/localhost:8000\/?p=388"},"modified":"2021-01-23T10:07:13","modified_gmt":"2021-01-23T01:07:13","slug":"docker-compose-nginx-https","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2019\/05\/docker-compose-nginx-https.html","title":{"rendered":"Docker Compose\u4e0a\u306eNginx\u3092HTTPS\u5bfe\u5fdc\u3059\u308b"},"content":{"rendered":"

\u30ed\u30fc\u30ab\u30eb\u958b\u767a\u7528\u306bDocker Comnpose\u3067\u52d5\u304b\u3057\u3066\u3044\u308bNginx\u3092HTTPS\u5bfe\u5fdc\u3059\u308b\u5fc5\u8981\u304c\u51fa\u3066\u304d\u305f\u306e\u3067\u3001\u81ea\u4f5c\u306eSSL\u8a3c\u660e\u66f8\u3092\u4f7f\u3063\u3066\u5bfe\u5fdc\u3059\u308b\u3053\u3068\u306b\u3057\u305f\u3002<\/p>\n

openssl\u3067\u79d8\u5bc6\u9375\u3001\u7f72\u540d\u30ea\u30af\u30a8\u30b9\u30c8\u3001SSL\u8a3c\u660e\u66f8\u3092\u4f5c\u6210<\/h2>\n

ssl\u3068\u3044\u3046\u30d5\u30a9\u30eb\u30c0\u3092\u4f5c\u6210\u3059\u308b<\/h3>\n
mkdir ssl\ncd ssl<\/code><\/pre>\n

\u79d8\u5bc6\u9375\uff08KEY\uff09\u3092\u4f5c\u6210\u3059\u308b<\/h3>\n

\u6697\u53f7\u306b\u306fRSA 2048\u30d3\u30c3\u30c8\u3092\u4f7f\u7528\u3059\u308b\u3002<\/p>\n

openssl genrsa 2048 > server.key<\/code><\/pre>\n

\u7f72\u540d\u30ea\u30af\u30a8\u30b9\u30c8\uff08CSR\uff09\u3092\u751f\u6210\u3059\u308b<\/h3>\n

SSL\u30b5\u30fc\u30d0\u8a3c\u660e\u66f8\u767a\u884c\u306b\u5fc5\u8981\u306a\u30c6\u30ad\u30b9\u30c8\u30c7\u30fc\u30bf\u3067\u3001\u8a8d\u8a3c\u5c40\u306f\u3053\u306e\u30c6\u30ad\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u5143\u306b\u5185\u5bb9\u3092\u7cbe\u67fb\u3057\u3066SSL\u8a3c\u660e\u66f8\u3092\u767a\u884c\u3059\u308b\u3089\u3057\u3044\u3002\uff08\u4eca\u56de\u306f\u81ea\u4f5c\u3059\u308b\u306e\u3067\u9069\u5f53\u3067OK\uff09<\/p>\n

openssl req -new -key server.key > server.csr\n> Country Name (2 letter code) []:81\n> State or Province Name (full name) []:tokyo\n> Locality Name (eg, city) []:tokyo\n> Organization Name (eg, company) []:none\n> Organizational Unit Name (eg, section) []:none\n> Common Name (eg, fully qualified host name) []:rinoguchi.com\n> Email Address []:hoge@gmail.com\n> A challenge password []:****<\/code><\/pre>\n

SSL\u8a3c\u660e\u66f8\uff08CRT\uff09\u3092\u751f\u6210\u3059\u308b<\/h3>\n

x509\u306f\u516c\u958b\u9375\u57fa\u76e4\u306e\u898f\u683c\u306e\u540d\u524d\u3089\u3057\u3044\u3002100\u5e74\u306e\u8a2d\u5b9a\u3067SSL\u8a3c\u660e\u66f8\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n

openssl x509 -days 36500 -req -signkey server.key < server.csr > server.crt<\/code><\/pre>\n

Docker\u306e\u8a2d\u5b9a<\/h2>\n

Docker\u30b3\u30f3\u30c6\u30ca\u306e443\u30dd\u30fc\u30c8\u3092\u516c\u958b\u3059\u308b<\/h3>\n

\u30b3\u30f3\u30c6\u30ca\u5074\u306e80\uff08http\uff09\u3068443\uff08https\uff09\u30dd\u30fc\u30c8\u3092\u516c\u958b\u3057\u3001\u30db\u30b9\u30c8\u5074\u306e\u540c\u4e00\u30dd\u30fc\u30c8\u756a\u53f7\u3068\u7d10\u3065\u3051\u308b\u3002<\/p>\n

version: '3'\nservices:\n  mynginx:\n    build: .\n    ports:\n      - "80:80"\n      - "443:443"<\/code><\/pre>\n

SSL\u95a2\u4fc2\u306e\u30d5\u30a1\u30a4\u30eb\u3092Docker\u30a4\u30e1\u30fc\u30b8\u306b\u8ffd\u52a0\u3059\u308b<\/h3>\n

Dockerfile\u306b\u3066\u3001\u4f5c\u6210\u3057\u305fSSL\u95a2\u4fc2\u306e\u30d5\u30a1\u30a4\u30eb\u3092\/etc\/nginx\/ssl<\/code>\u306b\u8ffd\u52a0\u3059\u308b\u8a2d\u5b9a\u3092\u884c\u3046\u3002<\/p>\n

FROM nginx:latest\nADD default.conf \/etc\/nginx\/conf.d\nADD ssl \/etc\/nginx\/ssl<\/code><\/pre>\n

Nginx\u306e\u8a2d\u5b9a<\/h2>\n

default.conf\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a2d\u5b9a\u3092\u884c\u3046\u3002<\/p>\n

server {\n    listen              80;\n    server_name         rinoguchi.com;\n    return              301                 https:\/\/$host$request_uri;\n}\n\nserver {\n    listen              443                 ssl;\n    server_name         rinoguchi.com;\n    ssl_certificate     \/etc\/nginx\/ssl\/server.crt; #SSL\u8a3c\u660e\u66f8\n    ssl_certificate_key \/etc\/nginx\/ssl\/server.key; #\u79d8\u5bc6\u9375\n\n    location \/ {\n        proxy_pass          http:\/\/host.docker.internal:8080\/;\n        proxy_redirect      default;\n    }\n}<\/code><\/pre>\n