我有下面的nginx.conf
问题
文件夹/legacy/app中有一些我不想访问的文件,
我认为以下位置会将所有请求重定向到php:
location / { try_files $uri $uri/ /index.php?$query_string;}
但我可以打开像www.example.com这样的文件site.com/php-fpm.conf,例如我想避免的文件。
个问题
要防止在没有自定义位置(如
location ~ \.(md|conf)$ {deny all;}
nginx.配置文件
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;
}
}
}
1条答案
按热度按时间igetnqfo1#
try_files
所做的是按照指定的顺序查找文件,最后一个参数是回退url。所以基本上
try_files $uri $uri/ /index.php?$query_string;
会寻找文件$url
,如果存在就提供它,然后寻找目录$url/
,如果存在就提供它,如果文件和目录都不存在,就回退到php文件。因此,如果采用这种方法,您可以尝试以下方法:
这将查找一个名为
_
的文件,该文件不应存在于您的文档根目录中,并将发出一个内部重定向到index.php。您也可以这样设置状态代码: