文章目录
- 配置访问本地文件某个目录
- 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;# }#}}