本文档是基本docker 安装nginx做相关配置
1.首先用docker启动一个nginx,从docker容器中把相关的配置文件复制到主机路径:
1.1 写法规则:docker cp 容器名称或者ID:容器内部配置文件路径 本地存放配置文件路路径
docker cp nginx:/etc/nginx/conf.d/default.conf /root/hktransfer
1.2 需要cp 出来的配置文件:
docker cp nginx:/etc/nginx/nginx.conf /home/docker-nginx/ #配置文件
docker cp nginx:/etc/nginx/conf.d/ /home/docker-nginx/conf/ #默认的配置文件
docker cp nginx:/usr/share/nginx/html/ /home/docker-nginx/html/ #默认请求的显示页面文件
docker cp nginx:/var/log/nginx/ /home/docker-nginx#日志文件
2.修改响应的配置文件:配置对应的https和对应负载均衡策略
upstream backend {
server 172.18.120.53:9997; # 第一个 Tomcat 服务器的本地端口
server 172.18.120.90:9997; # 第二个 Tomcat 服务器的本地端口
}
server {
listen 80;
listen [::]:80;
server_name localhost;
ssl on;
ssl_certificate /etc/nginx/conf.d/crt/server.pem; #这里是ssl key文件存放的绝对路径,根据自己的文件名称和路径来写
ssl_certificate_key /etc/nginx/conf.d/crt/server.key; #这里是ssl key文件存放的绝对路径,根据自己的文件名称和路径来写
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass https://backend$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
}
# 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;
#}
}
3.启动的nginx.yml配置写法
version: '2'
services:
nginx:
restart: always
image: nginx:latest
container_name: hk-nginx
volumes:
- /home/docker-nginx/nginx.conf:/etc/nginx/nginx.conf
- /home/docker-nginx/conf:/etc/nginx/conf.d
- /home/docker-nginx/html:/usr/share/nginx/html
- /home/docker-nginx/logs:/var/log/nginx
ports:
- 81:80
- 443:443
4.启动命令
docker-compose -f nginx.yml up -d