【问题标题】:Configurate nginx to work with laravel public folder配置 nginx 以使用 laravel 公用文件夹
【发布时间】:2022-02-14 20:33:58
【问题描述】:

这是我的 nginx.conf 文件:

我所做的配置文件不起作用,它没有转到 http://localhost/api 我的系统是manjaro

     ....

    http {
           ....
        server {
                 ....
            }
        include /etc/nginx/sites-enabled/*;      
    }

我的 laravel 配置文件是:

        server {
            root /srv/tamsam/public;
            index index.php index.html index.htm;

            gzip            on;
            gzip_types      text/plain application/xml text/css application/javascript;
            gzip_min_length 1000;



        location ~ \.php$ {
        fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        }

            location /storage {
                alias /srv/tamsam/public;
                try_files $uri $uri/ @laravelapi;
                location ~ \.php$ {
                    
            fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;
            fastcgi_index  index.php;
            include        fastcgi.conf;
                }
            }

            location /api {
                alias /srv/tamsam/public;
                try_files $uri $uri/ @laravelapi;
            location ~ \.php$ {
            fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
            }
            }
            location @laravelapi {
                rewrite /api/(.*)?$ /api/index.php?$is_args$args last;
            }

    }

但是 http://localhost/api 给我错误 404 Not Found

感谢您的帮助,谢谢

【问题讨论】:

    标签: laravel nginx


    【解决方案1】:

    您的 @laravelapi 位置配置似乎不正确。

    location @laravelapi {
      rewrite /api/(.*)$ /api/index.php last;
    
    }
    

    请同时检查您的 php 配置

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

    fastcgi_param SCRIPT_FILENAME $request_filename; 对其工作很重要。

    【讨论】:

    • 现在我收到错误 403 禁止
    • 如果我设置 autoindex=on 不再有 403 消息但不加载 index.php
    • error_log 中有什么内容?
    • 实际上错误日志行在 nginx.conf 文件中被注释掉了,我最终解决了这个问题。谢谢
    猜你喜欢
    • 2023-02-04
    • 1970-01-01
    • 2015-03-03
    • 2015-02-08
    • 2017-10-12
    • 2016-03-06
    • 2017-07-15
    相关资源
    最近更新 更多