【发布时间】:2014-07-15 08:44:07
【问题描述】:
刚接触 nginx 并遇到一些重写问题。
我有一个链接到子域 test.mydomain.com 的网站。 我当前的 nginx 配置:
server {
listen 80 default_server;
root /test/public;
index index.html index.htm index.php;
server_name localhost;
access_log /var/log/nginx/localhost.laravel-access.log;
error_log /var/log/nginx/locahost.laravel-error.log error;
charset utf-8;
location /admin/ {
alias /test/public/admin/public/;
try_files $uri $uri/ /admin/index.php?$query_string;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }
error_page 404 /index.php;
include hhvm.conf; # The HHVM Magic Here
# Deny .htaccess file access
location ~ /\.ht {
deny all;
}
}
以下是(默认)hhvm.conf 的内容
location ~ \.(hh|php)$ {
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
我访问 test.mydomain.com,一切正常。
然而!我在 test.mydomain.com/admin 上也有一个管理面板。所有请求
test.mydomain.com/admin/* 应该改写为/test/public/admin/public/*,
而是使用我当前的配置,它们都被重写为 public/index。
我的文件夹结构有点像这样:
/test
/admin
project.stuff
/public
index.php
etc.php
/public
index.php
etc.php
主要编辑
我在 hhvm.conf 中添加了以下条件:
if ($request_uri ~* /admin) {
root /test/admin/public;
}
/admin 请求现在被相应地重写。 nginx 文档指出,如果两个位置模式匹配,则只会使用最简单的一个(排序)。现在剩下的就是让它不仅适用于 .hh 和 .php 文件,还适用于资产文件(js、css、jpg 等)。
我认为类似的东西就足够了,但遗憾的是它没有:
location ~ \.(js|css)$ {
if ($request_uri ~* /admin) {
root /test/admin/public;
}
}
【问题讨论】:
-
你试过
alias吗?location /admin/ { alias /test/admin/; } -
@Deadooshka,nginx 文档推荐
location /admin/ { root /test; }- nginx.org/en/docs/http/ngx_http_core_module.html#alias 但可能这些是等价的。
标签: nginx laravel rewrite fastcgi hhvm