简体   繁体   English

Nginx和Laravel:如何将“位置”块限制为从“ /”到“ / api”?

[英]Nginx and Laravel: How to restrict “location” block to from “/” to “/api”?

I have a Laravel application that I would like to use only for its /api routes. 我有一个Laravel应用程序,我只想将其用于/api路由。 When / , /login , /profile , etc are accessed, I would like nginx to serve the index file located in the base root, /var/www/html/index.html . 当访问//login/profile等时,我希望nginx提供位于基本根目录/var/www/html/index.html的索引文件。

Here's my current default.conf . 这是我当前的default.conf The /api routes work, but it also serves / from the backend, which I don't want. /api路线的工作,但它也可/从后端,这是我不想要的。 If I simply change location / to location /api , then the /api routes become inaccessible, and trying to access them returns the index file located in the base root, /var/www/html/index.html . 如果仅将location /更改为location /api ,则/api路由将变得不可访问,并且尝试访问它们将返回位于基本根目录/var/www/html/index.html的索引文件。 This is the opposite of what I am trying to achieve. 这与我要实现的目标相反。 Haha. 哈哈。

How can I keep the /api routes accessible, while also preventing / from being served by the backend? 如何保持/api路由可访问性,同时又防止/为后端提供服务?

server {

    listen  80;
    root /var/www/html;
    server_name _;
    index index.php index.html index.htm;

    # API routes should be handled by the backend (Laravel).
    ##### I want to change the following line from "/" to "/api".
    location / {
        root /var/www/backend/public;
        try_files $uri $uri/ /index.php?$query_string;

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

    }

}

According to your configuration, that should already be happening. 根据您的配置,这应该已经发生了。

the directive: 指令:

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

Will attempt first to locate the static file, and if the static file exists and is not a php file, it will be served by nginx directly. 首先将尝试查找静态文件,如果该静态文件存在并且不是php文件,它将由nginx直接提供。 Only if it is a php file will it be served with the php backend. 仅当它是一个php文件时,它将与php后端一起提供。

If you want to prevent .php files from being executed at all, you can modify to separate the / location from the /api location: 如果要完全阻止.php文件执行,可以修改以将/位置与/api位置分开:

location ~ ^/api/.*\.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_index index.php;
    include fastcgi_params;
}

I accomplished this, but somewhat inelegantly. 我完成了这个任务,但是有点不明智。 I have 2 location blocks: 我有2个位置块:

  • location ~ "^(?!/api).*$" matches all routes except those that begin with /api . location ~ "^(?!/api).*$"匹配除以 /api开头的那些路由以外的所有其他路由。
  • location / matches all other routes. location /匹配所有其他路线。 The reason that it does not match all routes is simply that nginx matches location blocks with regular expressions first. 它不匹配所有路由的原因仅仅是因为nginx首先将位置块与正则表达式匹配。

Strangely, using a regular expression for the second block to match all routes that do begin with /api did not work. 奇怪的是,使用正则表达式的第二块匹配那些开头的所有路由/api没有工作。 I still don't understand why. 我还是不明白为什么。

server {
  listen  80;
  root /var/www/html;
  server_name _;
  index index.php index.html index.htm;

  # All non-API routes should be handled by the frontend.
  # Use a regular expression to identify all requests
  # that do NOT begin with "/api".
  location ~ "^(?!/api).*$" {
    root /var/www/frontend/public;
    try_files $uri $uri/ /index.html =404;
  }

  # API routes should be handled by the backend (Laravel).
  # (Since regex-based location blocks are matched first,
  # this will be a fallback to the above location block).
  location / {
    root /var/www/backend/public;
    try_files $uri $uri/ /index.php?$query_string;

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

  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM