docker-compose搭建Nginx并配置CDN
docker-compose.yml 文件
version: '3.1'
services:
nginx:
image: nginx
container_name: nginx
restart: always
ports:
- 80:80
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./html:/usr/share/nginx/wwwroot ## 这个两边都随意写目录,只要确定能够找到就行(前面是宿主机,后面是docker容器机)
nginx.conf 文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name 需要访问的IP或者网址都可以;
location / {
## 刚才配置的文件目录,与docker容器机内的目录一致
root /usr/share/nginx/wwwroot/;
index index.html index.htm;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, DELETE, OPTIONS';
}
location ~* \.(eot|ttf|woff|woff2|svg|otf)$ {
add_header Access-Control-Allow-Origin *;
## 同上,docker-compose.yml里配置的文件目录,与docker容器机内的目录一致
root /usr/share/nginx/wwwroot/;
}
}
}