Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

iamsubingyawali's avatar

Laravel 9 API works with localhost but not with device IP

I have my Laravel 9 installed inside xampp/htdocs. It works fine when I use API URL with localhost but it throws 404 when I use my device's IP address.

http://localhost/myapp/public/api/users It works fine and returns me all the users

but

http://192.168.254.6/myapp/public/api/users gives me 404. Due to this I am not able to access the app from local network too.

I simply tested the case with only one php file test.php and http://192.168.254.6/testapp/test.php works fine.

I logged the incoming requests in index.php inside Laravel public directory and I found that Laravel is receiving the requests but when I tried to log the requests inside a controller and even inside a middleware, I got nothing.

What might be the reason for it?

0 likes
13 replies
sr57's avatar

Typo ?

blank between api/ users

moritzd's avatar

Are you actually using Apache to serve your Laravel app or are you using the Artisan CLI? If you are using Artisan you should try supplying the optional --host option when executing and use your local IP address (192.168.254.6).

Try using php artisan serve --host 192.168.254.6 and tell us if it works.

If you are using Apache to actually serve the project make sure that your server is actually listening on your local IP address and not only for localhost (I'd assume your server is already properly configured since accessing your testfile did work).

iamsubingyawali's avatar

@moritzd Yes, I am using Apache and not artisan CLI. As you stated, I can access my test app without any problems but I couldn't make my Laravel app work. What can go wrong here?

moritzd's avatar

@iamsubingyawali Are you sure you are getting a 404 status code? If so it seems like you configured your laravel application wrong in some way. If you couldn't reach your server at all you would not receive a response and not a 404.

Have you tried changing the APP_URL as @sr57 suggested? Also, are you sure you configured your Laravel project properly to work with XAMPP?

iamsubingyawali's avatar

@moritzd I was surprised to see the same thing. I logged the incoming requests inside public/index.php and I can see the request coming there. I tried logging in to middleware, but I couldn't see any request coming to middleware. The server is not fully unreachable, so I am getting a proper 404 message. I have manually handled the NOT FOUND exception and I can see that response from server while using IP address (but not when using localhost).

Are there any specific configurations to do in Apache before I access my app? The app works fine if I use localhost or loopback IP as domain name but when I use my local network IP it throws 404.

sr57's avatar

@iamsubingyawali

  • are you sure of this IP?

  • have you test changing APP_URL in .env?

  • anything in your apache logs?

iamsubingyawali's avatar

@sr57 Yes, I am 100% sure of that IP as I am getting a response from Laravel server, the real problem is I am not getting a proper response that the app was supposed to send.

Yes, I tried changing the APP_URL=http://localhost to APP_URL=http://192.168.254.6 but It didn't work.

I traced the Apache log, and I didn't see anything wrong there. Below is one of the request logs:

192.168.254.6 - - [31/Mar/2022:01:19:36 +0545] "GET /myapp/public/api/users HTTP/1.1" 404 63 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.88 Safari/537.36"
iamsubingyawali's avatar

@sr57 While using http://localhost/myapp/public/api/users

Using Log::info($request);

Inside middleware

[2022-03-31 07:38:36] local.INFO: Incoming Request from Middleware: GET /myapp/public/api/users HTTP/1.1
Accept:                    text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding:           gzip, deflate, br
Accept-Language:           en-US,en;q=0.9
Connection:                keep-alive
Cookie:                    __tawkuuid=e::localhost::cr/fft5J9ID9ZseIpmu6eKCWmCWJolFvQw9RJhwoTHZgOT0ECIgKotIkAOLcIeFS::2
Dnt:                       1
Host:                      localhost
Sec-Fetch-Dest:            document
Sec-Fetch-Mode:            navigate
Sec-Fetch-Site:            none
Sec-Fetch-User:            ?1
Sec-Gpc:                   1
Upgrade-Insecure-Requests: 1
User-Agent:                Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.88 Safari/537.36
Cookie: __tawkuuid=e::localhost::cr/fft5J9ID9ZseIpmu6eKCWmCWJolFvQw9RJhwoTHZgOT0ECIgKotIkAOLcIeFS::2

Inside public/index.php

[2022-03-31 07:38:40] local.INFO: Incoming Request at Index: GET /myapp/public/api/users HTTP/1.1
Accept:                    text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding:           gzip, deflate, br
Accept-Language:           en-US,en;q=0.9
Connection:                keep-alive
Cookie:                    __tawkuuid=e::localhost::cr/fft5J9ID9ZseIpmu6eKCWmCWJolFvQw9RJhwoTHZgOT0ECIgKotIkAOLcIeFS::2
Dnt:                       1
Host:                      localhost
Sec-Fetch-Dest:            document
Sec-Fetch-Mode:            navigate
Sec-Fetch-Site:            none
Sec-Fetch-User:            ?1
Sec-Gpc:                   1
Upgrade-Insecure-Requests: 1
User-Agent:                Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.88 Safari/537.36
Cookie: __tawkuuid=e::localhost::cr/fft5J9ID9ZseIpmu6eKCWmCWJolFvQw9RJhwoTHZgOT0ECIgKotIkAOLcIeFS::2

While using http://192.168.254.6/myapp/public/api/users

Using Log::info($request);

Inside middleware - No Logs

Inside public/index.php

[2022-03-31 07:46:36] local.INFO: Incoming Request at Index: GET /myapp/public/api/users HTTP/1.1
Accept:                    text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding:           gzip, deflate
Accept-Language:           en-US,en;q=0.9
Connection:                keep-alive
Dnt:                       1
Host:                      192.168.254.6
Sec-Gpc:                   1
Upgrade-Insecure-Requests: 1
User-Agent:                Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.88 Safari/537.36
iamsubingyawali's avatar

@sauravs012 I tried modifying the file as below but It didn't work.

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array<int, string>
     */
    protected $except = [
        'http://192.168.254.6/',
    ];
}
iamsubingyawali's avatar
Level 1

After long research, I found the solution. The issue was due to a package configuration file tenancy.php of https://tenancyforlaravel.com/

The old configuration was:

    /**
     * The list of domains hosting your central app.
     *
     * Only relevant if you're using the domain or subdomain identification middleware.
     */
    'central_domains' => [
        '127.0.0.1',
        'localhost',
    ],

Adding the specific local IP or a blank string fixed the issue:

    /**
     * The list of domains hosting your central app.
     *
     * Only relevant if you're using the domain or subdomain identification middleware.
     */
    'central_domains' => [
        '127.0.0.1',
        'localhost',
        ''
    ],

As stated in the code comments, this config should have been only valid while using domain or subdomain but I was using request data to identify tenant, still it was causing the issue.

Anyway, thank you all for your responses.

1 like

Please or to participate in this conversation.