php - Nginx 在不同位置托管应用程序

标签 php nginx

<分区>

我正在尝试服务 CachetHQ在 nginx + php-fpm 中的特定位置。文档将此作为在 status.example.com 中提供的示例(有效):

server {
    listen 80;
    server_name status.example.com;

    root /var/www/Cachet/public;
    index index.php;

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

    location ~ \.php$ {
         include fastcgi_params;
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_index index.php;
         fastcgi_keep_conn on;
    }
}

但是,我不想在 status.example.com 中提供服务,而是希望在 example.com/status 中提供服务。

我原以为这会起作用,但从 error.log 中我看到它正在尝试 /etc/nginx/htmlindex.php,但它应该是 /mnt/data/site/www-cachet/public/index.php:

location /status/ {
    index index.php;
    root /mnt/data/site/www-cachet/public;

    try_files $uri index.php$is_args$args;

    location ~ ^/status/.+\.php$ {
        root /mnt/data/site/www-cachet/public;
        include fastcgi_params;
        fastcgi_pass   unix:/tmp/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        fastcgi_keep_conn on;
    }
}

最佳答案

让我从您声称有效的原始配置开始,并尝试根据您的要求修改它,结果如下:

location ^~ /status {
    alias /var/www/Cachet/public;
    index index.php;

    location = /status {
        return 302 /status/;
    }
    location / {
        try_files $uri /status/index.php$is_args$args;
    }

    location ~ \.php$ {
         include fastcgi_params;
         fastcgi_pass 127.0.0.1:9000;
         rewrite ^/status/(.*) /$1;
         rewrite ^(.*)/ $1/index.php; # who knows what fastcgi_index is for?
         fastcgi_param SCRIPT_FILENAME $document_root$uri;
         fastcgi_keep_conn on;
    }
}

基本上,您想使用 alias 而不是 root在这里,并且可能在最后的 try_files 中也有一个绝对路径以及。我认为不需要在嵌套位置中添加额外的前缀,但您可能希望确保根位置与 ^~ 的最终匹配。修饰符。

我猜,主要技巧是即使使用 alias指令性的东西不像适当的 root 那样花花公子。 ,因此,您必须确保 SCRIPT_FILENAME设置正确。这部分好像没有写的很清楚,我也懒得去测试是否 $fastcgi_script_name ngx variable fastcgi_index 指令与 alias 配合得很好-- 而不是试图确定它们是如何工作的(或不工作),我们只是做了几个 rewrite规则,并构建SCRIPT_FILENAME而是基于我们重写规则的结果。 :-)

然而,话虽如此,我认为第二个重写规则(以及它替换的 fastcgi_index)也可能是空操作,因为我们应该如何以 \.php$ 结束? location如果$uri没有以 .php 结束已经? (同样,您也可以自由尝试删除第一个重写规则,并将 $uri 中的 SCRIPT_FILENAME 替换为 $fastcgi_script_name ,看看事情是否仍然有效,但互联网 from 2009 可能表明他们没有't.)

关于php - Nginx 在不同位置托管应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33046159/

相关文章:

php - 如何在 Laravel 中获得最畅销的产品?

php - 如何在 Laravel 中将变量传递给另一个 Controller

java - Spring Boot docker无法连接到mysql(连接被拒绝/createCommunicationsException)

node.js - GAE 中 Node JS 项目中的 HTTP 413 请求实体太大

php - 将 php 5.2 的阿拉伯日期和时间转换为英语

php - 如何列出 MSSQL 中的所有表?

PHP 在 MYSQL 数据库中插入两个重复的行

nginx - 有人在代理我的服务器

python - 在 nginx 上使用 Fabric 的 Python Flask Web 应用程序中出现上游超时错误

linux - 是否有任何版本的 ModSecurity 可以与受支持的 Nginx 版本配合使用?