由於使用docker配置Nginx比較方便,所以博主就使用docker做為容器配置下
第一步、配置docker-compose.yml文件
version: '3 services: nginx: restart: always image: nginx container_name: nginx-1 ports: - 80:80 volumes: - /usr/local/application/nginx/conf/nginx.conf:/etc/nginx/nginx.conf - /usr/local/application/nginx/logs/:/var/log/nginx/ - /usr/local/application/nginx/data/:/var/share/nginx/html/ - /usr/local/application/nginx/cdn/:/usr/share/nginx/html/
說明下上面的代碼的幾個重要部分
首先就是volumes這個節點的值:
第一個 配置文件的映射 前面是宿主機 后面是容器
第二個 日志映射
第三個 數據文件映射
第四個 cdn的映射
第二步、配置nginx.conf文件
nginx.fonf
user nginx; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; server { listen 80; server_name 192.168.157.128; location / { root /usr/share/nginx/html; 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 *; root /usr/share/nginx/html; } } }
說明下上面部分代碼的含義
第一個地方:root這個的值表示的是容器內的值,因為是映射為容器的conf文件。
第二個地方:三個add_header
第一個表示的是允許訪問的域,*表示統配,為了安全推薦自定義的域名
第二個表示的是是否帶Cookie
第三個表示的是允許的請求方式,GET和OPTIONS必須投
第三方地方:include和default_type這兩個的值表示的是設置請求的Content—Type的內容,如果不設置的話,在html里面使用CDN的css文件和js文件會在瀏覽器中報錯,不理解的COntent-Type類型。