nginx 密码保护 - Laravel 虚拟位置

nginx 密码保护 - Laravel 虚拟位置

我尝试限制对某些 laravel 路由路径的访问,但仍然没有成功。看起来请求是由 error_page 404 定义处理的,因为如果我注释掉这些行,nginx 会显示错误 404。

http://admin.domain.com/accounts/login

这是我的配置

server {
  listen 80;
   server_name admin.domain.com;
    error_page  404 = /index.php;
    if (!-e $request_filename) {
    rewrite ^.*$ /index.php last;
    }
    root /home/backend/admin/public;
    access_log /home/backend/admin/logs/access.log;
    error_log /home/backend/admin/logs/error.log;

    location / {
        index  index.html index.htm index.php;
    }


location ~ /accounts/login {
  auth_basic "Protected Area";
  auth_basic_user_file /etc/nginx/htpasswd;
location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.0-fpm-admin.sock;
        fastcgi_read_timeout 900s;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.0-fpm-admin.sock;
        fastcgi_read_timeout 900s;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}

答案1

我找到了解决方案。在我删除

如果(!-e $request_filename){重写^.*$ /index.php最后;}

因为这如果语句丢弃其他所有内容。

这样就可以了

server {
  listen 80;
  server_name admin.domain.com;
  error_page  404 = /index.php;
  index index.html index.htm index.php;
  root /home/backend/admin/public;
  access_log /home/backend/admin/logs/access.log;
  error_log /home/backend/admin/logs/error.log;

  location ~ /accounts/login {
  auth_basic "Protected Area";
  auth_basic_user_file /etc/nginx/htpasswd;
   }
  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }
  location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.0-fpm-admin.sock;
        fastcgi_read_timeout 900s;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

  }

相关内容