1

我正在 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 访问它。

谢谢。

4

1 回答 1

0

如果每个应用程序的基本 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;
    }
}

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

于 2016-01-01T23:14:23.607 回答