Laravel-配置 Nginx 資料夾 / 子目錄訪問-重定向方式

悠悠山雨發表於2020-04-16

功能實現

  • 域名http://127.0.0.1正常訪問,對應專案地址/var/www/top/public
  • 域名http://127.0.0.1/nested正常訪問,對應專案地址/var/www/nested/public

廢話不說上配置

Nginx配置檔案default.conf

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/top/public;

    index index.html index.htm index.php;

    server_name _;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location /nested {
        alias /var/www/nested/public;
        try_files $uri $uri/ @nested;
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
    }

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
}

參考

https://serversforhackers.com/c/nginx-php-...

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章