【问题标题】:nginx dynamic doute for laravel project use first paramlaravel 项目的 nginx 动态 doute 使用第一个参数
【发布时间】:2021-06-19 06:48:02
【问题描述】:

嗨,我是一名 laravel 开发人员。在 envato 工作。我有一些 laravel 项目。我想为我的 laravel 项目进行自定义配置。

例子

  1. http://demo.jubayed.com/demo1/

  2. http://demo.jubayed.com/demo2/

  3. http://demo.jubayed.com/demo3/

  4. http://demo.jubayed.com/demo4/

  5. http://demo.jubayed.com/demo5/

    root demo.jubayed.com/$demoProject/

server {
    listen 80;
    
    server_name demo.jubayed.com;

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

    index index.php;

    charset utf-8;

    location /($demoProject)/ {
        try_files $uri $uri/ /index.php?$query_string;
        root /var/www/$demoProject/public;
    }

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

    error_page 404 /index.php;
   # error_page 500 /index.php;
   # error_page 401 /index.php;
   # error_page 403 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        
    }

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

【问题讨论】:

    标签: php laravel nginx dynamic path


    【解决方案1】:

    您可能希望使用alias 而不是root,因为您需要操作URI 以将public 插入到路径名中。详情请见this document

    您还需要为 PHP 使用嵌套的 location,因为它需要访问相同的路径名。

    location ~ ^/(?<project>[^/]+)/(?<suffix>.*)$ {
        alias /var/www/$project/public/$suffix;
    
        if (!-e $request_filename) { rewrite ^ /$project/index.php last; }
    
        location ~ \.php$ {
            if (!-f $request_filename) { return 404; }
    
            include fastcgi_params;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }
    

    由于long term issues,我避免在同一块内使用try_filesalias。关于if的使用见this caution

    【讨论】:

      猜你喜欢
      • 2018-06-08
      • 1970-01-01
      • 2019-06-19
      • 2018-01-28
      • 2021-11-11
      • 2020-01-06
      • 1970-01-01
      • 2019-01-22
      • 2015-09-07
      相关资源
      最近更新 更多