docker nginx监听80端口 同一 IP 多域名配置方法--多子配置文件包含 https
下载nginx镜像文件
docker pull nginx:1.24.0
宿主机上创建nginx_80 目录 html cert conf logs
创建 配置文件nginx.conf
一、Nginx 配置文件 nginx.conf
操作:在 http 模块增加(子配置文件的路径和名称):include /etc/nginx/conf.d/*.conf;
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 650;
gzip on;
#client_header_buffer_size 1280m;
#large_client_header_buffers 4 1280m;
client_max_body_size 1024M; # 允许客户端请求的最大单文件字节数
client_body_buffer_size 1024M;
#client_body_buffer_size 2560m; # 缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
include /etc/nginx/conf.d/*.conf;
}
二、Nginx 子配置文件
路径:/etc/nginx/conf.d 目录下
子配置文件例子:test.csydedu.com.conf
server {
listen 80;
listen [::]:80;
#server_name localhost;
#server_name 192.168.1.98;
server_name test.csydedu.com;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
# 真正提供服务的ip和端口 test.csydedu.com 域名解析反向代理到这个地址提供服务
proxy_pass http://23.100.60.201:9000;
root /usr/share/nginx/html;
index index.html index.htm;
# 设置是否允许 cookie 传输
add_header Access-Control-Allow-Credentials true;
# 允许请求地址跨域 * 做为通配符
add_header Access-Control-Allow-Origin * always;
# 允许跨域的请求方法
add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
}
#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 {
root /usr/share/nginx/html;
}
#location /prod-api/ {
# #mcyl_test0 容器名称 反向代理到 后台服务
# proxy_pass http://221.122.78.184:9067/;
# }
#location /api/uploads/{
# root /docker-root/lms/admin/upload_root/;
# autoindex on;
# }
# 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;
#}
}
server {
listen 443 ssl; #SSL协议访问端口号为443。此处如未添加ssl,可能会造成Nginx无法启动。
listen [::]:443 ssl;
server_name test.csydedu.com; #将localhost修改为您证书绑定的域名,例如:www.example.com。
#ssl on;
#root html;
#index index.html index.htm;
ssl_certificate /cert/12442796_ytj.163smart.cn.pem; #将domain name.pem替换成您证书的文件名。
ssl_certificate_key /cert/12442796_ytj.163smart.cn.key; #将domain name.key替换成您证书的密钥文件名。
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。
ssl_prefer_server_ciphers on;
location / {
root /usr/share/nginx/html/lms;
index index.html index.htm;
}
#location /api/ {
# # 反向代理到 后台服务
# #proxy_pass http://mcyl_test0:8080/;
# proxy_pass http://8.130.162.107:9062/;
# }
location / {
# 真正提供服务的ip和端口 test.csydedu.com 域名解析反向代理到这个地址提供服务
proxy_pass http://23.100.60.201:9000;
}
}
接着创建第二个域名的配置文件 同上
三、创建容器
#使用ssl证书 https访问 多一个443的端口映射
docker run --name nginx-80-1.24 --restart=always -d -p 80:80 -p 443:443 \
-v /mnt/docker-root/nginx_80/html:/usr/share/nginx/html \
-v /mnt/docker-root/nginx_80/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /mnt/docker-root/nginx_80/conf/conf.d:/etc/nginx/conf.d \
-v /mnt/docker-root/nginx_80/logs:/var/log/nginx \
-v /mnt/docker-root/nginx_80/cert:/cert/ \
--network my-net --log-opt max-size=10m --log-opt max-file=3 nginx:1.24.0
访问 test.csydedu.com 打开目标网站。