我对两个不同的域有以下 nginx 配置:
server {
listen 80;
root /srv/users/serverpilot/apps/myapp/public;
index index.php index.html index.htm;
server_name domain.com www.domain.com;
location / {
try_files $uri $uri/ @route;
}
location @route {
rewrite ^/(.+)$ /index.php?_route_=$1 last;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/srv/users/serverpilot/run/myapp.php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
我在 DigitalOcean Ubuntu 14.04 x64 droplet 上使用 ServerPilot 作为控制面板。
ServerPilot 应用程序的默认文档根目录是:
/srv/users/serverpilot/apps/APPNAME/public
在一个域中,我使用默认的文档根目录,这个 nginx 配置工作正常,但在另一个应用程序上,我的实际文档根目录是:
/srv/users/serverpilot/apps/myapp/public/public
在此,我已根据需要更改了根指令,但得到的只是错误No input file specified.
。
我已经检查了权限、检查了我的池权限,并检查了该文件是否存在。
任何想法或帮助都将不胜感激。
更新
将配置更改为:
server {
listen 80;
root /srv/users/serverpilot/apps/myapp/public;
index index.php index.html index.htm;
server_name my.domain.com;
location / {
rewrite ^/$ /public/ break;
rewrite ^(.*)$ /public/$1 break;
}
location ~ .*\.(ico|gif|jpg|jpeg|png) { }
location /public {
rewrite ^/(.*)/$ /$1 redirect;
if (!-e $request_filename){
rewrite ^/([^?]*) /index.php?_route_=$1 break;
}
}
location ~ \.php$ {
try_files $uri $uri/ /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/srv/users/serverpilot/run/myapp.php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
现在我可以按预期获取 index.php,但没有 css、js 或图像,单击链接会导致 404 错误。
所以我已经接近了,但仍然缺少一些东西。
更新
从错误日志中我可以看到路由失败的原因。此处请求的路由带有一个额外的尾部斜杠:
2015/04/29 11:16:06 [error] 9796#0: *7 open()
"/srv/users/serverpilot/apps/myapp/public/public//common/css&css=50d5d2cdd10873f535b8f87c3c65e908"
failed (2: No such file or directory), client: 127.0.0.1,
server: my.domain.com, request: "GET /common/css&css=50d5d2cdd10873f535b8f87c3c65e908 HTTP/1.1",
host: "my.domain.com",
referrer: "http://my.domain.com/"
对于图像,它们不会被路由到 public/ 目录:
2015/04/29 11:16:06 [error] 9796#0:
*11 open() "/srv/users/serverpilot/apps/myapp/public/image/cache/data/blog/post/landscape-b-40x30h.jpg"
failed (2: No such file or directory), client: 127.0.0.1,
server: my.domain.com, request: "GET /image/cache/data/blog/post/landscape-b-40x30h.jpg HTTP/1.1",
host: "my.domain.com", referrer: "http://my.domain.com/"
更新:
经过大量研究并查看了不同应用程序的其他服务器块后,我想出了这个,但仍然缺少一些东西。
upstream backend {
server unix:/srv/users/serverpilot/run/myapp.php-fpm.sock;
}
server {
listen 80;
root /srv/users/serverpilot/apps/myapp/public;
index index.php index.html index.htm;
server_name my.domain.com;
access_log /srv/users/serverpilot/log/myapp/myapp_nginx.access.log;
error_log /srv/users/serverpilot/log/myapp/myapp_nginx.error.log;
location /public {
try_files $uri $uri/ /public/index.php?_route_=$args;
}
location / {
rewrite ^(.*)$ /public/$1;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_pass backend;
}
}
剩下的唯一问题是每个 php 页面都返回主页。
链接的工作方式是,单击它们会改变 dom 历史记录,但每次请求都会加载相同的页面。
答案1
upstream myapp {
server unix:/srv/users/serverpilot/run/myapp.php-fpm.sock;
}
server {
listen 80;
server_name my.domain.com;
root /srv/users/serverpilot/apps/myapp/public;
index index.php index.html index.htm;
access_log /srv/users/serverpilot/log/myapp/myapp_nginx.access.log;
error_log /srv/users/serverpilot/log/myapp/myapp_nginx.error.log;
location /asset {
rewrite ^/asset/(.*)$ /public/asset/$1 break;
}
location /image {
rewrite ^/image/(.*)$ /public/image/$1 break;
}
location / {
try_files $uri $uri/ @front;
}
location @front {
rewrite ^/(.+)$ /index.php?_route_=$1 last;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_pass myapp;
}
}
没什么可感谢的。