dpb56083 2019-02-19 22:41
浏览 1126

使用nginx在Laravel API路由上找不到404

I am trying to set up a server with PHP 7.2 and nginx. I can get the laravel homepage to load when I navigate to my-site.com/snow however I can't get any of the API routes defined in API.php to load. All of my API routes are prefixed with /api/ like /api/login. I get a 404 error on all my API routes. What am I doing wrong?

location ~ \.php$ {
  include snippets/fastcgi-php.conf;
  # With php-fpm (or other unix sockets):
  fastcgi_pass unix:/run/php/php7.2-fpm.sock;
  # With php-cgi (or other tcp sockets):
  # fastcgi_pass 127.0.0.1:9000;
}

location /snow {
  index index.php;
}

location /snow/api {
  root /var/www/html/snow;
  rewrite ^/api/(.*)$ /$1 break;
  try_files $uri $uri/ /api/index.php$is_args$args;
}

location ~ snow/api/.+\.php$ {
  root /var/www/html/snow;
  rewrite ^/api/(.*)$ /$1 break;
  #include snippets/fastcgi-php.conf;
  fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}

.htaccess

Options -Indexes
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

<Files .env>
    Order allow,deny
    Deny from all
</Files>

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
  • 写回答

1条回答 默认 最新

  • douxie5930 2019-02-20 09:23
    关注

    If you're using the api.php routes file, Laravel already prefixes those routes with /api, so you may be duplicating that in your config. Check if /api/api/login works.

    If the above works, you don't need the two /api rules in your nginx config. Once you remove those rules, you should be able to access the APIs using /api/login.

    You can also try updating your nginx config to the following, as your current config doesn't appear to be pointing to the public directory of Laravel:

    location /snow/api {
      root /var/www/html/snow/pubic;
      try_files $uri $uri/ /index.php?$query_string;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services