当前位置: 首页 > news >正文

nginx配置案例,文件服务器配置,浏览某个目录下所有文件,中文乱码,try_files解释,root和alias区别

文章目录

      • 配置访问本地文件某个目录
      • root 和 alias的区别
      • autoindex 配置
      • try_files的作用
      • nginx中文乱码
      • 完整配置

配置访问本地文件某个目录

例如浏览器输入localhost/data/ 访问D:\data文件夹,只允许本机访问
location /data/ {
root D:/;
autoindex on;
autoindex_exact_size off;
allow 127.0.0.1;
}

location /data/ {
alias D:/;
autoindex on;
autoindex_exact_size off;
allow 127.0.0.1;
}
这样配置的话,浏览器输入localhost/data/ 访问的就是整个D盘,autoindex 默认是off,设置为on的话可以浏览目录,不设置也不影响文件的访问,autoindex_exact_size off 文件的大小显示的就是Kb、Mb
在这里插入图片描述

location /nginx/ {
alias D:/softwares/nginx-1.22.1/;
autoindex on;
autoindex_exact_size off;
allow 127.0.0.1;
}
这样配置的话,浏览器输入http://127.0.0.1/nginx/, 就会显示D:/softwares/nginx-1.22.1/下的所有目录和文件
在这里插入图片描述

location /conf/ {root D:/softwares/datax/;autoindex on;autoindex_exact_size off;allow 127.0.0.1;
}

这样配置的话,浏览器输入http://127.0.0.1/conf/, 就会显示D:/softwares/datax/conf下的所有目录和文件
在这里插入图片描述

location /log/ {root D:/softwares/datax/;autoindex off;autoindex_exact_size off;allow 127.0.0.1;
}

在这里插入图片描述
少一个/ 就找不到, 还是比较严格的

这样配置的话,浏览器输入http://127.0.0.1/log/, 就会显示 403 Forbidden;
在这里插入图片描述
浏览器输入 http://127.0.0.1/log/2021-12-08/s_datax_job_job_json-16_39_11.444.log 就会下载 s_datax_job_job_json-16_39_11.444.log 这个文件
在这里插入图片描述

root 和 alias的区别

root,访问的是root+location,alias访问时不会加上location

autoindex 配置

autoindex 默认是off不开启, on的话,浏览区输入的是一个目录,则显示目录下的所有文件和目录

try_files的作用

Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the fileparameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made

关键点1:按指定的file顺序查找存在的文件,并使用第一个找到的文件进行请求处理

关键点2:查找路径是按照给定的root或alias为根路径来查找的

关键点3:如果给出的file都没有匹配到,则重新请求最后一个参数给定的uri,就是新的location匹配

关键点4:如果是格式2,如果最后一个参数是 = 404 ,若给出的file都没有匹配到,则最后返回404的响应码

location /images/ {root /opt/html/;try_files $uri   $uri/  /images/default.gif;
}

比如 请求 127.0.0.1/images/test.gif 会依次查找 1.文件/opt/html/images/test.gif 2.文件夹 /opt/html/images/test.gif/下的index文件 3. 请求127.0.0.1/images/default.gif

4.其他注意事项
1.try-files 如果不写上 $uri/,当直接访问一个目录路径时,并不会去匹配目录下的索引页 即 访问127.0.0.1/images/ 不会去访问 127.0.0.1/images/index.html

nginx中文乱码

解决措施 服务器配置charset为utf-8

完整配置

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  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}#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;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}location /data/ {alias D:/;autoindex on;autoindex_exact_size off;allow 127.0.0.1;}location /nginx/ {alias D:/softwares/nginx-1.22.1/;autoindex on;autoindex_exact_size off;allow 127.0.0.1;}location /conf/ {root D:/softwares/datax/;autoindex on;autoindex_exact_size off;allow 127.0.0.1;}location /log/ {root D:/softwares/datax/;autoindex off;autoindex_exact_size off;allow 127.0.0.1;}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 数据结构-3.1.栈的基本概念
  • 一文说清楚ETL与Kafka如何实现集成
  • SalescustomerController
  • 分享一款idea插件
  • day-56 字符串转换整数 (atoi)
  • Matplotlib在运维开发中的应用
  • Vue组件:模板引用ref属性的使用
  • 【python】【绘制小程序】动态爱心绘制
  • 如何利用 Visual Studio 和 AI 工具实现高效编程
  • SQLPlus执行成功但数据没有更新的原因及解决办法
  • CTFHUB 技能树 信息泄露 HG泄露 解密过程记录
  • 【线性回归模型】
  • 分班 - 华为OD统一考试(E卷)
  • 【machine learning-七-线性回归之成本函数】
  • 力扣232:用栈实现队列
  • [ JavaScript ] 数据结构与算法 —— 链表
  • 2019年如何成为全栈工程师?
  • 345-反转字符串中的元音字母
  • Computed property XXX was assigned to but it has no setter
  • FastReport在线报表设计器工作原理
  • JavaScript类型识别
  • JavaScript设计模式系列一:工厂模式
  • Laravel 菜鸟晋级之路
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • webpack4 一点通
  • 从0实现一个tiny react(三)生命周期
  • 猴子数据域名防封接口降低小说被封的风险
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 巧用 TypeScript (一)
  • 如何在 Tornado 中实现 Middleware
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 探索 JS 中的模块化
  • 通信类
  • 详解NodeJs流之一
  • 学习使用ExpressJS 4.0中的新Router
  • hi-nginx-1.3.4编译安装
  • 阿里云ACE认证学习知识点梳理
  • 带你开发类似Pokemon Go的AR游戏
  • 如何用纯 CSS 创作一个货车 loader
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • #Datawhale X 李宏毅苹果书 AI夏令营#3.13.2局部极小值与鞍点批量和动量
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • #大学#套接字
  • $$$$GB2312-80区位编码表$$$$
  • (1)(1.13) SiK无线电高级配置(五)
  • (1)(1.9) MSP (version 4.2)
  • (16)Reactor的测试——响应式Spring的道法术器
  • (175)FPGA门控时钟技术
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (Charles)如何抓取手机http的报文
  • (二)hibernate配置管理
  • (附源码)ssm教材管理系统 毕业设计 011229
  • (附源码)ssm码农论坛 毕业设计 231126
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置