1. Laravel的nginx配置
try_files指令
语法:try_files file ... uri 或 try_files file ... = code
默认值:无
作用域:server location
其作用是按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有的文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。
需要注意的是,只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误。命名的location也可以使用在最后一个参数中。与rewrite指令不同,如果回退URI不是命名的location那么$args不会自动保留,如果你想保留$args,则必须明确声明。
location指令
语法:location [=|~|~*|^~|@] /uri/ { … }
默认值:无
作用域:server
location指令是用来为匹配的URI进行配置,URI即语法中的"/uri/",可以是字符串或正则表达式。但如果要使用正则表达式,则必须指定前缀。 [@] 即是命名location,一般只用于内部重定向请求。
server {
listen 80;
#listen 443 ssl;
#ssl on;
root /path;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name www.baidu.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9009; #替换环境中的转发端口
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}