我有以下 nginx.conf
問題
檔案夾 /legacy/app 有一些我不想訪問的檔案,
我認為以下位置會將所有請求重定向到 php:
location / { try_files $uri $uri/ /index.php?$query_string;}
但我可以打開像 site.com/php-fpm.conf 這樣的檔案,例如我想避免的。
問題
在沒有自定義位置的情況下防止打開此類靜態檔案的最佳解決方案是什么
location ~ \.(md|conf)$ {deny all;}
nginx.conf
worker_processes 1;
daemon off;
worker_rlimit_nofile 8192;
pid /tmp/nginx.pid;
user nginx;
error_log stderr;
events {
worker_connections 4096;
}
http {
client_body_temp_path /tmp/client_body_temp_path;
proxy_temp_path /tmp/nginx-proxy-temp-path;
fastcgi_temp_path /tmp/fastcgi_temp_path;
include .nginx/mime.types;
include .nginx/proxy.conf;
include .nginx/fastcgi.conf;
index index.php;
log_format client_logs
'$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
default_type application/octet-stream;
tcp_nodelay on;
sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 128;
keepalive_timeout 120;
port_in_redirect off; # Ensure that redirects don't include the internal container PORT - 8080
gzip on;
server {
server_name localhost;
listen 8080;
access_log /dev/stdout client_logs;
error_log /dev/stderr;
root /legacy/app;
index index.php;
error_page 500 502 503 504 /50x.html;
# do not list us in search engines
location = /robots.txt {
add_header Content-Type text/plain;
return 200 "User-agent: *\nDisallow: /\n";
access_log off;
log_not_found off;
}
location ~ ^/(images|javascript|js|css|fonts|static|assets)/ {
root /legacy/app/;
expires 30d;
add_header Cache-Control public;
access_log off;
}
location ~ \.php$ {
root /legacy/app;
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 600s;
fastcgi_split_path_info ^(. \.php)(/. )$;
fastcgi_index index.php;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
}
uj5u.com熱心網友回復:
什么try_files是按照您指定的順序查找檔案,最后一個引數是后備 url。
所以基本上try_files $uri $uri/ /index.php?$query_string;尋找檔案$url并在它存在時提供它。然后它查找目錄$url/并在它存在時提供它。如果檔案和目錄都不存在,它將回退到 php 檔案。
因此,如果采用這種方法,您可以嘗試以下方法:
location / {
try_files _ /index.php?$query_string;
}
這將查找一個名稱_不應該存在于您的檔案根目錄中的檔案,并將向 index.php 發出內部重定向。
您還可以像這樣設定狀態碼:
location / {
try_files _ =403;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/525911.html
