侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

Nginx 静态资源转发配置

2023-12-19 星期二 / 0 评论 / 0 点赞 / 136 阅读 / 3896 字

1.修改nginx.conf配置文件 user nobody;# 工作进程的属主worker_processes 4;#推荐worker数为cpu核数,避免cpu不必要的上下文切换error_lo

1.修改nginx.conf配置文件

user  nobody;# 工作进程的属主worker_processes  4;#推荐worker数为cpu核数,避免cpu不必要的上下文切换error_log   /usr/local/nginx/logs/error.log;#error_log  /usr/local/nginx/logs/error.log  notice;#error_log  /usr/local/nginx/logs/error.log  info;#pid         /usr/local/nginx/logs/nginx.pid;events {    use epoll;#Linux 下性能最好的 event 模式,macOS下无法启用此模式!    #表示每个worker进程所能建立连接的最大值    #一个nginx最大的连接数max=worker_connections*worker_processes;    #对于http请求本地资源最大并发数量为max    #如果http作为反向代理,最大并发数为max/2。因为每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。    worker_connections  2048;}http {    include       mime.types;    default_type  application/octet-stream;    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    #                  '$status $body_bytes_sent "$http_referer" '    #                  '"$http_user_agent" "$http_x_forwarded_for"';    #access_log	   /usr/local/nginx/logs/access.log  main;# 日志文件名    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;	    #开启gzip压缩    gzip  on;    gzip_min_length 1k;    gzip_buffers 4 8k;    gzip_http_version 1.1;    gzip_types text/plain application/x-javascript text/css application/xml;    server {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;		    location / {            root   html;            index  index.html index.htm;        }        location ~^/(images|image|javascript|js|css|static|json|staticImage)/ {            root   /usr/mmtStatic;            access_log  off;	    expires     3000d;        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }           }}

通过在server监控的 80 端口下新增一个 location 来指定静态文件路径.location允许根据url进行不同的配置.

  • “~” 匹配时区分大小写
  • “~*” 匹配时不区分大小写
  • “=” 精确匹配字符和字符串“^~”  
  • “^~” 例如: ^~ /images/ 匹配到任何以images开头的,便停止搜索。

   location ~^/(imgaes|image|js)/ {

    请求文件的根目录  /usr/mmtStatic;

    过期时间 expire 3000天;   

 }

这里的 location 匹配以images等开头的路径

    如果路径不存在,会提示404

    如果路径存在,则返回相关文件

 

example:

1.在浏览器中输入 http://172.16.20.83/json/login.json

实际在linux中,根据上面的配置文件,会到 /usr/mmtStatic/json 下去寻找login.json

在linux中目录如下:mmtStatic下面有json目录,在json目录中有login.json文件

因此浏览器会显示login.json中的相关内容,显示如下:

2.若修改为login1.json,则返回404页面

广告 广告

评论区