【问题标题】:ISPConfig Vhost allowing clean URLs in LaravelISPConfig Vhost 允许在 Laravel 中使用干净的 URL
【发布时间】:2015-10-27 03:48:45
【问题描述】:

我有一个运行良好的现有服务器,它使用 nginx 和 ISPconfig 托管了许多站点。但是我创建了一个新站点并希望使用 Laravel。

我已经通过 composer 成功安装了 Laravel,并且在我访问 mywebsite.com/public 时看到了熟悉的欢迎刀片

我接下来要做的是制作一些干净的网址。我在使用 vhost 文件方面的经验有些有限,而且我在配置方面遇到了一些问题。

我的路由文件是这样的

Route::get('/', function () {
    return view('welcome');
});
Route::get('/test', function () {
    return view('test');
});

我希望看到 mywebsite.com/test 显示 test.blade.php 的内容

我知道我需要对 vhost 文件做一些工作,然后才能期望它起作用,但我对 vhost 的经验有限,我有点不知所措。

我当前的文件是这样的

server {
    listen *:80;
    server_name mywebsite.com ;

    root   /var/www/mywebsite.com/web;

    index index.html index.htm index.php index.cgi index.pl index.xhtml;

    error_page 400 /error/400.html;
    error_page 401 /error/401.html;
    error_page 403 /error/403.html;
    error_page 404 /error/404.html;
    error_page 405 /error/405.html;
    error_page 500 /error/500.html;
    error_page 502 /error/502.html;
    error_page 503 /error/503.html;
    recursive_error_pages on;
    location = /error/400.html {

        internal;
    }
    location = /error/401.html {

        internal;
    }
    location = /error/403.html {

        internal;
    }
    location = /error/404.html {

        internal;
    }
    location = /error/405.html {

        internal;
    }
    location = /error/500.html {

        internal;
    }
    location = /error/502.html {

        internal;
    }
    location = /error/503.html {

        internal;
    }

    error_log /var/log/ispconfig/httpd/mywebsite.com/error.log;
    access_log /var/log/ispconfig/httpd/mywebsite.com/access.log combined;

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location /stats/ {

        index index.html index.php;
        auth_basic "Members Only";
        auth_basic_user_file /var/www/clients/client1/web5/web/stats/.htpasswd_stats;
    }

    location ^~ /awstats-icon {
        alias /usr/share/awstats/icon;
    }

    location ~ \.php$ {
        try_files /5e26a1d85cb98f7191261e023385e60d.htm @php;
    }

    location @php {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/lib/php5-fpm/web5.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
    }
}

现在在另一台服务器上,我可以使用这个简单的指令来工作

server {
    root /var/www/public;
    index index.php index.html index.htm;
    server_name localhost;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
}

但我只能在当前服务器上使用虚拟主机做些什么,因为 ISPconfig 为我编写了大部分内容,并且拒绝编写在其他地方工作的上述配置。另外我觉得直接编辑文件是不好的做法,我总是担心 ISPconfig 会为我重写文件,所以我不确定如何最好地进行。

我的选择是继续编辑虚拟主机并希望获得最好的结果,但如果我这样做,我将如何确保 ISPconfig 在不使用“hacky”方法的情况下无法覆盖文件?

或者,是否有一个我可以通过 ISPconfig 输入的配置,允许以适合 Laravel 的方式正确进行重写?在这种情况下,输入的任何指令都需要优先于 ~ .php$ 子句,因为该子句由 ISPconfig 在通过控制面板输入的任何指令之前编写。

【问题讨论】:

  • 我已经创建了 ispconfig 标签(现在这是标签下的第一个问题!),并且还从这个问题中删除了 nginx 标签,因为作为 nginx 专家,我看不出我能在这里提供什么帮助。

标签: vhosts laravel-5.1 ispconfig


【解决方案1】:

我最近也遇到了同样的问题。挖掘 ISPConfig 的来源,我知道它可以插入/合并/删除该默认 vhosts 文件的位置块。所以我做了以下事情:

站点菜单 > 选择网站 > 选项

然后我在“nginx Directives”字段中输入了以下内容:

# redirect stuff to the public inner folder
location / {
    root {DOCROOT}/public;
    try_files /public/$uri /public/$uri/ /public/index.php?$query_string;
} 

# merged the stuff people suggests for laravel inside the php block
# mind the 'merge' keyword that did the trick
location ~ \.php$ { ##merge##
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors off;
    fastcgi_buffer_size 16k;
    fastcgi_buffers 4 16k;
}

【讨论】:

  • 有趣,我不知道##merge##keyword 的用法...还注意到,与其明确放置 fastcgi_pass 指令,不如使用 {FASTCGIPASS} 代替(那样使用 TCP 套接字而不是 UNIX 套接字的配置将自动工作)。这对我有用,但有一些小故障——有时 Laravel 会在 URL 上添加 .../index.php/... ,而 nginx 会完全混淆。而且我还得到了一些断开的链接,这些链接显然来自未正确解析的内容。但是......它有点工作,比我拥有的要好得多!
【解决方案2】:

达尼洛的回答有一点问题。 php 运行了,但像 js/css/images 这样的资产停止加载。将以下内容添加到 ISPConfig 中的 nginx 指令对我有用:

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

【讨论】:

    【解决方案3】:

    我不是这方面的专家,但根据我过去的经验,如果我有两个域,我会在两个不同的文件中为每个域定义服务器块,并将它们放在 /etc/nginx/sites-available/site1.com 和 /etc/ nginx/sites-available/site2.com

    但您似乎已经有一个网站,您可以使用位于 /var/www/mywebsite.com/web 的 mywesite.com 访问该网站; (查看配置文件的根值)

    现在您将 Laravel 安装在 /var/www/mywebsite.com/test 位置的测试文件夹中。

    要访问它,您可以尝试在您的 ispconfig 文件末尾添加以下内容。

    注意我是如何使用从服务器块根目录到 laravel 公用文件夹的相对路径的。

    location /../test/public {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    

    如需更详细的教程,请尝试Nginx Server Block Setup。

    希望这会有所帮助,

    K

    【讨论】:

    • 感谢您的努力,这将适用于典型的 NGINX 设置。我遇到的问题是 ISPConfig 为我重写了很多 vhost 文件,我想避免不得不从 ISPConfig 中夺走控制权
    • 哦...抱歉我的问题搞错了,很遗憾我从来没有机会使用 ispconfig 控制面板。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2016-04-24
    相关资源
    最近更新 更多