Why can’t I forward from the host port 8080 to guest port 80 (Mojave)

mojavevirtualbox

I'm running a virtual machine so I can work with WordPress; I have a database on my VM Linux machine in VBox and I'm using a Mac. I have to be able to use my mac to go to localhost/wp-admin so I can test code changes to my WordPress templates.

I have created a pf.anchor called com.user.forwarding and it says

rdr pass inet proto tcp from any to any port 8080 -> 127.0.0.1 port 80

my pf.conf file says:

scrub-anchor "com.apple/*"
nat-anchor "com.apple/*"
rdr-anchor "com.apple/*"
rdr-anchor "org.user.forwarding"
dummynet-anchor "com.apple/*"
anchor "com.apple/*"
load anchor "com.apple" from "/etc/pf.anchors/com.apple"
load anchor "org.user.forwarding" from "/etc/pf.anchors/org.user.forwarding"

in my VBox, with NAT, my settings are:

my settings in VBox

and in my hosts file is:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
127.0.0.1   mywebsite.com
::1     localhost

when I go to localhost:80/wp-admin, I get 'Safari can't open the page "localhost/wp-admin/" because the server where this page is located isn't responding.' My VM has nginx and mariadb running.

I've searched for the sysctl.conf file on my mac and it doesn't exist. I've done everything in here (except play with sysctl file which doesn't exist on my machine)

What's going on???

Best Answer

You have created the forwarding rule the wrong way around - i.e. you have reversed it.

Instead of:

rdr pass inet proto tcp from any to any port 8080 -> 127.0.0.1 port 80

It needs to look like this:

rdr pass inet proto tcp from any to any port 80 -> 10.0.2.15 port 8080

In regards to the sysctl.conf file, you'll absolutely need to make that change. It doesn't matter that the file doesn't exist on your computer - just create an empty file (/etc/sysctl.conf) and add those lines.

If you can't get the sysctl.conf file working, you can run the sysctl command manually on the command line:

 sudo sysctl -w net.inet.ip.forwarding=1