AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 566779
Accepted
Sergiks
Sergiks
Asked: 2014-01-13 13:02:40 +0800 CST2014-01-13 13:02:40 +0800 CST 2014-01-13 13:02:40 +0800 CST

如何设置 nginx 和 fastcgi 以在子文件夹中运行 Laravel?

  • 772

我正在单个域的子文件夹下设置多个 Laravel 应用程序(社交网络应用程序,需要 https,单个证书)。

无法弄清楚如何编写 nginx 配置以将 HTTP 请求映射/app1/到 Laravel 安装/var/www/other_folder/public/

这是配置测试的最新迭代。调试日志被省略。Laravel 根位置 /app1/ 有效,但路由映射 ( /app1/api/method/) 无效。请帮助,如何调试 nginx 如何逐步处理请求(调试日志不是那么解释),或者给我一个提示如何将子文件夹映射/app1/...到index.phpLaravel。

server {
    listen      80;
    server_name  apps.mydomain.com;
    root    /var/www/apps.mydomain.com/docs;

    location /  {
        index   index.html index.php;
    }

    location /app1    {
        alias   /var/www/other_folder/public;
        index   index.php   index.html;
        try_files   $uri  $uri/ /app1/index.php$is_args$args /app1/index.php?$query_string;
    }

    location ~ /app1/.+\.php$ {
        rewrite ^/app1/(.*)$  /$1  break;
        include /etc/nginx/fastcgi.conf;
        fastcgi_param    SCRIPT_FILENAME    /var/www/other_folder/public$fastcgi_script_name;

        fastcgi_index  index.php;
        fastcgi_pass php;

        #   Database access parameters
        fastcgi_param   DB_HOST "localhost";
        fastcgi_param   DB_USER "apps";
        fastcgi_param   DB_PASS "xxxxxxxx";
        fastcgi_param   DB_NAME "app1";
    }

    # Other locations skipped
    include /etc/nginx/global/php.conf; # other php scripts
}
nginx
  • 2 2 个回答
  • 11723 Views

2 个回答

  • Voted
  1. Best Answer
    Fabio Scaccabarozzi
    2014-01-18T02:07:38+08:002014-01-18T02:07:38+08:00

    我认为您应该更改该位置的根目录。根据 Nginx 文档,根指令还具有上下文“位置”:

    语法:根路径;

    上下文:http,服务器,位置,如果在位置

    因此,您应该能够执行以下操作:

    server {
        listen      80;
        server_name  apps.mydomain.com;
        root    /var/www/apps.mydomain.com/docs;
    
        location /  {
            index   index.html index.php;
        }
    
        location /app1    {
            root   /var/www/other_folder/public;
            rewrite ^/app1/(.*)$  /$1  break;
            index   index.php   index.html;
            try_files   $uri  $uri/ /index.php$is_args$args /index.php?$query_string;
        }
    
        location ~ /app1/.+\.php$ {
            root   /var/www/other_folder/public;
            rewrite ^/app1/(.*)$  /$1  break;
            include /etc/nginx/fastcgi.conf;
            fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    
            fastcgi_index  index.php;
            fastcgi_pass php;
    
            #   Database access parameters
            fastcgi_param   DB_HOST "localhost";
            fastcgi_param   DB_USER "apps";
            fastcgi_param   DB_PASS "xxxxxxxx";
            fastcgi_param   DB_NAME "app1";
        }
    
        # Other locations skipped
        include /etc/nginx/global/php.conf; # other php scripts
    }
    

    据我了解, alias 指令将一个 URL 重新映射到另一个 URL 并继续处理,它没有定义一个目录从哪里到源文件。我不确定您是否仍需要在 PHP 位置进行重写。

    • 4
  2. Rowanto
    2014-01-17T16:11:05+08:002014-01-17T16:11:05+08:00

    此代码未经测试。但是您说它适用于 /app1/ 但不适用于 /app1/api/method/ 。我猜你的问题出在这部分:

    location /app1    {
        alias   /var/www/other_folder/public;
        index   index.php   index.html;
        try_files   $uri  $uri/ /app1/index.php$is_args$args /app1/index.php?$query_string;
    }
    

    所以这基本上是告诉去尝试url,如果不匹配,尝试将其作为目录匹配url/,如果不存在文件和目录,则我们尝试将其匹配为/app1/index.php?someargs,如果不存在,则匹配它作为/app1/index.php?$query_string

    首先,alias 和 try_files不能一起工作,所以你可以使用 root 来代替。将 /app1 位置块更改为以下内容,以查看它是否解决了您的问题。

    location /app1    {
        root   /var/www/other_folder/public;
        index   index.php   index.html;
        try_files $uri $uri/ /index.php$is_args$args;
    }
    

    其次, $query_string 与 $args 相同,唯一的区别是它是只读的。

    就像我说的,这是未经测试的,但如果它有效,那就太好了。如果没有,我还有另一个想法。

    • 1

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve