使用 Laravel 和 HHVM 重写 NGINX

Kev*_*amp 1 fastcgi nginx url-rewriting laravel hhvm

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是(默认)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;
}
Run Code Online (Sandbox Code Playgroud)

我访问 test.mydomain.com,一切正常。然而!我在 test.mydomain.com/admin 上还有一个管理面板。所有请求都 test.mydomain.com/admin/*应该重写为/test/public/admin/public/*,而不是根据我当前的配置,它们都被重写为 public/index.html 。

我的文件夹结构看起来有点像这样:

/test
    /admin
        project.stuff
        /public
            index.php
            etc.php
    /public
        index.php
        etc.php
Run Code Online (Sandbox Code Playgroud)

主要编辑

我在 hhvm.conf 中添加了以下条件:

if ($request_uri ~* /admin) {
    root /test/admin/public;
}
Run Code Online (Sandbox Code Playgroud)

/admin 请求现在会相应地重写。nginx 文档指出,如果两个位置模式匹配,则只会使用最简单的一个(排序)。现在剩下的就是让它不仅适用于 .hh 和 .php 文件,还适用于资产文件(js、css、jpg 等)。

我认为类似的东西就足够了,但遗憾的是还不够:

location ~ \.(js|css)$ {
    if ($request_uri ~* /admin) {
        root /test/admin/public;
    }
}
Run Code Online (Sandbox Code Playgroud)

den*_*ned 5

这部分适用于问题的初始版本

root指令可以在 inside 中使用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;
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,根据您的陈述,即在您进入 s 后root,location您的常规站点停止工作,可以得出结论,您没有提供所有需要的信息,并且您的配置中还有其他问题,这就是为什么我要求您附加到您的询问你完整的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

更新

也许您还需要在hhvm.conf文件和文件中替换fastcgi_params此行:

fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
Run Code Online (Sandbox Code Playgroud)

具有以下内容:

fastcgi_param  SCRIPT_FILENAME $request_filename;
Run Code Online (Sandbox Code Playgroud)