【问题标题】:Laravel and WordPress integration routing using nginx使用 nginx 的 Laravel 和 WordPress 集成路由
【发布时间】:2016-04-05 20:10:13
【问题描述】:

我正在使用 Laravel 5.2 开发网络应用程序。我有现有的 WordPress 网站。所以,我想将 Laravel 与 WordPress 集成。 WordPress 应用程序具有静态页面。我的根目录中有两个单独的 Laravel 和 WordPress 目录。

  1. laraApp
  2. wpApp

我想将 wpApp 设为默认应用。所以当用户点击登录按钮时,用户将被重定向到 laraApp。我想要 www.example.com 上的 wpApp 和 www.example.com/laraApp 上的 laraApp。我正在运行 nginx 网络服务器。那么我的 nginx 配置文件应该是什么?

当前的 nginx 配置文件是:

server {
    listen 80;
    root /var/www/root/wpApp;
    index index.php index.html index.htm;
    server_name www.example.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    # rewrite rules for laravel routes
        location /laraApp {
        rewrite ^/laraApp/(.*)$ /laraApp/public/index.php?$1 last;
    }
    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

我的 Laravel 应用可以使用 url www.example.com/laraApp/public/ 访问 我想使用 www.example.com/laraApp 访问它。

谢谢。

【问题讨论】:

    标签: wordpress laravel nginx laravel-routing laravel-5.2


    【解决方案1】:

    如果每个应用程序的基本 URI 不重叠,配置会更简单。但鉴于您的问题的限制,您必须为配置中每个应用程序的 PHP 部分使用两个不同的文档根。

    当您将其中一个应用程序放置在/ 中时,另一个应用程序通过使用嵌套的位置块保持独立。 注意使用 ^~ 修饰符来防止第一个 PHP 块处理 Laravel 请求。

    index index.php index.html index.htm;
    
    root /var/www/root/wpApp;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {
        try_files $uri /index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
    
    location ^~ /laraApp {
        rewrite ^/laraApp(.*)$ /laraApp/public$1 last;
    }
    
    location ^~ /laraApp/public {
        root /var/www/root;
    
        try_files $uri $uri/ /laraApp/public/index.php?$query_string;
    
        location ~ \.php$ {
            try_files $uri /laraApp/public/index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    }
    

    我目前不在我的测试系统中,因此上述内容尚未经过语法检查或测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 2015-05-14
      • 1970-01-01
      • 2019-06-01
      • 2017-06-11
      • 2015-04-23
      • 2017-12-30
      相关资源
      最近更新 更多