Sla*_* II 3 nginx web-server php5 php-fpm
我的 nginx 服务器有以下配置:
server {
listen 80 default_server;
server_name example.com www.example.com;
root /var/www/example.com/web;
index index.php index.html;
location / {
# try to serve file directly, fallback to rewrite
try_files $uri $uri/ @rewriteapp;
}
location @rewriteapp {
# rewrite all to index.php
rewrite ^(.*)$ /index.php last;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
}
}
Run Code Online (Sandbox Code Playgroud)
以及以下目录结构:
/var/www/example.com/:
当客户端点击:' example.com/dir/ ' 然后index.html被提供。
当客户端访问一些不存在的 URL 时,像这样:' example.com/non-existent/ ' 然后index.php在根目录中提供。
但是当客户端点击:' example.com/empty/ ' 时,会发送403状态代码。
Flo*_*aie 11
发生这种情况是因为,在大多数 Web 服务器中,文件夹的默认操作是“目录列表”,默认情况下该操作是禁用的。如果您禁用目录索引,通常会在 Apache 中发生同样的情况。您可以在 nginx 中做的是将 =404 放在 try_files 指令的末尾。
您可以通过将 /index.php 放在 try_files 指令的末尾来完成此操作。但是,出于安全原因,这并不总是值得推荐的。
另外,在您的配置中对 nginx 有一个小小的误解:您应该将 $uri/ 替换为 $uri/index.php 或 $uri/index.html 或其他任何内容。它在 try_files $uri/ 处停止,因为它确实找到了该位置,但禁止用户访问它。