【问题标题】:Configure NGinx to distribute laravel in a subfolder配置 NGinx 以在子文件夹中分发 laravel
【发布时间】:2023-02-04 17:36:24
【问题描述】:

我尝试配置 Nginx 以将我的 laravel api 分发到我域的子文件夹中

server {
    listen 80;
    server_name stackoverflow.com;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
    add_header X-XSS-Protection "1; mode=block";

    charset utf-8;

    location /dev {

        alias /home/debian/www/stackoverflow/api/dev/public;

        try_files $uri $uri/ /dev/index.php?$query_string;

        location = /dev/favicon.ico { access_log off; log_not_found off; }
        location = /dev/robots.txt  { access_log off; log_not_found off; }

        error_page 404 /dev/index.php;
        error_log /home/debian/logs/nginx/error_log;
        access_log /home/debian/logs/nginx/access_log;

        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
            include fastcgi_params;
        }

    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

我尝试了不同的配置,在 try_files 上使用 /dev,没有 /dev,但总是出现相同的结果“找不到文件”和错误日志:

*1 FastCGI 在 stderr 中发送:“主脚本未知”,同时从上游读取响应标头,客户端:109.221.246.178,服务器:xxx,请求:“GET /dev/ HTTP/1.1”,上游:“fastcgi://unix: /var/run/php/php8.1-fpm.sock:", host: "xxx"

或者

*1 open() "/home/debian/www/xxx/api/dev/public/login" failed (2: No such file or directory), client: xxx, server: xxx, request: "GET /dev/login HTTP/1.1", 主机: "xxx"

我对 Nginx 的了解到此为止。我知道它是关于文件路径问题,但无法理解在哪里...... 当我尝试在 location / { } 上设置时,配置工作正常

非常感谢你的帮助 !

【问题讨论】:

    标签: php laravel nginx subdirectory


    【解决方案1】:

    这是我将 laravel 应用程序放在域子文件夹中时基于的 conf,您可以尝试在其上添加自定义设置。

    location /subsite {
        alias /var/www/project/subsite/public/;
        try_files $uri $uri/ @subsite;
    
        location ~ .php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            fastcgi_index index.php;
        }
    }
        
    location @subsite {
        rewrite /subsite/(.*)$ /subsite/index.php?/$1 last;
    }
    

    【讨论】:

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