我的nginx设置以我想要的方式运行时遇到了一些麻烦。我有一个站点example.localhost位于/ vagrant / frontend / www。我的配置正常,看起来像这样:
server {
listen 80;
server_name example.localhost;
root /vagrant/frontend/www;
index index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ .*\.php$ {
include /etc/nginx/fastcgi.conf;
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
}
}
但后来我想将位于/ vagrant / backend / www的管理站点添加到地址example.localhost / admin。这是出现问题的时候,根据this post我在下面的设置中找不到404:
location /admin {
root /vagrant/backend/www;
}
location ~ /admin/.+\.php$ {
include /etc/nginx/fastcgi.conf;
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
}
错误日志如下所示:
2014/04/23 12:30:10 [error] 15459#0: *1 "/vagrant/backend/www/admin/index.php" is not found (2: No such file or directory), client: 192.168.56.1, server: example.localhost, request: "GET /admin/ HTTP/1.1", host: "example.localhost"
我知道为什么我会得到404,但我怎么做到这一点?非常感谢任何帮助!
更新
我已将管理员位置更改为:
location /admin {
alias /vagrant/backend/www/;
try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
include /etc/nginx/fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
}
我也删除了这个块:
location ~ /admin/.+\.php$ {...}
现在对example.localhost / admin的调用处理正确,但是如果我将其更改为例如example.localhost / admin / site / index,请求将由前端处理。看起来/ admin位置不匹配......有什么想法吗?
答案 0 :(得分:0)
location
块完全独立。您的请求最后会在location
块中结束,并且永远不会从第一个请求中看到root
指令。将root
指令放到第二个块。
location ~ /admin/.+\.php$ {
root /vagrant/backend/www;
include /etc/nginx/fastcgi.conf;
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
}
答案 1 :(得分:0)
server {
listen 80;
server_name example.localhost;
root /vagrant/frontend/www;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
include fastcgi.conf;
}
}
location /admin {
return 301 /admin/;
}
location /admin/ {
alias /vagrant/backend/www/;
try_files $uri $uri/ @handler;
location ~ \.php$ {
include fastcgi.conf;
}
}
location @handler {
rewrite / /admin/index.php?$args;
}
}