请问如何用docker成功安装Nginx,还有配置https呢?
docker run --name nginx80 -d -p 80:80 -v /etc/docker/nginx80/conf:/etc/nginx/nginx.conf -v /usr/docker/nginx80/html:/usr/share/nginx/html -v /etc/docker/nginx80/conf.d:/etc/nginx/conf.d nginx
为什么我按照大多数网站上用docker配置Nginx会报错呢。。nginx.conf 本来就要替换虚拟机那个文件啊。。。。请指教。。
而且课程说会提供如何设置https的指导教程,具体在哪里呢?能详细一步步教我吗?网上的都不是docker的前提下设置的,如果是docker的话又不是自动续期的那种。。。到底该怎么办呢,能远程协助就最好了
正在回答 回答被采纳积分+1
nginx 的配置其实不难,先不要急躁,把思路想清楚。
首先,nginx 与 jenkins 是不是都在容器中?
那么要让 jenkins 走 nginx的网络,才能让 nginx 控制 jenkins 并且代理它。
所以,需要创建一个网络,docker network create https
然后把 nginx 与 jenkins加到同一个网络中来(docker-compose 中可以使用 external network)
docker network connect https nginx容器的名称
docker network connect https jenkins容器的名称
创建 nginx 容器docker-compose.yml
version: "3"
services:
web:
image: jms_nginx:1.5.5
container_name: "some-nginx"
restart: always
volumes:
# 宿主机的目录与容器目录进行映射
- /home/nginx/nginx.conf:/etc/nginx/nginx.conf
- /home/nginx/conf.d:/etc/nginx/conf.d
# SSL certs 宿主机的目录与容器目录进行映射
- /home/acme:/home/acme
ports:
- "80:80"
- "443:443"
networks:
- default
networks:
default:
external:
name: https
其次,是证书的申请,使用 acme.sh,官网:https://github.com/acmesh-official/acme.sh,你需要有一个域名,还需要有 acme 支持的 DNS 申请的厂商:https://github.com/acmesh-official/acme.sh/wiki/dnsapi
最后,有了证书之后就是配置 Nginx:
jenkins.conf 配置,放置在conf.d目录中
# Upstream server
upstream jenkinsserver {
server jenkins容器名称:8080 fail_timeout=0;
}
# listen on HTTP2/SSL
server {
listen 443 ssl http2;
server_name jenkins.toimc.com;
# ssl certs from letsencrypt
ssl_certificate /home/acme/fullchain.pem; # 密钥文件目录
ssl_certificate_key /home/acme/key.pem; # 密钥文件目录
# dhparam.pem
ssl_dhparam /home/acme/dhparam.pem; # dhparam.pem文件
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
# set proxy headers for cloudflare/jira
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# hand the request off to jira on non-ssl
proxy_pass http://jenkinsserver;
}
}
# redirect HTTP and handle let's encrypt requests
server {
listen 80;
server_name jenkins.toimc.com;
# root /var/lib/jira;
# handle letsencrypt domain validation
location ~ /.well-known {
allow all;
}
# send everything else to HTTPS
location / {
return 302 https://jenkins.toimc.com;
# set proxy headers for cloudflare/jira
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# hand the request off to jira on non-ssl
proxy_pass http://jenkinsserver;
}
}
nginx.conf 配置
user nobody nogroup;
pid /run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 65535;
events {
# 设置事件驱动模型,是内核2.6以上支持
use epoll;
worker_connections 65535;
accept_mutex off;
multi_accept off;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
send_timeout 120;
keepalive_timeout 300;
client_body_timeout 300;
client_header_timeout 120;
proxy_read_timeout 300;
proxy_send_timeout 300;
#tcp_nopush on;
types_hash_max_size 4096;
client_header_buffer_size 16m;
client_max_body_size 1024m;
include /etc/nginx/mime.types;
include /etc/nginx/conf.d/*.conf;
# include /usr/share/nginx/modules/*.conf;
default_type application/octet-stream;
# Logging Settings
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 开启gzip
gzip on;
# 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k;
# gzip 压缩级别,1-10,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明
gzip_comp_level 2;
# 进行压缩的文件类型。javascript有多种形式。其中的值可以在 mime.types 文件中找到。
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png font/ttf font/otf image/svg+xml;
# 是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;
# 禁用IE 6 gzip
gzip_disable "MSIE [1-6]\.";
}
配置完成之后可以使用
docker exec -it nginx容器的名称 nginx -t 来测试
[root@imooc nginx]# docker exec -it some-nginx nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
如果看到/etc/nginx/nginx.conf syntax is ok说明 nginx 的配置没有问题。
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星