我正在尝试运行 Nginx Web 服务器的 PhalconPHP 和 AngularJS 组合,其文件结构如下:
/root
/api
index.php
index.html
PhalconPHP 位于 API 文件夹中,需要与 AngularJS 不同的重写规则。我尝试在 Nginx 中使用以下配置设置虚拟主机:“
server {
listen 80; ## Listen on port 80 ##
server_name example.com; ## Domain Name ##
root /home/vagrant/example.com/angular/build; ## Site root for the Angular code ##
index index.html index.php; ## Set the index for site to use ##
charset utf-8; ## Set the charset ##
location / { ## Handle default requests ##
try_files $uri $uri/ /index.html; ## Try files and fall back to index.html ##
}
location /api { ## URL string to use for api ##
## 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?_url=$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;
}
}
## Don't fail or log request to favicon or robots.txt ##
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
## Access log is off and error log location is set ##
access_log off;
error_log /var/log/nginx/example.com-error.log error;
## Disable sendfile ##
sendfile off;
## 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;
}
## Don't allow access to .ht files ##
location ~ /\.ht {
deny all;
}
}
我从这里
我设法让 AngularJS 运行起来,但不幸的是,当我导航到 /api 时什么都没有显示出来(只是一个空白页)。我尝试更改虚拟主机规则的某些部分,但没有结果(上面发布的配置实际上是略有更改的配置,我在其中更改了位置 /api 部分:
location /api { ## URL string to use for api ##
## 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?_url=$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;
}
}
我究竟做错了什么?
答案1
我现在明白了,对于那些同样为此苦苦挣扎的人来说,这是解决方案:
server {
listen 80;
server_name mydomain.com wwww.mydomain.com;
root /path/to/website/files/public_html;
index index.html;
location / {
try_files $uri /index.html;
}
location /api {
index index.php;
## 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/(.*)$ /api/index.php?_url=/$1 last;
## Normal php block for processing ##
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
}
欢迎提供反馈:D
答案2
你可以禁用 fastcgi_param HTTPS;
如果你不使用 https/ssl
如果您使用 fastcgi_param HTTPS 关闭;
浏览器重定向到 https://
这是最终配置
server_name mydomain.com wwww.mydomain.com; root /path/to/website/files/public_html; index index.html; location / { try_files $uri /index.html; } location /api { index index.php; ## 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/(.*)$ /api/index.php?_url=/$1 last; ## Normal php block for processing ## location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # fastcgi_param HTTPS off; } } }