按照phalcon官网的配置,配了一个:
server {
listen 80;
server_name localhost.portal;
root /var/www/portal/public;
index index.php index.html index.htm;
charset utf-8;
location / {
try_files $uri $uri/ index.php?_url=$uri&$args;
}
location /public/ {
root /var/www/portal;
}
location ^~ /api/ {
root /var/www/portal;
rewrite ^/api/(.*)$ /api/index.php?_url=/$1 last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
其他url都能正常访问,可是访问api目录下的文件就会报404,api访问的目录依然是全局root目录:/var/www/portal/public,location里单设的root:/var/www/portal不生效。原因是:api路径 rewrite之后,再次访问生效的是location ~ .php$ {}配置,而这个配置继承的是全局的root。
修改为:不设全局的root,放到每个location里自行设置
把 location ~ .php$ {} 放到每个location里嵌套,就可以正常访问了。
server {
listen 80;
server_name localhost.portal;
index index.php index.html index.htm;
charset utf-8;
rewrite_log on;
access_log /var/log/nginx/portal_access.log main;
error_log /var/log/nginx/portal_error.log notice;
location / {
root /var/www/portal/public;
try_files $uri $uri/ /index.php?_url=$uri&$args;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
location /public/ {
root /var/www/portal;
}
location ^~ /api/ {
root /var/www/portal;
rewrite ^/api/(.*)$ /api/index.php?_url=/$1 last;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
location ~ /\.ht {
deny all;
}
}