【问题标题】:Nginx configuration for Laravel in subfolder子文件夹中 Laravel 的 Nginx 配置
【发布时间】:2019-06-18 16:14:06
【问题描述】:

我已将我的 Laravel 5 项目放在 /var/www/my_project/ 中,我想通过 http://my_domain.com/my_project/ 联系它。但是,我不知道如何配置 nginx 服务器块。

我想要的是这个:

  • http://my_domain.com/ 此时应该为空。稍后,此处将显示另一个项目。
  • http://my_domain.com/my_project/ 应该是我现在要添加的项目。

请注意,Laravel 公用文件夹位于 /var/www/my_project/public。

这是我的 nginx 配置(/etc/nginx/sites-enabled/):

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/my_project/public;
    index index.php index.html index.htm;

    server_name my_ip;

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

    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

实现所需配置的最佳方法是什么?

【问题讨论】:

  • 非常好的问题。让 laravel 应用程序与另一个应用程序在同一个虚拟主机上工作需要相当出色的工作
  • 你想要的是一个别名,把你的根留为/var/www/并添加一个位置为location /my_project { alias /var/www/my_project/public; }
  • Fabio,这是我尝试过的配置之一,但它不起作用。 http://my_domain.com/my_project/ 返回 nginx/Ubuntu 404 页面。不过还是谢谢你的建议!

标签: php laravel nginx laravel-5


【解决方案1】:

如果你想把你的laravel 项目放在subfolder 的服务器上,ngnix-ubuntu 16-php.7.2,那么这里是更新 ngnix 配置:

1) 您的嵌套(子文件夹)不在您的主文件夹中

/var/www/main:
/var/www/nested:

然后你的配置:

location /nested {

        alias /var/www/nested/public;

        try_files $uri $uri/ @nested;

               location ~ \.php$ {
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME $request_filename;
                        fastcgi_pass   unix:/run/php/php7.2-fpm.sock;

                                }
   }

location @nested {
        rewrite /nested/(.*)$ /nested/index.php?/$1 last;
}

2) 你的 laravel-test 文件夹(子文件夹)在你的主文件夹中:

/var/www/main:
/var/www/main/nested:

然后你的配置:

location /laravel-test {

    alias /var/www/main/laravel-test/public;

    try_files $uri $uri/ @laravelTest;

           location ~ \.php$ {
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $request_filename;
                    fastcgi_pass   unix:/run/php/php7.2-fpm.sock;

                            }


  }

location @laravelTest {
        rewrite /laravel-test/(.*)$ /laravel-test/index.php?/$1 last;
}

【讨论】:

    【解决方案2】:

    这对我有用。其他答案没有醒来

    location /rmbdatamis/ {
        root            /home/baiduwork/rmb-odp/webroot;
        index index.php;
        include     fastcgi.conf;
        fastcgi_pass $php_upstream;
        if (!-e $request_filename){
            rewrite ^/rmbdatamis/(.*) /rmbdatamis/index.php?/$1 last;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 2023-02-04
      • 2017-07-15
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多