Nginx根据路径设置静态资源

mac2024-03-22  34

Nginx根据路径设置静态资源

 

示例nginx.conf

user root; worker_processes auto; error_log /var/log/nginx/error.log; pid /usr/local/nginx/logs/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #access_log logs/access.log main; client_max_body_size 20m; sendfile on; tcp_nopush on; keepalive_timeout 65; gzip on; # 设置缓存的路径和其他参数 # proxy_cache_path path [levels=levels] keys_zone=name:size [inactive=time] [max_size=size] [loader_files=number] [loader_sleep=time] [loader_threshold=time]; # 缓存路径 /data/nginx/cache 缓存结构为 2 层,即该路径下会有 2 层子目录,缓存文件会保存在最下层子目录 # 缓存的 key 会保存在名为 web_cache 的内存区域,该内存区域大小为 50 m # 10 分钟内缓存没有被访问就会过期 # 缓存文件最多占用 1g 空间 proxy_cache_path ./web_cache levels=1:2 keys_zone=web_cache:1024m inactive=1000m max_size=1g; server { listen 8040 ssl; server_name shengtai.brc-ulife.com; ssl_certificate /root/.caddy/acme/acme-v02.api.letsencrypt.org/sites/shengtai.brc-ulife.com/shengtai.brc-ulife.com.crt; ssl_certificate_key /root/.caddy/acme/acme-v02.api.letsencrypt.org/sites/shengtai.brc-ulife.com/shengtai.brc-ulife.com.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; index index.html index.htm; # 开启gzip gzip on; # 启用gzip压缩的最小文件,小于设置值的文件将不会压缩 gzip_min_length 1k; # gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明 gzip_comp_level 3; # 进行压缩的文件类型。javascript有多种形式。其中的值可以在 mime.types 文件中找到。 gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml; # 是否在http header中添加Vary: Accept-Encoding,建议开启 gzip_vary on; # 禁用IE 6 gzip gzip_disable "MSIE [1-6]\."; # 设置压缩所需要的缓冲区大小 gzip_buffers 32 4k; # 设置gzip压缩针对的HTTP协议版本 gzip_http_version 1.0; location / { root /root/jeecg/upFiles/; index index.html index.htm; } location ^~ /jeecg-boot/sys/common/view { alias /root/jeecg/upFiles/; index index.html index.htm; } #后台服务配置,配置了这个location便可以通过http://域名/jeecg-boot/xxxx 访问 location ^~ /jeecg-boot { proxy_pass http://172.18.231.224:8080/jeecg-boot/; proxy_set_header Host 172.18.231.224; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }

 

配置路径

配置当访问 http://ip:port/jeecg-boot/sys/common/view访问 /root/jeecg/upFiles/ 路径下时,请使用 alias,如果使用root,则nginx则查询/root/jeecg/upFiles/jeecg-boot/sys/common/view路径下

alias配置

location ^~ /jeecg-boot/sys/common/view { alias /root/jeecg/upFiles/; index index.html index.htm; }

root配置

location / { root /root/jeecg/upFiles/; index index.html index.htm; }

 

最新回复(0)