我全新安装了 Ubuntu 14.04 服务器。我已经安装了 nginx、php 等....
server {
listen 80;
listen [::]:80;
server_name testone.local;
root /var/www/htmlone;
index index.html;
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /alias {
alias /var/www/htmlalias;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在/var/www/htmlone php 中使用一个简单的 php 脚本,它会按预期执行。如果我在/var/www/htmlalias其中使用相同的脚本,则不会按预期执行。如果我将 HTML 脚本放入/var/www/htmlalias其中,但会按预期显示,因此别名充当别名,但不执行 php 文件,但 php 在主根目录中工作。
我在许多 serverfault 问题中发现,这种通用设置应该可以工作,但事实并非如此。有没有人看到我可能做错了什么?我在错误日志中看不到任何消息。
我应该添加这是针对 nginx 版本的:nginx/1.8.0
您遇到的问题实际上是 3 年前提交的一个长期存在的错误,它导致alias并try_files不能真正协同工作。
在错误页面上有Luke Howell的解决方法,如下所示:
location /api { ## URL string to use for api ##
alias /home/api/site_files/; ## Site root for api code ##
## Check for file existing and if there, stop ##
if (-f $request_filename) {
break;
}
## Check for file existing and if there, stop ##
if (-d $request_filename) {
break;
}
## If we get here then there is no file or directory matching request_filename ##
rewrite (.*) /api/index.php?$query_string;
## Normal php block for processing ##
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,就 nginx 而言,IF 是邪恶的,应尽可能避免。
| 归档时间: |
|
| 查看次数: |
10743 次 |
| 最近记录: |