一、使用docker-compose部署nginx
1.上传docker-compose文件
version: '3.1'
services:
nginx:
image: nginx:1.21.6 # 镜像名称
container_name: nginx # 容器名字
restart: always # 开机自动重启
ports: # 端口号绑定(宿主机:容器内)
- "80:80"
- "443:443"
volumes: # 目录映射(宿主机:容器内)
- .nginx/conf/nginx.conf:/etc/nginx/nginx.conf ###配置文件
- .nginx/log:/var/log/nginx ###这里面放置日志
- .nginx/html:/usr/share/nginx/html ###这里面放置前端资源目录
#nginx配置动态修改
command: /bin/sh -c "envsubst < /etc/nginx/conf.d/nginx_web.conf.temp > /etc/nginx/conf.d/nginx_web.conf && exec nginx -g 'daemon off;'"
2.创建目录
mkdir nginx/conf
mkdir nginx/log
mkdir nginx/html
3.nginx配置文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
#该ip是虚拟机地址,根据实际情况修改。
server_name 192.168.0.110;
location / {
# Nginx解决浏览器跨域问题
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,PATCH,OPTIONS;
root /html;
index index.html index.htm;
}
location /abc {
default_type text/plain;
return 200 "access success";
}
location /api {
# 填写需要跳转的API地址 ,根据实际情况修改。
proxy_pass http://192.168.0.3:8789/data;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Nginx解决浏览器跨域问题
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,PATCH,OPTIONS;
}
#nginx防盗链设置
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|mp4|m4v|ogg|mp3|pdf|docx|doc|wma|xls|ppt|mid|flv|zip|rar|swf|json)$
{
root /usr/share/nginx/html;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Headers *;
#expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
expires 30d;
}
}
}
4.将index.html放到html文件夹下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>docke-compose搭建nginx</title>
</head>
<body>
<h1>docke-compose搭建nginx映射成功</h1>
</body>
</html>
5.使用docker-compose启动nginx
docker-compose up -d
6.验证
二、nginx作静态文件服务器
还记得刚才我们配置的这个目录挂载嘛?
- .nginx/html:/usr/share/nginx-proxy/html ###这里面放置前端资源目录
此时我们可以将任意一张图片放到这个文件夹下
此时,我们输入网址+图片的全名称+图片后缀即可在浏览器下载此图片。
为什么不用输入/home/skytree/nginx-proxy/html这个全路径呢,因为我们在上面已经将/nginx-proxy/html给挂在出去了,直接访问服务器地址就可以到这个目录下。效果如下:
三、nginx防盗链
在上面已经配置了防盗链,如下:
#nginx防盗链设置
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|mp4|m4v|ogg|mp3|pdf|docx|doc|wma|xls|ppt|mid|flv|zip|rar|swf|json)$
{
root /usr/share/nginx/html;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Headers *;
#expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
expires 30d;
}
详细说明:
location ~ .*\.(png|jpg|gif)$
location:输入的url后面跟的路径,如果路径与location后的内容相匹配就执行location下的内容
~:开始正则匹配
.*:.表示出换行符/n之外的所有字符,*就是匹配0次或多次。
\.:\转译符号将具有特殊含义的字符,转为纯字符型,.这里有包含任意字符的意思,所以需要转译
(png|jpg|gif)$:表示结尾要是以png、jpg、gif为结尾
vaild_referers none blocked www.baidu.com
这个是如果返回的头部referer为www.baidu.com那么就不会执行valid_referer下面内容,反之就会
if ($invalid_referer)
这个含义就是结合上面的valid_referer,如果请求头部为www.baidu.com,那么就$invalid_referer就会被赋值为0,就不会执行返回错误代码401,如果不是www.baidu.com,那么就会赋值为1,就会执行下面内容