【问题标题】:NGINX rewrite with Laravel and HHVMNGINX 用 Laravel 和 HHVM 重写
【发布时间】: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;
    }
}

【问题讨论】:

标签: nginx laravel rewrite fastcgi hhvm


【解决方案1】:

这部分适用于初版问题

root 指令可以在location {} 中使用,因此在您的情况下,配置可能如下所示:

server {
    listen 80 default_server;
    server_name localhost;
    index index.html index.htm index.php;
    try_files $uri $uri/ =404;
    ...

    location / {
        root /test/public;
        ...
    }

    location /admin/ {
        root /test;
        ...
    }
}

http://nginx.org/en/docs/http/ngx_http_core_module.html#root

小更新:

我已将try_files $uri $uri/ index.php?$query_string; 替换为try_files $uri $uri/ =404,因为这对我来说似乎更合乎逻辑,考虑到index index.html index.htm index.php 就在这里。


这部分适用于第二大版题

根据您目前提供的信息,这是正确的配置版本:

server {
    listen 80 default_server;
    server_name localhost;

    root /test/public;
    index index.html index.htm index.php;
    ...

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        ...
    }

    location /admin/ {
        alias /test/public/admin/public/;
        try_files $uri $uri/ /admin/index.php?$query_string;
        ...
    }
}

但是,根据您将root 移至locations 后您的常规站点停止工作的说法,可以断定您没有提供所有需要的信息,并且您的配置中还有其他问题,这就是为什么我要求您将您的完整 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/admin/public/;
        try_files $uri $uri/ /admin/index.php?$query_string;
        include hhvm.conf;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        include hhvm.conf;
    }

    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;

    # Deny .htaccess file access
    location ~ /\.ht {
        deny all;
    }
}

更新

您可能还需要在hhvm.conf 文件和fastcgi_params 文件中替换这一行:

fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

以下内容:

fastcgi_param  SCRIPT_FILENAME $request_filename;

【讨论】:

  • 感谢您为我指明了 nginx 根功能的方向。我根据您的回答更新了我的问题。
  • 由于某种原因,StackOverflow 不允许我更新我的答案,所以我在这里简要回答: - 如果您的管理面板脚本的基本目录实际上是 /test/public/admin/public 而不是 /test/admin 作为您最初的声明,那么你应该使用location /admin/ { alias /test/public/admin/public/; } 请注意,alias 路径中的斜杠是必需的,并且路径应该是绝对的,而不是相对的。后一个要求也适用于root 路径!
  • 感谢您的编辑!我首先将根声明移动到位置 / {} 内,这样做导致网页根本不显示 (404),因此似乎存在某种差异。我整天都在修补根/别名,但找不到有效的组合,这令人沮丧!
  • 您能否将您当前的 nginx 配置的完整列表添加到问题中?
  • 那么您还需要在fastcgi_params 文件中替换您在hhvm.conf 中替换的行。我已经相应地更新了我的答案。您问题的“主要编辑”部分的修改是错误的,不需要。
猜你喜欢
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 2014-10-24
  • 2013-03-29
  • 1970-01-01
相关资源
最近更新 更多