配置方式,以及注意的地方分为两种,1:当前主机为nginx安装宿主机;2:nginx位于当前主机安装的docker容器中;
一、基于docker容器的nginx配置宿主机中的静态图片:
server {
listen 8004;
server_name 39.10.26.6;
#当前主机IP
charset utf-8;
location / {
# docker容器中nginx对应的图片地址;映射到当前主机图片地址
root /usr/share/nginx/html/upload;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
# 精细化 配置相关静态资源参数,优化访问静态资源文件
location ~ .*\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt|woff)$
{
root /usr/share/nginx/html/upload;
}
}
docker-compose映射当前主机图片地址方式
version: '3.7'
services:
nginx:
container_name: nginx
image: nginx:1.18.0-alpine
restart: always
privileged: true
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./data:/usr/share/nginx/html
- /data/shop/runtime/upload:/usr/share/nginx/html/upload
environment:
- TZ=Asia/Shanghai
ports:
- 89:89
- 82:82
说明:/data/shop/runtime/upload 为当前宿主机的图片地址; /usr/share/nginx/html/upload为docker容器nginx中的映射图片地址,该地址用于nginx.conf文件中server的root路径配置。
图片路径访问(图片宿主机图片地址:/data/shop/runtime/upload/1.png):
http://39.10.26.6:8004/1.png
二、基于当前主机的nginx配置中的静态图片,非docker容器的nginx:
server {
listen 8004;
server_name 39.10.26.6;
#当前主机IP
charset utf-8;
location / {
# 当前服务器中nginx对应的图片地址
root /usr/share/nginx/html/upload;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
# 精细化 配置相关静态资源参数,优化访问静态资源文件
location ~ .*\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt|woff)$
{
root /usr/share/nginx/html/upload;
}
}
说明:这里相比上面配置就比较简单了,省略了本地图片地址对nginx目录的映射
图片路径访问(图片宿主机图片地址:/usr/share/nginx/html/upload/1.png):
http://39.10.26.6:8004/1.png
1、原创文章,作者:诺米,如若转载,请注明出处:https://www.http3w.com/archives/581
2、本站内容若有雷同从属巧合,若侵犯了您的权益,请联系本站删除,E-mail: wtao219@qq.com