laravel 不能在 nginx 中作为别名工作

Posted

技术标签:

【中文标题】laravel 不能在 nginx 中作为别名工作【英文标题】:laravel not working in nginx as alias 【发布时间】:2017-12-13 11:56:18 【问题描述】:

我有一个 nginx 网络服务器,我想在上面运行多个 laravel 项目

第一个项目工作正常,但第二个项目只打开主页,主页地址是

example.com/secondproject/

但是当我想打开时

example.com/secondproject/foo

nginx 将跳过 /secondproject 别名并在第一个项目中查找请求的路由(nginx 配置中服务器的根目录)

这是我的配置

server 
    listen 80;
    server_name example.com;
    rewrite_log on;

    location / 

        root /path/to/first-project/public;
        index index.php index.html index.htm;

        try_files $uri $uri/ $uri/index.php /index.php;

        location ~ \.php$ 
            fastcgi_pass unix:/run/php-fpm/www.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
        

    
    location /secondproject 

        alias /path/to/second-project/public;
        index index.php;

        location /secondproject 
            try_files $uri $uri/ $uri/index.php /index.php;
        

        location ~ \.php$ 
            fastcgi_pass unix:/run/php-fpm/www.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
        

    


当我替换时

root /path/to/second-project/public;

而不是

alias /path/to/second-project/public;

nginx 显示 403!

【问题讨论】:

【参考方案1】:

试试这样:

   server 

        listen 80;

        server_name example.com www.example.com;

        location / 

            root /path/to/first-project/public;
            index index.php index.html index.htm;

            try_files $uri $uri/ $uri/index.php /index.php;

            location ~ \.php$ 
                fastcgi_pass unix:/run/php-fpm/www.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $request_filename;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
            

        

        # ------- second

        location /secondproject 

            root /path/to/second-project/public;
            index index.php index.html index.htm;

            try_files $uri $uri/ $uri/index.php /index.php;

            location ~ \.php$ 
                fastcgi_pass unix:/run/php-fpm/www.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $request_filename;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
            

        

    

【讨论】:

添加位置后无法重启nginx /secondproject rewrite ^/* /secondproject last; 删除它,然后再试一次

以上是关于laravel 不能在 nginx 中作为别名工作的主要内容,如果未能解决你的问题,请参考以下文章

如何在laravel eloquent中为左连接表起别名

宅基地中的laravel websocket无法正常工作

别名环境中的 nginx path_info

nginx 从根服务静态文件并从别名上传文件

实战在Laravel中使用swoole来取代nginx作为http服务器

单个域上多个 laravel 应用程序的虚拟主机别名