Api returns data correctly when requested by localhost but not by ip

Asked

Viewed 157 times

2

Yesterday I did this question:

I made a REST api using Laravel, to an application that I’m doing , to perform the requisition tests I created a VM where is the api.

Assign a fixed ip to this VM and turn off the firewall to not bother.

I set it up like this:

In /etc/apache2/sites-enabled/000-default.conf:

<VirtualHost *:80>

  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
  ServerName api.dev
  DocumentRoot /var/www/html/apiTeste/public
</VirtualHost>

<Directory "/var/www/html/apiTeste">
  AllowOverride All
</Directory>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

In /etc/hosts:

127.0.0.1 localhost
127.0.1.1 ouvidoria
127.0.0.1 api.dev

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Performing test within the VM itself, if used http://api.dev this returns the Laravel home page, but if used http://api.dev/api/unidades this returns a 404 Not Found:

Not Found

The requested URL /api/unidades was not found on this server.

Apache/2.4.25 (Ubuntu) Server at api.dev Port 80

But if I spin the php artisan serve and access http://api.dev:8000/api/unidades this returns the data correctly.

Through the reply I was able to solve the problem I had at the time, and I can perform the request with the service running on apache, but only if I run this request inside the VM.

If you try to access from outside the VM, when entering the address http://192.168.0.23/ returns the page Apache2 Ubuntu Default Page as it should be.

Accessing http://192.168.0.23/apiTeste/public/ shows me the home page of Laravel.

But if I enter the requisition address, http://192.168.0.23/api/unidades this returns me an error of 404 Not Found:

Not Found

The requested URL /api/unidades was not found on this server.

Apache/2.4.25 (Ubuntu) Server at 192.168.0.23 Port 80
  • How do you top up the VM? Using Vagrant + Virtualbox, VB only, ... ?

  • I only use virtualbox

  • Go on your VM properties, go on the network tab, have an Adapter 1 as NAT has not? Create another, adpater 2 as Bridge Adapter with Prmiscuoues Mode Allow All.

  • @Rboschini o Adapter 1 ta em bridge com o ip setado manually

  • Tando that I can access the apache, case in Nat could not

  • I couldn’t figure out the error of this one, but I started a new one from scratch and it’s worked perfectly

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.